在转发器内调用按钮或下拉列表

时间:2011-05-22 12:08:40

标签: .net asp.net vb.net drop-down-menu repeater

我在label内有dropdownlistrepeater。当我点击转发器外部的按钮时,我想要访问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" />

有关我如何访问lblProdName.TextddlSize.SelectedValue的任何建议:

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

   Dim ProductName
   Dim Size

End Sub

感谢您的时间。

3 个答案:

答案 0 :(得分: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

答案 1 :(得分: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 .........

    }
}

答案 2 :(得分: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