2017-06-29 26 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 
相關問題