user776676
user776676

Reputation: 4385

ASP.NET: GridView Select Button: How to change the button's properties programmatically?

I have a gridview with a Select button in every row. How do I change the properties of this button programmatically? For example if the Party value of the row is Republican, I want the Select button to be Red; otherwise I want it to be Blue.

Thanks for any info.

Upvotes: 2

Views: 1787

Answers (2)

Adam Tuliper
Adam Tuliper

Reputation: 30152

In your code (assuming server side) you need to get a reference to that row and then call FindControl passing in your buttons name. Then you can set whatever properties on it (css class, color, etc)

See this posting for details:

Accessing the different controls inside a GridView control

Upvotes: 2

Akram Shahda
Akram Shahda

Reputation: 14781

Try using Expressions:

<asp:Button ... BackColor=<%# Eval("Party") = "Republican" ? "Red" : "Blue" %> ... />

Here, the BackColor property will take the appropriate value based on the Party value whenever a RowDataBound event gets fired ..

Upvotes: 3

Related Questions