简体   繁体   中英

Change backColor of Repeater Button onClick

I have Repeater buttons that i want to change their backcolor on click. The problem is that I dont know how to identify which button is clicked and I cant reach it through the onclick function. Just to make it clear - I want to change the button that has been clicked. It should be like a "selected" button.

THOSE ARE THE BUTTONS

<asp:Repeater ID="rptFillButtonCategory" runat="server">
    <ItemTemplate>
        <asp:Button ID="FillButton" runat="server" Width="100%" OnClick="ButtonSelectionFill" 
             CommandArgument='<%#DataBinder.Eval(Container.DataItem, "Id") %>' 
             Text='<%#DataBinder.Eval(Container.DataItem, "Name") + " for next version" %>'/>
    </ItemTemplate>
</asp:Repeater>

This is the on click function

public void ButtonSelectionFill(object o, EventArgs e)
{
    Button btn = ((Button)o);
    btn.BackColor = System.Drawing.Color.Red;
    DropCategory.SelectedValue = Convert.ToInt16(((Button)o).CommandArgument.ToString());
}

Thank you for the help.

This worked:

<asp:Repeater ID="Repeater1" runat="server" DataSourceID="ObjectDataSource1">
    <ItemTemplate>
        <asp:Button ID="Button1" runat="server" 
            Text='<%#DataBinder.Eval(Container.DataItem, "Id") %>' OnClick="Button1_Click" />
    </ItemTemplate>
</asp:Repeater>

protected void Button1_Click(object sender, EventArgs e)
{
    Button b = (Button)sender;
    b.BackColor = System.Drawing.Color.Red;
}

在此处输入图片说明

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