ASP.NET - 如何从Repeater Control中的动态生成的下拉列表中获取选定值

时间:2017-06-29 13:53:27

标签: asp.net repeater

您在ASP.NET中有以下vb代码:

<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SQL_SRC_DEV_HIERACHY">
    <ItemTemplate>
        <p class="input_title">Hierachy:
            <asp:Literal ID="Literal1" runat="server" Text='<%# Eval("Hierarchy_SKey")%>'></asp:Literal><br />
        </p>
        <asp:DropDownList ID="DropDownList_H" runat="server" DataSourceID="SQL_SRC_DEV_GROUPINGDESCS" DataTextField="Description" DataValueField="FTE_PARENT_SKEY"></asp:DropDownList><br />
    </ItemTemplate>
</asp:Repeater>

我现在需要为每个创建的下拉列表获取选定值,并且+ =到一个变量,以便我可以根据所选值构建一个SQL INSERT命令。

有人能指出我正确的方向来实现这一目标吗?谢谢。

2 个答案:

答案 0 :(得分:0)

您可以在后面的代码中循环Repeater中的项目,并使用FindControl来获取DropDownList的SelectedValue。

protected void Button1_Click(object sender, EventArgs e)
{
    //loop all the items in the repeater
    foreach (RepeaterItem item in Repeater1.Items)
    {
        //use findcontrol to locate the dropdownlist and cast it back to one
        DropDownList drp = item.FindControl("DropDownList_H") as DropDownList;

        //display the result
        Label1.Text += drp.SelectedValue;
    }
}

答案 1 :(得分:0)

感谢VDWWD - 我已经转换为VB并且它完美运行!非常感谢。

VB版本如下所示:

Protected Sub GEN_CODE()

    Dim txtField As DropDownList
    Dim DDL_Values As String = ""
    For Each item In Repeater1.Items

        //use findcontrol to locate the dropdownlist and cast it back to one
        txtField = item.FindControl("DropDownList_H")

        //display the result
        DDL_Values += " " + txtField.SelectedValue

    Next

End Sub