Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > GridView - RowCreated EventHandler doesn't work if page IsPostBack

Reply
Thread Tools

GridView - RowCreated EventHandler doesn't work if page IsPostBack

 
 
Paul
Guest
Posts: n/a
 
      04-28-2007
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

 
Reply With Quote
 
 
 
 
Eliyahu Goldin
Guest
Posts: n/a
 
      04-29-2007
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.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
>



 
Reply With Quote
 
 
 
 
Paul
Guest
Posts: n/a
 
      04-29-2007
Eliyahu,

Works beautifully. Thanks,

Paul

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why Do IsPostBack and Master.IsPostBack Always Fire Twice? Joey ASP .Net 2 10-09-2008 06:17 AM
GridView RowCreated Handler Issues bigbrorpi@gmail.com ASP .Net 2 10-25-2007 04:05 PM
Intermittent IsPostBack/EventHandler problem ourwebstop@gmail.com ASP .Net Web Controls 0 03-30-2007 06:25 PM
Gridview RowCreated - way to change just first row command text? K B ASP .Net 1 06-06-2006 03:56 PM
InputAttributes in RowCreated (gridview) Pipo ASP .Net 1 04-01-2006 05:28 PM



Advertisments