【问题标题】:Find and change a specific item in Repeater在中继器中查找和更改特定项目
【发布时间】:2013-02-05 18:18:38
【问题描述】:

我有以下<asp:Repeater>

<asp:Repeater ID="repMenuParent" runat="server" DataSourceID="odsMenuParent" OnItemCommand="repMenuParent_OnItemCommand" OnItemDataBound="repMenuParent_OnItemDataBound" >
                <ItemTemplate>                        
                    <asp:Button id="btnRepeaterParent" runat="server" SkinID="btnAgroquimicos" CssClass="celdasMenusDinamicas" Text='<%# Eval("NOMBRE") %>' CommandName='<%# Eval("NOMBRE") %>' />
                 </ItemTemplate>
            </asp:Repeater>

当 commandEvent 被触发时,我使用 commandName 和 uniqueId 建立了一个会话:

protected void repMenuParent_OnItemCommand(object source, RepeaterCommandEventArgs e)
{
    Session.Add("nombreSubMenuAgro", e.CommandName);

    String id = e.Item.UniqueID;
    Session.Add("souceId", id);        
}

问题如下:我需要更改用户点击的中继器内按钮的前景色。在开始时,所有按钮(中继器中的项目)都是灰色的。当点击某人时,我需要将其前景色更改为红色(如面包屑)。

我试图在itemDataBound 中找到该项目并比较 uniqueId 但它不起作用,我不知道如何找到单击以更改前景色的特定项目:

protected void repMenuParent_OnItemDataBound(object Sender, RepeaterItemEventArgs e)
{        
    String sessionID = "";
    Button btnRepeater;

    if ((e.Item.ItemType == ListItemType.Item) ||  (e.Item.ItemType == ListItemType.AlternatingItem))
    {
        btnRepeater = (Button)e.Item.FindControl("btnRepeaterParent");

        if (btnRepeater != null)
        {                
            if(btnRepeater.UniqueID.Equals(sessionID))
            {
                btnRepeater.ForeColor = System.Drawing.Color.FromArgb(69, 187, 32);
            }
        }
     }            
}

我已经阅读了许多关于中继器的博客主题并找到了它的项目,但找不到任何可以解决我的问题的帖子,请帮帮我吗?

【问题讨论】:

  • 您是否在 _OnItemCommand 事件被触发后重新绑定网格?
  • @user2044081 您在哪里检索 Session["souceId"] 值?
  • 不,没有重新绑定,而是转发器而不是网格。
  • Marcus,我省略了代码的和平。在 itemdataBOund 中,将 Session["sourceId"] 分配给 de var sessionID。像这样:SessionID = Session["souceId"].ToString();

标签: c# asp.net repeater


【解决方案1】:

我认为这个问题最好用 Javascript 来解决,尽管我确实为你提供了服务器端解决方案。

命令事件对象有一个名为 CommandSource 的属性。这使您可以访问触发命令事件的按钮。

protected void repMenuParent_OnItemCommand(object source, RepeaterCommandEventArgs e)
{
    Button btnRepeater=e.CommandSource;
    btnRepeater.ForeColor = Drawing.Color.Red;
}

【讨论】:

  • 这个解决方案我以前试过,但是如果用户点击不同的按钮,他点击的所有按钮都保持红色。
【解决方案2】:

如果您维护按下按钮的 ID 列表,则可以执行以下操作:

首先,将中继器中的按钮更改为使用CommandArgument CommandName。像这样:

<asp:Button id="btnRepeaterParent" ... CommandArgument='<%# Eval("NOMBRE") %>' />

创建用于访问 ID 列表的页面级属性。像这样:

public List<string> ClickedIds
{
   get
   {
      if(Session("ClickedIds") == null)
         Session("ClickedIds") = new List<string>();

      return (List<string>)Session("ClickedIds");
   }
}

当你的中继器中的一个按钮被点击时,像这样处理它:

protected void repMenuParent_OnItemCommand(object source, RepeaterCommandEventArgs e)
{
    this.ClickedIds.Add(e.CommandArgument);       
}

现在,当您在代码隐藏中绑定中继器时,请像这样检查每个按钮:

protected void repMenuParent_OnItemDataBound(object Sender, RepeaterItemEventArgs e)
{        

   if ((e.Item.ItemType == ListItemType.Item) ||  (e.Item.ItemType == ListItemType.AlternatingItem))
   {
      //retrieve the id of the data item
      //I am not sure of your data item structure, but it will be something to this effect
      int nombre = e.Item.DataItem("NOMBRE");

      if(this.ClickedIds.Contains(nombre))
      {
         ((Button)e.Item.FindControl("btnRepeaterParent")).ForeColor = System.Drawing.Color.FromArgb(69, 187, 32);
      }
   }            
}

免责声明: 我没有测试过这段代码。

【讨论】:

  • 测试此代码时,我收到此错误:'System.String' to type 'System.Collections.Generic.List`1[System.String]
  • 稍微改变一下列表,因为命令是aString,“NAME”实际上是一个字符串id
  • @dashaRe:我已经编辑了我的答案以反映 ID 是字符串。这有帮助吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-11-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-05
  • 2020-01-02
  • 1970-01-01
相关资源
最近更新 更多