I am having to create a GridView 100% in C# CodeBehind. I have it selecting a row and posting back using this code:
void dataGrid_ItemCreated(object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.AlternatingItem ||
e.Item.ItemType == ListItemType.Item)
{
e.Item.Attributes.Add("onmouseover",
"this.style.backgroundColor='beige';this.style.cursor='pointer'");
e.Item.Attributes.Add("onmouseout",
"this.style.backgroundColor='#FFFFFF';");
e.Item.Attributes.Add("onclick", "javascript:__doPostBack" +
"('_ctl0$DataGrid1$_ctl" +
((Convert.ToInt32(e.Item.ItemIndex.ToString())) + 2) +
"$_ctl0','')");
}
}
This does post back but then how do I get the ID of the row the user clicked on?
void dataGrid_ItemCreated(object sender, DataGridItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.Item)
{
var item = e.Item.DataItem; // <- entity object that's bound, like person
var itemIndex = e.Item.ItemIndex; // <- index
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With