TreeView传值问题

magicbacon 2008-10-12 11:36:00
各位,我用TreeView显示部门组织结构,希望点击时传值到本页面,进行查询,请问能否实现,如何实现?谢谢各位~
...全文
552 29 打赏 收藏 转发到动态 举报
写回复
用AI写文章
29 条回复
切换为时间正序
请发表友善的回复…
发表回复
amandag 2008-10-13
  • 打赏
  • 举报
回复
关键是不能消除本页面已有的参数
==
什么参数?
IMAGSE 2008-10-13
  • 打赏
  • 举报
回复
rootNode.NavigateUrl = "xx.aspx?ID=1"; ???????? 这样可以不?
wanghao3616 2008-10-13
  • 打赏
  • 举报
回复
TreeView 应该可以了 我用他做过导航。。。
数据源是 xml文件
magicbacon 2008-10-13
  • 打赏
  • 举报
回复
关键TreeView可以很形象地表现组织结构,如果有什么好的替代办法也好啊,不用TreeView的~
magicbacon 2008-10-13
  • 打赏
  • 举报
回复
多谢ls,那个ajax extension什么的我早就装了,一直都没用,呵呵~
wjfmail 2008-10-13
  • 打赏
  • 举报
回复
如果是用URL传值,那就让数据源绑定到request.querystring上的treeview的值就是了.
按楼主说的已经换了个页面了;就没有必要用ajax了.

简单说说:updatepanel
在页面里先放入一个scriptmanager的控件.
然后是查询条件的控件,(它们的autopostback要设为true)
加入一个updatepanel控件,把查询结果的gridview之类的控件拖入这个updatepanel里.
然后再简单设一下updatepanel的triger属性.就可以work 了.

说的实在是有点简单.不好意思,水平有限.
你可以下载微软的ajax包.里面有很多demo和说明,比我详细多了.建议你看看这个(http://dflying.cnblogs.com)应该有所帮助.我以前也是向他学习的.
qinhl99 2008-10-13
  • 打赏
  • 举报
回复
对已经选择的参数,可以先ViewState起来啊
magicbacon 2008-10-13
  • 打赏
  • 举报
回复
我以为只有CSDN在用框架了,别处好像见得不多~
「已注销」 2008-10-13
  • 打赏
  • 举报
回复
使用框架吧!
magicbacon 2008-10-13
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 wjfmail 的回复:]
你只要把页面第一次初始化的内容放到if (!IsPostBack) {} 里面;postback肯定不会给你初始化了.而且你用了数据源绑定的话,把treeview的SelectedValue绑定到数据源的某个参数就是了.(没有用ajax)
当然入如果要ajax把结果的显示部分放到一个updatepanel 就是了.这就是最简单的ajax方式.
[/Quote]

我用TreeView是用URL传值,实际上是真正重新载入了页面,而不是POSTBACK。还有别的传值方法吗?

用UpdatePanel怎么做?简单说说,我再加分,呵呵~
zhangli0911 2008-10-13
  • 打赏
  • 举报
回复
protected void TreeViewType_SelectedNodeChanged(object sender, EventArgs e)
{
TreeView tree = sender as TreeView;
if (tree != null)
{
TreeNode node = tree.SelectedNode;
if (node != null)
{
if (node.Value == "-1")
model = 1;
else if (node.Parent != null && node.Parent.Value == "-1")
model = 2;
else if (node.Parent != null && node.Parent.Value == "-2")
model = 3;
else
{
TreeNode pNode = node;
string strValue = null;
do
{
strValue = pNode.Value;
pNode = pNode.Parent;
}
while (pNode.Value != "-1");

int.TryParse(strValue, out year);
model = 4;
}
}
//下面是重新绑定数据
// this.MyGridView1.OnBind();
}
}
wjfmail 2008-10-13
  • 打赏
  • 举报
回复
你只要把页面第一次初始化的内容放到if (!IsPostBack) {} 里面;postback肯定不会给你初始化了.而且你用了数据源绑定的话,把treeview的SelectedValue绑定到数据源的某个参数就是了.(没有用ajax)
当然入如果要ajax把结果的显示部分放到一个updatepanel 就是了.这就是最简单的ajax方式.
zabcd117 2008-10-13
  • 打赏
  • 举报
回复
window.location.hash
magicbacon 2008-10-13
  • 打赏
  • 举报
回复
回传我也想过,但是不知道弹出页面弹出后能不能不进行任何操作就自行关闭呢?我在Page_Load里用"window.close()"会出错。我可真是菜~
wjxluck 2008-10-13
  • 打赏
  • 举报
回复
不行的话 你把之前的参数保存起来
然后一并 传过去
我觉得你弹出一个窗口 然后在回传 父页面参数应该没影响吧
fellowcheng 2008-10-13
  • 打赏
  • 举报
回复
POSTBACK时你的参数被初始化了?

如果参数是TreeView的节点值 ,可以通过SelectedNodeChanged事件来获得参数,然后再查询绑定

 protected void TreeViewType_SelectedNodeChanged(object sender, EventArgs e)
{
TreeView tree = sender as TreeView;
if (tree != null)
{
TreeNode node = tree.SelectedNode;
if (node != null)
{
if (node.Value == "-1")
model = 1;
else if (node.Parent != null && node.Parent.Value == "-1")
model = 2;
else if (node.Parent != null && node.Parent.Value == "-2")
model = 3;
else
{
TreeNode pNode = node;
string strValue = null;
do
{
strValue = pNode.Value;
pNode = pNode.Parent;
}
while (pNode.Value != "-1");

int.TryParse(strValue, out year);
model = 4;
}
}
//下面是重新绑定数据
// this.MyGridView1.OnBind();
}
}
NIJIA72 2008-10-13
  • 打赏
  • 举报
回复
AJAX 可手动写postback 回调本页面
tm62490309 2008-10-13
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 magicbacon 的回复:]
引用 7 楼 amandag 的回复:
关键是不能消除本页面已有的参数
==
什么参数?


我的意思是页面中已经选择了得一些条件,希望在POSTBACK时不会被初始化。

唔,先看一下melon23兄的答复。
[/Quote]

傻一点的办法是先把这些已经选择了的条件用什么保存起来 然后在进行绑定 暂时只想到这个
思考ing//....
magicbacon 2008-10-13
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 amandag 的回复:]
关键是不能消除本页面已有的参数
==
什么参数?
[/Quote]

我的意思是页面中已经选择了得一些条件,希望在POSTBACK时不会被初始化。

唔,先看一下melon23兄的答复。
melon23 2008-10-13
  • 打赏
  • 举报
回复
最近我在用treeview+自定义SiteMapDataSource控件读取数据库数据做导航列。
如不消除本页面已有的参数,试一下在自定义SiteMapDataSource控件的 BuildSiteMapRecurse 方法
给string url添加你的参数
HttpContext context = new HttpContext();
context.Request.QueryString....添加参数。

我的代码如下:
<asp:TreeView
id="treeCategories"
ImageSet="Msdn"
DataSourceID="srcSiteMap"
AutoGenerateDataBindings="false"
SelectedNodeStyle-BackColor="#CCCCCC"
Runat="server" OnDataBound="treeCategories_DataBound">
<DataBindings>
<asp:TreeNodeBinding TextField="Title" ValueField="Key" SelectAction="Select" />
</DataBindings>
</asp:TreeView>

<asp:SiteMapDataSource
id="srcSiteMap"
StartingNodeUrl="~/Products.aspx"
Runat="server" />


自定义SiteMapDataSource控件:
using System;
using System.Collections.Specialized;
using System.Web.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.Caching;

namespace AspNetUnleashed
{

/// <summary>
/// Returns Site Map from database
/// </summary>
public class CategorySiteMapProvider : StaticSiteMapProvider
{

private bool _isInitialized = false;
private SiteMapNode _rootNode;

private string _connectionString;
private string _navigateUrl;
private string _idFieldName;

/// <summary>
/// Loads configuration settings from Web configuration file
/// </summary>
public override void Initialize(string name, NameValueCollection attributes)
{
if (_isInitialized)
return;

base.Initialize(name, attributes);

// Get database connection string from config file
string connectionStringName = attributes["connectionStringName"];
if (String.IsNullOrEmpty(connectionStringName))
throw new Exception("You must provide a connectionStringName attribute");
_connectionString = WebConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString;
if (String.IsNullOrEmpty(_connectionString))
throw new Exception("Could not find connection string " + connectionStringName);

// Get navigateUrl from config file
_navigateUrl = attributes["navigateUrl"];
if (String.IsNullOrEmpty(_navigateUrl))
throw new Exception("You must provide a navigateUrl attribute");

// Get idFieldName from config file
_idFieldName = attributes["idFieldName"];
if (String.IsNullOrEmpty(_idFieldName))
_idFieldName = "id";

_isInitialized = true;
}

/// <summary>
/// Retrieve the root node by building the Site Map
/// </summary>
protected override SiteMapNode GetRootNodeCore()
{
HttpContext context = HttpContext.Current;
return BuildSiteMap();
}

/// <summary>
/// Resets the Site Map by deleting the
/// root node. This causes the BuildSiteMap()
/// method to rebuild the Site Map
/// </summary>
public void ResetSiteMap()
{
_rootNode = null;
}

/// <summary>
/// Build the Site Map by retrieving
/// records from database table
/// </summary>
/// <returns></returns>
public override SiteMapNode BuildSiteMap()
{
// Only allow the Site Map to be created by a single thread
lock (this)
{
if (_rootNode == null)
{
// Show trace for debugging
HttpContext context = HttpContext.Current;
HttpContext.Current.Trace.Warn("Loading category site map from database");

// Clear current Site Map
Clear();

// Load the database data
DataTable tblSiteMap = GetSiteMapFromDB();

// Get the root node
_rootNode = GetRootNode(tblSiteMap);
AddNode(_rootNode);

// Build the child nodes
BuildSiteMapRecurse(tblSiteMap, _rootNode);
}
return _rootNode;
}
}

/// <summary>
/// Load the contents of the database table
/// that contains the Site Map
/// </summary>
private DataTable GetSiteMapFromDB()
{
string selectCommand = "SELECT Id,ParentId,Title,Description FROM Categories";
SqlDataAdapter dad = new SqlDataAdapter(selectCommand, _connectionString);
DataTable tblSiteMap = new DataTable();
dad.Fill(tblSiteMap);
return tblSiteMap;
}

/// <summary>
/// Get the root node from the DataTable
/// </summary>
private SiteMapNode GetRootNode(DataTable siteMapTable)
{
DataRow[] results = siteMapTable.Select("ParentId IS NULL");
if (results.Length == 0)
throw new Exception("No root node in database");
DataRow rootRow = results[0];
return new SiteMapNode(this, rootRow["Id"].ToString(), _navigateUrl, rootRow["title"].ToString(), rootRow["description"].ToString());
}

/// <summary>
/// Recursively build the Site Map from the DataTable
/// </summary>
private void BuildSiteMapRecurse(DataTable siteMapTable, SiteMapNode parentNode)
{
DataRow[] results = siteMapTable.Select("ParentId=" + parentNode.Key);
foreach (DataRow row in results)
{
string url = String.Format("{0}?{1}={2}", _navigateUrl, _idFieldName, row["id"]);

SiteMapNode node = new SiteMapNode(this, row["Id"
].ToString(), url, row["title"].ToString(), row["description"].ToString());
AddNode(node, parentNode);
BuildSiteMapRecurse(siteMapTable, node);
}
}

}
}
导航地图:
<?xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
<siteMapNode url="~/default.aspx" title="Home " description="Home page">
<siteMapNode provider="CategorySiteMapProvider" />
<siteMapNode url="~/shoppingcart.aspx" title="Shopping Cart" description="View Shopping Cart" />
<siteMapNode url="~/contactinfo.aspx" title="Contact Us" description="Contact us by phone or email" />
<siteMapNode siteMapFile="~/manage/web.sitemap" />
</siteMapNode>
</siteMap>

加载更多回复(8)

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧