I want my GridView Rows to fire the SelectedIndexChanged event when I click on them.
I tried the following in code behind:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowIndex != -1)
{
e.Row.Attributes.Add("onMouseOver", "Highlight(this)");
e.Row.Attributes.Add("onMouseOut", "UnHighlight(this)");
e.Row.Attributes.Add("onClick", String.Format("javascript:__doPostBack('GridView1','Select${0}')", e.Row.RowIndex));
}
}
Postback happens when I click on a row, but it doesn’t fires SelectedIndexChanged. In the Page_Load I get the arguments from the request:
if (IsPostBack)
{
object obTarget = this.Request.Form["__EVENTTARGET"]; //GridView1
object obArg = this.Request.Form["__EVENTARGUMENT"]; //Select$4
}
I think that SelectedIndexChanged would have to be raised, or do i have a misunderstanding on this?
You are probably rebinding the GridView to its DataSource on postback. You should do that only
if(!IsPostBack)(with enabled ViewState), otherwise events aren’t triggered.You should also register event handler on every postback but
RowDataBoundis only called on databinding. So useRowCreatedinstead.It’s also better to use
Page.GetPostBackClientHyperlinkinstead of hardcoding the javascript function.