【问题标题】:Call a button or dropdownlist inside a repeater在中继器中调用按钮或下拉列表
【发布时间】:2011-05-22 12:08:40
【问题描述】:

我在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" />

关于我如何访问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

感谢您的宝贵时间。

【问题讨论】:

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


    【解决方案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
    

    【讨论】:

      【解决方案2】:
        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 lblName 和 ddlSize,产品 2 等等。
      • 我也收到以下错误,对象引用未设置为对象的实例。
      • 在这种情况下,您只需将 rptProduct.FindControl 更改为 item.FindControl 检查中继器循环示例:msdn.microsoft.com/en-us/library/…
      • 我只能添加项目,不能添加项目?
      【解决方案3】:

      您必须迭代中继器行....

      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 .........
      
          }
      }
      

      【讨论】:

      • 无法识别命令Item.FindControl,只能识别Items
      • 它的 C# 代码,我认为 vb.net 中的 FindControl 等价物是 DirectCast,这就是你接受答案的方式。如果你在 C# 中尝试,上面的代码没有错
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-11
      相关资源
      最近更新 更多