[英]Get SelectedItem's Id in dynamically generated Menu
I have created a Menu Dynamically. 我已经动态创建了一个菜单。
Here is the structure of my Table: 这是我的表的结构:
MenuItemId int PrimaryKey
MenuItemName nvarchar(50)
ParentId int
NavigateURL nvarchar(500)
Here is the C# code to get that data in Heirarchical structure: 这是以Heirarchical结构获取数据的C#代码:
private void GetMenuItems()
{
string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
SqlConnection con = new SqlConnection(cs);
SqlDataAdapter da = new SqlDataAdapter("spGetMenuData", con);
DataSet ds = new DataSet();
da.Fill(ds);
foreach (DataRow dr in ds.Tables[0].Rows)
{
if (dr["ParentID"].ToString().Trim() == "")
{
MenuItem item = new MenuItem();
item.Text = dr["MenuItemName"].ToString();
item.NavigateUrl = dr["NavigateUrl"].ToString();
foreach (DataRow drChild in ds.Tables[0].Rows)
{
if (drChild["ParentID"].ToString() == dr["MenuItemID"].ToString())
{
GetChildItems(ds, drChild, item);
}
}
Menu1.Items.Add(item);
}
}
}
private static void GetChildItems(DataSet ds, DataRow dr, MenuItem item)
{
MenuItem childItem = new MenuItem();
childItem.Text = dr["MenuItemName"].ToString();
childItem.NavigateUrl = dr["NavigateUrl"].ToString();
foreach (DataRow drChild in ds.Tables[0].Rows)
{
if (drChild["ParentID"].ToString() == dr["MenuItemID"].ToString())
{
GetChildItems(ds, drChild, childItem);
}
}
item.ChildItems.Add(childItem);
}
Here is my Html: 这是我的HTML:
<asp:Menu ID="Menu1" runat="server" DataSourceID="" BackColor="#B5C7DE" DynamicHorizontalOffset="2" Font-Names="Verdana"
Font-Size="0.8em" ForeColor="#284E98" StaticSubMenuIndent="10px" BorderStyle="Groove" BorderColor="WhiteSmoke" DynamicMenuStyle-BorderStyle="Outset">
<DynamicHoverStyle BackColor="Red" ForeColor="White" />
<DynamicMenuItemStyle HorizontalPadding="15px" VerticalPadding="10px" />
<DynamicMenuStyle BackColor="#99ff99" />
<DynamicSelectedStyle BackColor="Green" />
<StaticHoverStyle BackColor="Blue" ForeColor="White" />
<StaticMenuItemStyle HorizontalPadding="10px" VerticalPadding="10px" />
<StaticMenuStyle BackColor="#ff6699" />
<StaticSelectedStyle BackColor="Green" />
</asp:Menu>
Now I want to get the selectedItem's MenuItemId from that menu. 现在,我想从该菜单中获取selectedItem的MenuItemId。
Can anybody tell me how can I get the same? 有人可以告诉我如何获得相同的礼物吗?
Thanks. 谢谢。
这应该做。
string s = Menu1.SelectedValuePath=ID;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.