简体   繁体   English

在动态生成的菜单中获取SelectedItem的ID

[英]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.

相关问题 如何获取动态生成的控件的ID? - How can I get ID's of Dynamically generated Controls? 如何在一组动态生成的组合框上设置SelectedItem - How to set the SelectedItem on a set of dynamically generated comboboxes 无法获取,从包含在wpf MVVM中动态生成的单选按钮的列表框中设置selectedItem - unable to get, set selectedItem from the listbox containing radiobuttons being generated dynamically in wpf MVVM 获取Razor中动态生成的按钮的ID - Get id of dynamically generated button in Razor 获取动态生成的ajax文本框ID - get dynamically generated ajax textbox id 获取动态生成的文本框的ID,该文本框正在调用javascript函数 - Get the ID of the dynamically generated textbox which is calling a javascript function 单击时如何获取动态生成的按钮的id - How to get the id of dynamically generated button when clicked 从上下文菜单中获取所选菜单项的名称(或索引),该菜单项是通过绑定到ObservableCollection的ItemsSource动态生成的 - Get name(or index) of selected menu item from context menu, which was dynamically generated via ItemsSource bound to a ObservableCollection 如何获取动态生成的usercontrol控件的属性值? - How to get the property value of dynamically generated usercontrol's control? 如何获取 Azure Functions 的框架生成的请求 ID? - How to get Azure Functions's framework generated request id?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM