简体   繁体   中英

Find and change a specific item in Repeater

I have the following <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>

When the commandEvent is triggered i set up a session with the commandName and the uniqueId:

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

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

The issue is the following: I need change the forecolor of the botton inside the repeater that is cicked by the user. In the beginnig all the buttons (items in the repeater) are gray. When someone is clicked I need to change the forecolor of it to red (like a bread crumb).

I've trying to find the item in the itemDataBound and compare the uniqueId but it doesn't work, i don't know how to find the specific item that is clicked to change the forecolor:

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);
            }
        }
     }            
}

I've read many blog's threads about repeater and find it's items, but can´t find any that solved my issue, Can help me please?

I think this problem is best solved with Javascript though I do have a server side solution for you.

The command event object has a property called CommandSource. This gives you access to the button that triggered the command event.

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

If you maintain a List of the IDs of the buttons pressed, then you can do the following:

First, change your button in the repeater to use CommandArgument not CommandName . Like so:

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

Create a page level property for accessing the list of IDs. Like so:

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

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

When one of your buttons in your repeater is clicked, handle it like so:

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

Now, when you are binding the repeater in code-behind, check each button like so:

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);
      }
   }            
}

DISCLAIMER: I have not tested this code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM