在Repeater中查找并更改特定项目

本文关键字:项目 Repeater 查找 | 更新日期: 2023-09-27 17:57:53

我有以下<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);        
}

问题如下:我需要更改用户调用的中继器内botton的前景。一开始,所有按钮(中继器中的项目)都是灰色的。当有人被点击时,我需要把它的前色改为红色(就像面包屑一样)。

我试图在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);
            }
        }
     }            
}

我读了很多关于中继器的博客帖子,找到了它的项目,但找不到任何解决我问题的东西,能帮我吗?

在Repeater中查找并更改特定项目

我认为这个问题最好用Javascript来解决,尽管我有一个服务器端的解决方案。

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

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

如果您维护按下按钮的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);
      }
   }            
}

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