【问题标题】:asp.net repeater - get value of row item _DataBoundasp.net 中继器 - 获取行项_DataBound 的值
【发布时间】:2013-03-28 17:47:11
【问题描述】:

如何在转发器 _DataBound 子中获取我想要的每个单元格的值? - 这样我就可以稍微更改值并将更改应用于文字

【问题讨论】:

    标签: asp.net repeater itemdatabound


    【解决方案1】:

    假设这是您的中继器(因为您没有包含任何代码):

    <asp:Repeater ID="_r" runat="server">
        <ItemTemplate>
            <asp:Literal ID="_lit" Text='<%# Eval("yourItemName")%>' runat="server"></asp:Literal>
            <br /><br />
        </ItemTemplate>
    </asp:Repeater>
    

    并且您创建了一个 ItemDataBound 事件,因为您想在每个文字的末尾添加“meow”(否则只会显示来自数据源的 yourItemName 的值):

    protected void Page_Load(object sender, EventArgs e)
    {
        _r.DataSource = table;
        _r.ItemDataBound += new RepeaterItemEventHandler(RItemDataBound);
        _r.DataBind();
    } 
    
    protected void RItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Literal lit = (Literal)e.Item.FindControl("_lit");
            lit.Text += "meow";
        }
    }
    

    阅读:Repeater.ItemDataBound Event

    【讨论】:

      【解决方案2】:

      您可以在该行中获取文字,但将事件转换为 arg:

      Protected Sub repeater_dataBind(ByVal sender As Object, ByVal e As RepeaterItemEventArgs) Handles myRepeater.ItemDataBound
          If (e.Item.ItemType <> ListItemType.Item And e.Item.ItemType <> ListItemType.AlternatingItem) Then
                  Return
          Else
             Dim myLiteral As New Literal
             myLiteral = CType(e.Item.FindControl("IDofLiteral"), Literal)
          End If
      
      end Sub
      

      【讨论】:

      • 只是一个小指针,如果您在if 语句中执行return,则不需要else 语句。
      • 只看到e.Item.ItemtypeItemAlternatingItem 会更简洁,然后你就不需要“else”语句了。
      【解决方案3】:

      您可以使用FindControl 获取repeater 中单元格的值。

      protected void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
      {
          if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
          {
              Literal lbl = (Literal)e.Item.FindControl("ltrl");  
              lbl.Text = // will give you the value for each `ItemIndex`
      
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-02-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多