<asp:TreeView ID="TreeView1" runat="server">
</asp:TreeView>
-----------------------------------------------------------------------------------------------------
in Source Code (c#) page
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GetData();
BindTreeView(null,0);
}
}
protected void GetData()
{
DataTable dt = new DataTable();
string Constring = @"Data Source=MICROSOF-SXZEHI;Initial Catalog=test;Integrated Security=SSPI;";
SqlConnection con = new SqlConnection(Constring);
SqlDataAdapter da = new SqlDataAdapter("Select * from tree_Test", con);
da.Fill(dt);
ViewState["dtTree"] = dt;
}
protected void BindTreeView(TreeNode tn, int PaerntId)
{
DataTable dtTree = (DataTable)ViewState["dtTree"];
DataView dvTree = new DataView(dtTree);
dvTree.RowFilter = "parentid=" + PaerntId;
if (dvTree.Count > 0)
{
for (int iLoop = 0; iLoop < dvTree.Count; iLoop++)
{
TreeNode tnn = new TreeNode(dvTree[iLoop]["name"].ToString(), dvTree[iLoop]["id"].ToString());
BindTreeView(tnn, Convert.ToInt32(dvTree[iLoop]["id"].ToString()));
if (tn == null)
TreeView1.Nodes.Add(tnn);
else
tn.ChildNodes.Add(tnn);
}
}
}
No comments:
Post a Comment