更改Gridview的Linkbutton的背景颜色

时间:2015-08-07 07:54:23

标签: c# asp.net gridview

我想使用C#ASP.NET更改LinkButton事件中的OnRowDataBound背景颜色。我在下面解释我的代码。

的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";
        }
    }
}

我想在上面的LinkButton语句中将if颜色更改为红色。如果文本为R,拒绝按钮将为红色。我怎么能这样做?

2 个答案:

答案 0 :(得分:1)

您只需找到链接按钮并更新其颜色和文字,如下所示: -

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

请注意,此处我认为您的链接按钮的ID为myLinkButton。您需要相应地更新它。

答案 1 :(得分:0)

您可以投射LinkBut​​ton并访问其所有属性:

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