2017-06-29 25 views
0

嗨内selectedvalues在ASP.NET以下VB代码:ASP.NET - 如何从dynamicallyproduced dropdownlists得到Repeater控件

<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> 

我现在需要获得创建下拉的每个实例的selectedvalues,和+ =添加到变量,以便我可以基于选定的值构建SQL INSERT命令。

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

回答

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; 
    } 
} 
+0

感谢VDWWD - 这看起来像正是我之后。我忘了提到我在VB中的代码 - 你有可能再次用VB显示吗? – OneBigEgg

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 
相关问题