RowDataBound trouble

  • Thread starter Thread starter Jeff
  • Start date Start date
J

Jeff

hi

asp.net 2.0

I'm having trouble with this RowDataBound method. the problem is when I
click on the edit buttton on a row and wants to edit the row, then the
textbox isn't filled with data... but if I click on the edit button on a
different row then the textbox gets its value... so it looks like this code
only works on every second row I click on.. But I want it to work on every
row...

Here is the entire method:
protected void gvTest_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Category category = (Category)e.Row.DataItem;
if ((e.Row.RowState == DataControlRowState.Normal) || (e.Row.RowState ==
DataControlRowState.Alternate))
{
Literal literal = (Literal)e.Row.FindControl("litTest");
literal.Text = car.Name;
}
else if (e.Row.RowState == DataControlRowState.Edit)
{
TextBox textbox = (TextBox)e.Row.FindControl("txtTest");
textbox .Text = car.Name;
}

}
}

any ideas what I do wrong?
 
try this:



protected void gvTest_RowDataBound(object sender, GridViewRowEventArgs
e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Category category = (Category)e.Row.DataItem;
if ((e.Row.RowState == DataControlRowState.Normal) ||
(e.Row.RowState ==
DataControlRowState.Alternate))
{
Literal literal = (Literal)e.Row.FindControl("litTest");
literal.Text = car.Name;
}

if (e.Row.RowState == DataControlRowState.Edit)
{
TextBox textbox = (TextBox)e.Row.FindControl("txtTest");
textbox .Text = car.Name;
}

}
}


note the removal of the else keyword :)
 
hi John,

thaks for your suggestion, but it didn't fix the problem. It seems like
problem occur on rows of state Alernate, I've been testing and sees that my
code works on row with state normal..

I've even set a breakpoint inside the last if-block if (e.Row.RowState ==
DataControlRowState.Edit)
but that breakpoint isn't triggered when row is of state alternate

any suggestions?
 
The RowState is a combination of flags try:

if (e.Row.RowState & DataControlRowState.Edit ==
DataControlRowState.Edit)



does the second if statement hit then?


Greets,
John.
 
thank you, it solved the problem, although I had to modify the if statement
a bit to get it to work:
if ((e.Row.RowState & DataControlRowState.Edit) > 0)
 
Back
Top