【问题标题】:How can i capture a value from one dropdown menu when multiple are present?当存在多个下拉菜单时,如何从一个下拉菜单中捕获一个值?
【发布时间】:2014-03-08 08:51:39
【问题描述】:

我正在创建一个组件表,并且需要能够将下拉列表中的项目添加到表中的每个项目。这些列表是使用这样的 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呢?

更新:Moe S' 方法

我无法让它工作。我尝试了几种不同的方法来使用button.NamingContainer 访问下拉菜单,但一直遇到Object reference not set to an instance of an object. 错误。我的最后一次尝试如下:

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)button.Parent).Parent).Cells[0].Controls[0];

【问题讨论】:

  • 这是winforms、WPF、Web应用吗?
  • @BlackICE 不,不是。不过感谢您的回复!
  • 那是个什么样的应用呢?
  • @BlackICE DotNetNuke 5.(不幸的是)

标签: c# asp.net .net


【解决方案1】:

您应该能够使用以下内容访问表格行:

TableRow tblRow = (TableRow) button.NamingContainer;

然后使用 FindControl 选项访问 DropDownList

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

然后显然是SelectedValue来捕获值

【讨论】:

  • 我无法让它工作。你能看看我的更新,看看你是否知道出了什么问题? (我也完全按照您的方式尝试过,但也没有用)
  • 在使用 NamingContainer 获取对父控件的引用之前,尝试调试您的代码以查看您的变量“按钮”是否返回 null。您可能还想尝试投射您的按钮对象,看看是否有帮助。
  • 我刚刚添加了另一个更新。看起来按钮不为空。还有其他想法吗?
  • 这很有趣......假设我的控制层次结构是正确的,你能告诉你是否为 tblRow.. TableRow tblRow = (TableRow)(( TableCell) ((Button) Sender).NamingContainer).NamingContainer;
  • 看起来像是挂在表格单元格演员表上:Unable to cast object of type 'ASP.desktopmodules_robinson_maintenance_assignrigactiontypestocomponenttypes_ascx' to type 'System.Web.UI.WebControls.TableCell'.
猜你喜欢
  • 2021-08-15
  • 1970-01-01
  • 2020-11-13
  • 1970-01-01
  • 1970-01-01
  • 2011-07-21
  • 2021-12-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多