Gridview Rowcommand / Rowcreated question

B

benji

Hi,

I've got a gridview that I bind programmatically to a view of a typed
dataset. One column has a command button, and in the RowCreated event, I test
to see if it's a DataRow, and if it is, I grab the button and attach a value:

{
Response.Write("<br/>" + e.Row.Cells[1].Controls[0].ToString() +
"<br/>");
// Retrieve the LinkButton control from the first column.
Button addButton = (Button)e.Row.Cells[1].Controls[0];

// Set the LinkButton's CommandArgument property
addButton.CommandArgument =
((DataRowView)e.Row.DataItem)["RowID"].ToString();

}

}

This works fine when the page loads (and I see the response.write). When I
click the button for a row, however, I get a null reference exception, not in
the RowCommand handler but in the RowCreated handler (likely because my data
source is not persisted, and I'm not refreshing my dataset from the database
or grabbing it from viewstate on postback - i didn't expect RowCreated to
fire again).

As a test, I replaced the line

addButton.CommandArgument = ((DataRowView)e.Row.DataItem)["RowID"].ToString();
with:
addButton.CommandArgument = Convert.ToString(1);

I expected to get around the null reference error, which I did, but now, I
didn't get the response.write! This seems to indicate that with this change,
it didn't even fire the RowCreated event when it was doing the RowCommand,
which it seemed to when I was getting the null reference exception.

So: when a rowcommand is fired, is a rowcreated event fired also? Is this
based on some condition? (I don't do any rebinding on postback)

Thanks for any help for this foggy situation.

-Ben
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top