I'm trying to make a row in a gridview clickable, so that it causes a postback so that I can then execute code-behind.
I have this in my GridView's RowDataBound event handler. This WORKS:
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes["onmouseover"] = "this.style.cursor='pointer';this.style.textDecoration='underline';";
e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';";
e.Row.Attributes["onclick"] = "javascript:__doPostBack('PostBackFromItemWindow', '');";
}
But this DOESN'T WORK:
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes["onmouseover"] = "this.style.cursor='pointer';this.style.textDecoration='underline';";
e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';";
e.Row.Attributes["onclick"] = "<script type='text/javascript'>__doPostBack('PostBackFromItemWindow', '');</script>";
}
Questions:
- Why does the first work, but the second doesn't?
- In trying to accomplish this task (call code-behind from javascript), are there any alternative methods of doing this? I did some reading and came across WebMethods(), but ultimately didn't like the fact that they need to be static in order to work. The above actually gives me exactly the functionality I need, I just want to make sure that it's an acceptable way of doing it (i.e. it's not deprecated or something), and that I'm not inevitably causing myself trouble later thanks to some unforeseen mistake at this point.