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:
This could help you out:
The first one works because, you have specified that the onClick handler is associated to the __doPostBack function which is in javascript. The 'javascript:' just specifies that the function is written in javascript and this should be used only when the script differs from that specified in the meta tag.
The second one does not work because you have specified the html scripts for the javascript handler. When this goes to the javascript interpreter, it wouldnt be able to understand the tags and hence wouldnt work.
__doPostBack is not recommended for all the cases as mentioned here . But if you have no other choice of creating a postback, then you can use it. This link will be able to give you more info.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.