简体   繁体   中英

Change background color of Linkbutton of Gridview

I want to change the LinkButton background color inside the OnRowDataBound event using C# ASP.NET. I am explaining my code below.

index.aspx:

<asp:GridView ID="comnt_Gridview" runat="server" AutoGenerateColumns="false" Width="100%" CssClass="table table-striped table-bordered margin-top-zero" OnRowDataBound="comnt_Gridview_RowDataBound"  DataKeyNames="Bnr_ID"  >
    <Columns>
        <asp:TemplateField HeaderText="Sl. No" ItemStyle-CssClass="col-md-1 col-sm-1">
            <ItemTemplate>
                <%# Container.DataItemIndex + 1 %>
            </ItemTemplate>

            <ItemStyle CssClass="col-md-1 col-sm-1"></ItemStyle>
        </asp:TemplateField>

        <asp:TemplateField HeaderText="Health ID" ItemStyle-CssClass="col-md-1 col-sm-1" ItemStyle-HorizontalAlign="Center">
            <ItemTemplate>
                <asp:Label ID="healthid" runat="server" Text='<%#Eval("Health_ID") %>'></asp:Label>
            </ItemTemplate>

            <ItemStyle HorizontalAlign="Center" CssClass="col-md-1 col-sm-1"></ItemStyle>
        </asp:TemplateField>

        <asp:TemplateField HeaderText="Name" ItemStyle-CssClass="col-md-1 col-sm-1" ItemStyle-HorizontalAlign="Center">
            <ItemTemplate>
                <asp:Label ID="Name" runat="server" Text='<%#Eval("Health_Comment_Name") %>'></asp:Label>
            </ItemTemplate>

            <ItemStyle HorizontalAlign="Center" CssClass="col-md-1 col-sm-1"></ItemStyle>
        </asp:TemplateField>

        <asp:TemplateField HeaderText="Email" ItemStyle-CssClass="col-md-2 col-sm-2" ItemStyle-HorizontalAlign="Center">
            <ItemTemplate>
                <asp:Label ID="Email" runat="server" Text='<%#Eval("Health_comment_Email") %>'></asp:Label>
            </ItemTemplate>

            <ItemStyle HorizontalAlign="Center" CssClass="col-md-2 col-sm-2"></ItemStyle>
        </asp:TemplateField>

        <asp:TemplateField HeaderText="Web Url" ItemStyle-CssClass="col-md-2 col-sm-2" ItemStyle-HorizontalAlign="Center">
            <ItemTemplate>
                <asp:Label ID="Url" runat="server" Text='<%#Eval("Health_Comment_Website") %>'></asp:Label>
            </ItemTemplate>

            <ItemStyle HorizontalAlign="Center" CssClass="col-md-2 col-sm-2"></ItemStyle>
        </asp:TemplateField>

        <asp:TemplateField HeaderText="Comments" ItemStyle-CssClass="col-md-3 col-sm-3" ItemStyle-HorizontalAlign="Center">
            <ItemTemplate>
                <asp:Label ID="lblRemark" runat="server"
                    Text='<%# Eval("Health_Comment_Message").ToString().Length > 100? (Eval("Health_Comment_Message") as string).Substring(0,100) + " ..." : Eval("Health_Comment_Message")  %>'
                    ToolTip='<%# Eval("Health_Comment_Message") %> '> </asp:Label>
            </ItemTemplate>

            <ItemStyle HorizontalAlign="Center" CssClass="col-md-3 col-sm-3"></ItemStyle>
        </asp:TemplateField>

        <asp:TemplateField HeaderText="Status" ItemStyle-CssClass="col-md-1 col-sm-1" ItemStyle-HorizontalAlign="Center">
            <ItemTemplate>
                <asp:Label ID="Url" runat="server" Text='<%#Eval("Health_Comment_Status") %>'></asp:Label>
            </ItemTemplate>

            <ItemStyle HorizontalAlign="Center" CssClass="col-md-1 col-sm-1"></ItemStyle>
        </asp:TemplateField>

        <asp:TemplateField HeaderText="Action" ItemStyle-CssClass="col-md-2 col-sm-2" >
            <ItemTemplate>
                <div class="pull-left rpaddingdivsmall1"><a href="javascript:void(0)" data-toggle="tooltip" title="" class="btn btn-xs btn-success" data-original-title="View" onClick="doctordetailsshowdiv()"><i class="fa fa-eye"></i></a> / </div>
                    <div class="profile_right_side_eidt pull-left">
                        <div class="btn-group btn-toggle"> 
                            <asp:LinkButton ID="accept" CssClass="btn btn-xs btn-inactive" CommandName="Accept" runat="server" style="padding:1px 2px;" Text="Accept" OnClick="accept_click"></asp:LinkButton>
                            <asp:LinkButton ID="reject" runat="server" Text="Reject" CommandName="Reject" 
                                CssClass="btn btn-xs btn-success active"  style="padding:1px 2px;" 
                                onclick="reject_Click" ></asp:LinkButton>
                        </div>
                    </div>
                <div class="clearfix"></div>
            </ItemTemplate>

            <ItemStyle CssClass="col-md-2 col-sm-2"></ItemStyle>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

index.aspx.cs:

protected void comnt_Gridview_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        if (e.Row.Cells[6].Text=="R")
        {
            e.Row.Cells[6].Text = "Rejected";
        }
    }
}

I want to change the LinkButton color to red inside the above if statement. If text will be R, the reject button will red. How can I do this?

You can simply find your link button and update its color and text like this:-

protected void comnt_Gridview_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        if (e.Row.Cells[6].Text=="R")
        {
             LinkButton myLinkButton = (LinkButton)e.Row.FindControl("myLinkButton");
             myLinkButton.Text = "Rejected";
             myLinkButton.ForeColor = System.Drawing.Red;
         }
     }
}

Please note here I have considered your Link button's id to be myLinkButton . You need to update that accordingly.

You can cast LinkButton and access all of its properties:

if (e.Row.Cells[6].Text=="R")
{
    //cast LinkButton
    LinkButton linkBtn = (LinkButton)e.Row.FindControl("reject") as LinkButton;
    linkBtn.BackColor = Color.Red;

    linkBtn.Text = "Rejected";
}

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