.net 呼叫中继器内的按钮或下拉列表
我有一个.net 呼叫中继器内的按钮或下拉列表,.net,asp.net,vb.net,drop-down-menu,repeater,.net,Asp.net,Vb.net,Drop Down Menu,Repeater,我有一个标签和下拉列表在中继器内。单击中继器外的按钮时,我希望访问标签.Text值和ddl.SelectedIndex值 <asp:Repeater ID="rptProduct" runat="server" DataSourceID="objdsProduct" OnItemCommand="rptProduct"> <ItemTemplate> <div> <div> <asp:Label ID
标签
和下拉列表
在中继器内
。单击中继器外的按钮时,我希望访问标签.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" />
谢谢你抽出时间
Dim ProductName As String = DirectCast(rptProduct.FindControl("lblProductName"), Label).Text
Dim Size As Integer = DirectCast(rptProduct.FindControl("ddlSize"), DropDownList).SelectedValue
但是。。。如何识别中继器中要从中获取值的项目
有一个,特别是这个位:
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
Sub R1\u ItemCommand(发送方作为对象,e作为中继器commandeventargs)
Label2.Text=“Button”和_
Repeater1.Items(例如Item.ItemIndex.ItemIndex.ToString()&_
“刚刚被单击!
”
端接头
您必须迭代中继器行
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 .........
}
}
将此添加到您的按钮单击:
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
我会循环浏览我展示的每个产品吗?因此,产品1 lblName和ddlSize、产品2等。我还得到以下错误,对象引用未设置为对象的实例。在这种情况下,您只需将rptProduct.FindControl更改为item.FindControl检查重复循环示例:我只能添加项而不能添加项?无法识别命令
item.FindControl
,只有Items
它的C#代码,我认为vb.net中的FindControl equiulant是DirectCast,这就是你所接受的答案。如果您尝试使用C语言,上面的代码中没有错误#
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