本文介绍了维护每个postbak的treeview状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我有一个从SQL数据库绑定的树视图.父级和子节点的导航URL属性设置为页面..但是在每篇帖子中,我都无法保持树状视图的状态...我该怎么办sam

以下是我的代码
受保护的void Page_Load(对象发送者,EventArgs e)
{

PopulateNodes();
if(Request.QueryString ["uid"]!= null || Request.QueryString ["cid"]!= null)
{
getData();
}

}
公共无效PopulateNodes()
{
DataSet dst = GetTreeViewData();
foreach(dst.Tables ["Universities"].Rows中的DataRow masterRow)//循环遍历行.
{
TreeNode masterNode =新的TreeNode(masterRow ["u_name"].ToString());
masterNode.Value = masterRow ["uid"].ToString();
masterNode.NavigateUrl =〜/coursedetails.aspx?uid =" + masterRow ["uid"].ToString();
TreeView1.Nodes.Add(masterNode);
foreach(masterRow.GetChildRows("Children")中的DataRow childRow)
{
TreeNode childNode =新的TreeNode(childRow ["c_name"].ToString());
childNode.NavigateUrl =〜/coursedetails.aspx?cid =" + childRow ["cid"].ToString();
childNode.Value = childRow ["cid"].ToString();
masterNode.ChildNodes.Add(childNode);
}
}

}
公共数据集GetTreeViewData()
{
SqlConnection con =新的SqlConnection(WebConfigurationManager.AppSettings.Get("mysqlserver"));
SqlDataAdapter dadunivstys =新的SqlDataAdapter("SELECT u_name,id as uid from tbluniversity",con);
SqlDataAdapter dadcourses =新的SqlDataAdapter("SELECT c_name,id as cid,uid,type FROM tblcourses",con);
DataSet dst = new DataSet();
dadunivstys.Fill(dst,大学");
dadcourses.Fill(dst,"Courses");
dst.Relations.Add("Children",dst.Tables ["Universities"].Columns ["uid"],dst.Tables ["Courses"].Columns ["uid"]);
返回dst;
}



公共无效的getData()
{

if(Request.QueryString ["cid"]!= null)
{

DataTable dt = new DataTable();
dt = DALSelect.DAL_selectcoursedetails(Request.QueryString ["cid"].ToString());
如果(dt.Rows.Count> 0)
{
dlstcourse.Visible = true;
}
dlstcourse.DataSource = dt;
dlstcourse.DataBind();

}
如果(Request.QueryString ["uid"]!= null)
{
DataTable dt = new DataTable();
dt = DALSelect.DAL_selectuniversitydetails(Request.QueryString ["uid"].ToString());
dluniversity.DataSource = dt;
dluniversity.DataBind();
如果(dt.Rows.Count> 0)
{
dluniversity.Visible = true;

}


}
}
感谢您的帮助

Reogeo

Hi all,
i have a treeview binded from an sql database.Parent and child nodes have their navigateurl proprety set to a page ..But on each post back i can''t maintain the treeview status...What shuld i do for the sam

following is my code
protected void Page_Load(object sender, EventArgs e)
{

PopulateNodes();
if (Request.QueryString["uid"] != null || Request.QueryString["cid"] != null)
{
getData();
}

}
public void PopulateNodes()
{
DataSet dst = GetTreeViewData();
foreach (DataRow masterRow in dst.Tables["Universities"].Rows) // Loop over the rows.
{
TreeNode masterNode = new TreeNode(masterRow["u_name"].ToString());
masterNode.Value = masterRow["uid"].ToString();
masterNode.NavigateUrl = "~/coursedetails.aspx?uid=" + masterRow["uid"].ToString();
TreeView1.Nodes.Add(masterNode);
foreach (DataRow childRow in masterRow.GetChildRows("Children"))
{
TreeNode childNode = new TreeNode(childRow["c_name"].ToString());
childNode.NavigateUrl = "~/coursedetails.aspx?cid=" + childRow["cid"].ToString();
childNode.Value = childRow["cid"].ToString();
masterNode.ChildNodes.Add(childNode);
}
}

}
public DataSet GetTreeViewData()
{
SqlConnection con = new SqlConnection(WebConfigurationManager.AppSettings.Get("mysqlserver"));
SqlDataAdapter dadunivstys = new SqlDataAdapter("SELECT u_name,id as uid FROM tbluniversity", con);
SqlDataAdapter dadcourses = new SqlDataAdapter("SELECT c_name,id as cid,uid,type FROM tblcourses", con);
DataSet dst = new DataSet();
dadunivstys.Fill(dst, "Universities");
dadcourses.Fill(dst, "Courses");
dst.Relations.Add("Children", dst.Tables["Universities"].Columns["uid"], dst.Tables["Courses"].Columns["uid"]);
return dst;
}



public void getData()
{

if(Request.QueryString["cid"]!=null)
{

DataTable dt = new DataTable();
dt = DALSelect.DAL_selectcoursedetails(Request.QueryString["cid"].ToString());
if (dt.Rows.Count > 0)
{
dlstcourse.Visible = true;
}
dlstcourse.DataSource = dt;
dlstcourse.DataBind();

}
if (Request.QueryString["uid"]!= null)
{
DataTable dt = new DataTable();
dt = DALSelect.DAL_selectuniversitydetails(Request.QueryString["uid"].ToString());
dluniversity.DataSource = dt;
dluniversity.DataBind();
if (dt.Rows.Count > 0)
{
dluniversity.Visible = true;

}


}
}
Thanks for any help

Reogeo

推荐答案


这篇关于维护每个postbak的treeview状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 09:21