user776676
user776676

Reputation: 4385

ASP.NET Gridview: How to access the Select button?

I am getting there! Need a tiny bit of extra help. My gridview has 2 columns. One is Party (text) and one is a Select column with a Select button in every row. This is what I want: Change the color of the Select button to Red if the Party is Republican. Otherwise set it to Blue. Below is the code I have. Please help me fill in the missing code.

protected void GridView1_RowDataBound1(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.Cells[0].Text == "Republican")
                 {
                  // WHAT SHOULD I DO HERE???
                 }
        }


    }

Upvotes: 1

Views: 601

Answers (1)

Vespassassina
Vespassassina

Reputation: 515

Instead of setting the color in the codebehind you could just set the CSS Class of your button in the aspx file, using gridview's template fields:

<templateField>
    <itemtemplate>
        <asp:button runat="server" id="selectButton" CSSClass='<%# Eval("PARTY") %>' />
    </itemTemplate>
</templateField>

and then defining the CSS Class in your stylesheet.

.republican{background-color:#F00;}

Upvotes: 2

Related Questions