C# 当存在多个下拉菜单时,如何从一个下拉菜单中捕获值?

C# 当存在多个下拉菜单时,如何从一个下拉菜单中捕获值?,c#,asp.net,.net,C#,Asp.net,.net,我正在创建一个组件表,需要能够将下拉列表中的项目添加到表中的每个项目。这些列表是使用如下foreach以编程方式添加的: MyDatabase db = new MyDatabase(); if (db.ComponentTypes.Count() > 0) { foreach (ComponentType componentType in db.ComponentTypes) { // Header row components Tabl

我正在创建一个组件表,需要能够将下拉列表中的项目添加到表中的每个项目。这些列表是使用如下foreach以编程方式添加的:

MyDatabase db = new MyDatabase();

if (db.ComponentTypes.Count() > 0)
{
    foreach (ComponentType componentType in db.ComponentTypes)
    {
        // Header row components
        TableRow componentRow = new TableRow();
        TableCell componentTypeCell = new TableCell();

        // Create Header Row
        componentTypeCell.ColumnSpan = 5;
        componentTypeCell.Text = componentType.Name;
        componentTypeCell.Attributes.Add("style", "background: black; color: white; font-weight: bold;");

        componentRow.Cells.Add(componentTypeCell);
        tblRigActionTypesAndComponentTypes.Rows.Add(componentRow);

        // Middle portion omitted for simplicity

        //=================================================
        // Relevant portion

        // DDL Row Components
        TableRow addActionRow = new TableRow();
        TableCell rigActionTypeMenuCell = new TableCell();
        TableCell addRigActionTypeButtonCell = new TableCell();
        DropDownList ddlRigActionTypeMenu = new DropDownList();
        Button addRigActionTypeButton = new Button();

        // Populate dropdown with action types
        Helper.PopulateDropdownWithActionTypes(ddlRigActionTypeMenu);
        rigActionTypeMenuCell.Controls.Add(ddlRigActionTypeMenu);

        addRigActionTypeButton.Text = "Add This Action";
        addRigActionTypeButton.CommandName = "Add";
        addRigActionTypeButton.CommandArgument = componentType.ID.ToString();
        addRigActionTypeButtonCell.ColumnSpan = 4;
        addRigActionTypeButtonCell.Controls.Add(addRigActionTypeButton);

        addActionRow.Cells.Add(rigActionTypeMenuCell);
        addActionRow.Cells.Add(addRigActionTypeButtonCell);
        tblRigActionTypesAndComponentTypes.Rows.Add(addActionRow);
    }
}
按钮处理程序

protected void ButtonHandler(object sender, EventArgs e)
{
    Button button = (Button)sender;
    MyDatabase db = new MyDatabase();

    if (button.CommandName == "Add")
    {
        // How do I capture the selected value from the 
        // dropdown menu paired with the "add" button?   
    }
}
使用
CommandArgument
属性捕获按钮所属的组件很容易,但是如何获取相应的DDL呢

更新:教育部的方法

我一直无法让它工作。我尝试了几种不同的方法来使用
按钮访问下拉菜单。NamingContainer
,但一直点击未设置为对象实例的
对象引用。
错误。我的最后一次尝试如下:

Control control = button.NamingContainer;
Control test = control.FindControl("ddlRigActionTypeMenu");
lblPageHeader.Text = test.UniqueID;
更新2:

为了进一步了解上述(非工作)代码,以下内容确实有效:

Control control = button.NamingContainer;
lblPageHeader.Text = button.NamingContainer.UniqueID;
这会将页面标题更改为
dnn$ctr498$AssignRigActionTypesToComponentTypes

已解决

我认为Moe是公认的答案,因为他为我指明了正确的方向,但最终对我有效的是
Parent
,而不是
NamingContainer
。但所有这些原则仍然适用

解决方案:


DropDownList ddl=(DropDownList)((TableRow)((TableCell)按钮.Parent.Parent).Cells[0]。控件[0]

您应该能够使用以下命令访问表行:

TableRow tblRow = (TableRow) button.NamingContainer;
然后使用FindControl选项访问DropDownList

DropDownList ddlMenu = (DropDownList) tblRow.FindControl("ddlRigActionTypeMenu");

然后很明显地选择了值来捕获值

这是winforms、WPF、web应用程序吗?@BlackICE不,不是。谢谢你的回复!那么它是什么样的应用呢?@BlackICE DotNetNuke 5。(不幸的是)我没能让它工作。你能看一下我的更新,看看你是否知道出了什么问题?(我也像您一样尝试了这一点,但也不起作用)在使用NamingContainer获取对父控件的引用之前,请尝试调试代码以查看变量“button”是否返回null。您可能还想尝试强制转换按钮对象,看看这是否有帮助。我刚刚添加了另一个更新。看起来按钮不是空的。还有其他想法吗?那很有趣。。。假设我的控制层次结构是正确的,你能告诉我tblRow的值是否为空吗。。TableRow tblRow=(TableRow)((TableCell)((按钮)发送器).NamingContainer.NamingContainer;它似乎挂在表单元格强制转换处:
无法将“ASP.desktopmodules\u robinson\u maintenance\u assignrigactiontypestocomponenttypes\u ascx”类型的对象强制转换为“System.Web.UI.WebControl.TableCell”。