<asp:Menu ID="Menu1" runat="server">
</asp:Menu>
-------------------------------------------------------------------------------------------
In Source Code (c#) page
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GetData();
BindMenu(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 BindMenu(MenuItem mn, int ParentId)
{
DataTable dtTree = (DataTable)ViewState["dtTree"];
DataView dvTree = new DataView(dtTree);
dvTree.RowFilter = "parentid=" + ParentId;
if (dvTree.Count > 0)
{
for (int iLoop = 0; iLoop < dvTree.Count; iLoop++)
{
MenuItem mnn = new MenuItem(dvTree[iLoop]["name"].ToString(), dvTree[iLoop]["id"].ToString());
BindMenu(mnn, Convert.ToInt32(dvTree[iLoop]["id"].ToString()));
if (mn == null)
Menu1.Items.Add(mnn);
else
mn.ChildItems.Add(mnn);
}
}
}
No comments:
Post a Comment