2011-05-22 42 views
2

我在repeater內有labeldropdownlist。當我點擊中繼器外的按鈕時,我想訪問label.Text值和ddl.SelectedIndex值。調用中繼器內的按鈕或下拉列表

<asp:Repeater ID="rptProduct" runat="server" DataSourceID="objdsProduct" OnItemCommand="rptProduct"> 
    <ItemTemplate> 
    <div> 
     <div> 
     <asp:Label ID="lblProdName" runat="server" Text='<%# Eval("ProductName") %>'></asp:Label> 
     </div> 
     <div> 
     <asp:DropDownList ID="ddlSize" runat="server" AutoPostBack="False" DataSourceID="objdsSize" DataTextField="SizeName" AppendDataBoundItems="True" DataValueField="SizeID"> 
      <asp:ListItem Text="select a size" Value=0></asp:ListItem> 
     </asp:DropDownList> 
    </div> 
    </ItemTemplate> 
</asp:Repeater> 

<asp:Button ID="btnChoose" runat="server" Text="Choose Products" /> 

任何建議,我怎麼可以在訪問和ddlSize.SelectedValue

Protected Sub btnChoose_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnChoose.Click 

    Dim ProductName 
    Dim Size 

End Sub 

謝謝您的時間。

回答

1

添加到您的按鈕點擊:

Dim item As RepeaterItem 
For Each item In rptProduct.Items 
    Dim ProductName As String = DirectCast(item.FindControl("lblProdName"), Label).Text 
Dim Size As Integer = (DirectCast(item.FindControl("ddlSize"), DropDownList).SelectedValue 
Next item 
+0

WOOOOOOOOOOOOOOOT !!!最後一個可用的答案:) – CustomX 2011-05-22 14:10:23

1
Dim ProductName As String = DirectCast(rptProduct.FindControl("lblProductName"), Label).Text 
    Dim Size As Integer = DirectCast(rptProduct.FindControl("ddlSize"), DropDownList).SelectedValue 

但是......您將如何確定您想要從中繼器中獲取值?

有一個look at this MSDN page,比較特別的位:

Sub R1_ItemCommand(Sender As Object, e As RepeaterCommandEventArgs) 
    Label2.Text = "Button " & _ 
     Repeater1.Items(e.Item.ItemIndex).ItemIndex.ToString() & _ 
     " has just been clicked! <br />" 
End Sub 
+0

好我會循環顯示每個展示的產品?因此Product 1 lblName和ddlSize,Product 2等。 – CustomX 2011-05-22 13:30:42

+0

我還得到以下錯誤,對象引用未設置爲對象的實例。 – CustomX 2011-05-22 13:36:42

+0

在這種情況下,您只需將rptProduct.FindControl更改爲item.FindControl,請在此處查看重複器循環示例:http://msdn.microsoft.com/zh-cn/library/system.web.ui.webcontrols.repeateritem.aspx #Y5867 – gbs 2011-05-22 13:42:04

1

你必須重複轉發行....

protected void btnChoose_Click(object sender, EventArgs e) 
{ 
    foreach (RepeaterItem item in Repeater1.Items) 
    { 
     Label lblProdName = item.FindControl("lblProdName") as Label; 
     lblProdName.Text ......... 
     DropDownList ddlSize = item.FindControl("ddlSize") as DropDownList; 
     ddlSize.SelectedValue ......... 

    } 
} 
+0

命令'Item.FindControl'不被識別,只有'Items' – CustomX 2011-05-22 14:00:52

+0

其C#代碼,我認爲vb.net中的FindControl equiulant是DirectCast,這就是你接受的答案。上面的代碼沒有錯,如果你用C# – 2011-05-22 16:46:17

相關問題