[英]Calling code-behind with Javascript clarification
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. 我正在尝试使gridview中的一行可点击,以便它引起回发,以便随后可以执行代码隐藏。
I have this in my GridView's RowDataBound event handler. 我在GridView的RowDataBound事件处理程序中拥有此功能。 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. 第一个有效的原因是,您已指定onClick处理程序与javascript中的__doPostBack函数相关联。 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. “ javascript:”仅指定该函数是用javascript编写的,并且仅当脚本与meta标记中指定的脚本不同时才应使用此函数。
The second one does not work because you have specified the html scripts for the javascript handler. 第二个不起作用,因为您已经为javascript处理程序指定了html脚本。 When this goes to the javascript interpreter, it wouldnt be able to understand the tags and hence wouldnt work. 当到达javascript解释器时,它将无法理解标签,因此将无法工作。
__doPostBack is not recommended for all the cases as mentioned here . 不建议将__doPostBack用于此处提到的所有情况。 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. 该链接将为您提供更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.