Home > Archive > ASP .NET > April 2007 > GridView - RowCreated EventHandler doesn't work if page IsPostBack
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
GridView - RowCreated EventHandler doesn't work if page IsPostBack
|
|
|
| Hello All,
I am trying to use the following RowCreated Event Handler to make the
BackColor of a cell different if it has a particular value.
protected void GridView1_RowCreated(object sender,
GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
object cellValue = DataBinder.Eval(e.Row.DataItem,
"Primary Diagnosis");
if(cellValue != null)
{
if (cellValue.Equals("Unknown or Service Recipient
Declined"))
{
e.Row.Cells[1].BackColor = Color.Yellow;
}
}
}
}
The page works as expected the first time it is loaded. But if it is a
PostBack cellValue is null, so the code to color the cells BackColor
does not run. If I take out condition "if(cellValue != null) I get an
error message. How can I use the RowCreated Event handler to control
the BackColor of cells even when it is a PostBack.
Help is always appreciated. Thanks,
Paul
| |
| Eliyahu Goldin 2007-04-29, 4:17 am |
| Paul,
Two things:
1. Use RowDataBound event instead of RowCreated.
2. There is no need to call DataBinder.Eval. You can use e.Row.DataItem with
typecasting. If your datasource is a datatable, you can write something like
object cellValue = (DataRowView)e.Row.DataItem["Primary Diagnosis"];
--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net
"Paul" <paulod.toronto@gmail.com> wrote in message
news:1177803394.982814.222880@n76g2000hsh.googlegroups.com...
> Hello All,
>
> I am trying to use the following RowCreated Event Handler to make the
> BackColor of a cell different if it has a particular value.
>
> protected void GridView1_RowCreated(object sender,
> GridViewRowEventArgs e)
> {
> if (e.Row.RowType == DataControlRowType.DataRow)
> {
>
> object cellValue = DataBinder.Eval(e.Row.DataItem,
> "Primary Diagnosis");
> if(cellValue != null)
> {
>
> if (cellValue.Equals("Unknown or Service Recipient
> Declined"))
> {
> e.Row.Cells[1].BackColor = Color.Yellow;
> }
>
> }
> }
> }
>
> The page works as expected the first time it is loaded. But if it is a
> PostBack cellValue is null, so the code to color the cells BackColor
> does not run. If I take out condition "if(cellValue != null) I get an
> error message. How can I use the RowCreated Event handler to control
> the BackColor of cells even when it is a PostBack.
>
> Help is always appreciated. Thanks,
>
> Paul
>
| |
|
| Eliyahu,
Works beautifully. Thanks,
Paul
|
|
|
|
|