【问题标题】:Change repeater row value?更改中继器行值?
【发布时间】:2012-05-06 08:40:16
【问题描述】:

我正在尝试更改中继器中的值:(通过 itemdatabound 事件)

如果年份为空 - 设置值blabla

我的中继器:

 <ItemTemplate>
                <tr  >
                    <td  >
                        <%#Eval("year") %>
                   </td>

我的 C# 代码:

 void RPT_Bordereaux_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            if (string.IsNullOrEmpty(((DataRowView)e.Item.DataItem)["year"].ToString()))
            { 
                (((DataRowView)e.Item.DataItem)["year"]) = "blabla"; // ???????

            }
        }

它确实改变但不显示在转发器中(显示旧值)。

一种解决方案是在itemTemplate 中添加server controlliteral(runat 服务器) - 并在服务器中添加“findControl” - 并更改其值。

其他解决方案是通过 jQuery - 搜索空的最后一个 TD。

但是 - 我的问题:

还有其他服务器端解决方案吗()?

【问题讨论】:

    标签: c# asp.net webforms repeater itemdatabound


    【解决方案1】:

    你可以试试这样的:

    .aspx 中的中继器:

    <asp:Repeater ID="Repeater1" runat="server">
        <ItemTemplate>
        <table>
        <tr>
              <td> <%# GetText(Container.DataItem) %></td>
        </tr>
    
        </table>
        </ItemTemplate>
    </asp:Repeater>
    

    .cs

     protected static string GetText(object dataItem)
     {
        string year = Convert.ToString(DataBinder.Eval(dataItem, "year"));
        if (!string.IsNullOrEmpty(year))
        {
            return year;
        }
        else
        {
            return "blahblah";
        }
    
     }
    

    IN GetText 方法可以通过字符串检查是否为空而不是返回字符串。

    【讨论】:

    • 傻我,我怎么没想到...... - 猜我太依恋datarowview类......
    • 看看这里:stackoverflow.com/questions/9820464/…。和你想要的一样
    • 您的解决方案很好,但是您提供的链接没有回答我的问题,因为它是另一个问题。我需要设置一个值,他没有设置任何值
    【解决方案2】:

    您可以尝试使用在控件绑定之前而不是在控件绑定之后发生的 itemcreated 事件。第一个链接中的示例:

    http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.repeater.itemcreated.aspx

    http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.repeater_events.aspx

    ItemDataBound 在Repeater 控件中的项目被数据绑定之后但在页面上呈现之前发生。

    【讨论】:

      【解决方案3】:

      HTML 文件

      <asp:Repeater ID="RPT_Bordereaux" runat="server">
          <ItemTemplate>
          <table>
          <tr>
                <td> <%# GetValue(Container.DataItem) %></td>
          </tr>
      
          </table>
          </ItemTemplate>
      </asp:Repeater>
      

      .CS 代码

      protected void RPT_Bordereaux_ItemDataBound(object sender, RepeaterItemEventArgs e)
      {
          if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
         {
         }
      }
      
      
      protected static string GetValue(object dataItem)
      {
         string year = Convert.ToString(DataBinder.Eval(dataItem, "year"));
          if (!string.IsNullOrEmpty(year))
           {
             return Convert.ToString(year);
           }
           else
                  {
                      return "blahbla";
                  }
      
               }
      

      这应该可以工作

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-09-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-12-19
        • 1970-01-01
        相关资源
        最近更新 更多