Call a button or dropdownlist inside a repeater
I have a label
and dropdownlist
inside a repeater
. When I click a button outside the repeater I would like to access the label.Text
value and ddl.SelectedIndex
value.
<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="开发者_如何学CobjdsSize" 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" />
Any suggestions how I can access lblProdName.Text
and ddlSize.SelectedValue
within:
Protected Sub btnChoose_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnChoose.Click
Dim ProductName
Dim Size
End Sub
Thank you for your time.
Add this to your button click:
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
Dim ProductName As String = DirectCast(rptProduct.FindControl("lblProductName"), Label).Text
Dim Size As Integer = DirectCast(rptProduct.FindControl("ddlSize"), DropDownList).SelectedValue
But ... how are you going to identify which item in the repeater you want to get the values from?
Have a look at this MSDN page, specifcally this bit:
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
You have to iterate repeater rows....
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 .........
}
}
精彩评论