Set button attribute inside Gridview

问题内容:

I have a button inside a Gridview. What I want to do is when the button is clicked simply change the button color.
However, after publishing the paged and clicking the button nothing happens.
Am I missing a step.

Here is my .aspx code

<asp:GridView   id="gvStatus" runat="server" AutoGenerateColumns="false" BorderWidth="1px" BackColor="White" CellPadding="3" CellSpacing="2" BorderStyle="Solid" BorderColor="Black" GridLines="Both" Pager="10" OnRowDataBound="setcolor" OnRowCommand="setsingle" >
        <Columns>
                <asp:TemplateField HeaderText="1"  Visible="true" HeaderStyle-CssClass= "hdrBase" ItemStyle-CssClass="GridBase">
                    <ItemTemplate>
                        <asp:Button id="btnDOne" Width="35px"  Height ="25px" runat="server" Text='<%#(Eval("dateone", "{0:ddd}"))%>' CommandName="GetData" CommandArgument="<%#((GridViewRow) Container).RowIndex %>" ></asp:Button>
                    </ItemTemplate>
                </asp:TemplateField>        
        </Columns>
    </asp:GridView>

And here is the c#

        protected void setsingle(object sender, GridViewCommandEventArgs e)
    {   
        if (e.CommandName == "GetData")
            { 
                //Get row index
                int rowindex  = Convert.ToInt32(e.CommandArgument);
                ////Get Row
                GridViewRow gvr = gvStatus.Rows[rowindex];
                //Find the button
                Button DayButton = gvr.FindControl("btnDOne") as Button;    
                //Set the color of the button
                DayButton.BackColor = Color.Cyan;
            }

问题评论:

    
Have you set a breakpoint in setsingle?
    
button clicks trigger PostBacks there are actually lots of examples on how to add attributes to asp.net controls do a google search to see

原文地址:

https://stackoverflow.com/questions/47754374/set-button-attribute-inside-gridview

添加评论