RowDataBound trouble

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?
 
J

John

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 :)
 
J

Jeff

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?
 
J

John

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.
 
J

Jeff

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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top