ItemCreate event handler changes wrong DataGridItem

C

Chris Smith

I am changing the EditText in a EditCommandColumn in a DataGrid in an
ItemCreate event handler . The only problem I am having is that instead of
changing the text in the current row it changes the next row.

I watch the value in the debugger, and I see it change at what appears to be
the right time.

The EnableViewState is set to True.

Any suggestions or thoughts appreciated!



I read a value out of the current DataGridItem DataItem (a person's name),
and use it to replace the EditCommandColumn's EditText field.:

private void hhItemCreated(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{
string HouseholdName;

DataGrid dg;
dg = (DataGrid)sender;

EditCommandColumn ecc;
ecc = (EditCommandColumn)dg.Columns[0];

Troop.FunWithXML.TroopDB.HouseholdRow r =
(Troop.FunWithXML.TroopDB.HouseholdRow)e.Item.DataItem;
if(r == null) return;

HouseholdName =
((Troop.FunWithXML.TroopDB.HouseholdRow)e.Item.DataItem)["HHName"].ToString(
);
ecc.EditText = HouseholdName;
}
}
 
G

Giorgio Parmeggiani

Hi
changing the text in the current row it changes the next row.

I think your example doesn't work because at the ItemCreated event the page
has already created the LinkButton of the column using the "EditText"
property of the EditCommandColumn.

Therefore you must modify directly the "Text" property of the LinkButton.

here an example:

private void hhItemCreated(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{
string HouseholdName;

Troop.FunWithXML.TroopDB.HouseholdRow r =
(Troop.FunWithXML.TroopDB.HouseholdRow)e.Item.DataItem;
if(r == null) return;

HouseholdName =
((Troop.FunWithXML.TroopDB.HouseholdRow)e.Item.DataItem)["HHName"].ToString(
);
((LinkButton)e.Item.Cells[0].Controls[0]).Text = HouseholdName;

}


Hi
Giorgio
 

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,755
Messages
2,569,536
Members
45,019
Latest member
RoxannaSta

Latest Threads

Top