简体   繁体   English

用Java澄清调用后台代码

[英]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: 问题:

  1. Why does the first work, but the second doesn't? 为什么第一个有效,而第二个无效?
  2. In trying to accomplish this task (call code-behind from javascript), are there any alternative methods of doing this? 在尝试完成此任务(从javascript调用代码后面)时,是否有其他替代方法可以做到这一点? 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. 我读了一些书,偶然发现了WebMethods(),但最终不喜欢它们必须静态才能工作的事实。 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 (ie it's not deprecated or something), and that I'm not inevitably causing myself trouble later thanks to some unforeseen mistake at this point. 上面的内容实际上为我提供了我需要的功能,我只想确保它是一种可以接受的方式(即不被弃用或其他),并且由于某些意外错误,我也不会不可避免地给自己造成麻烦这一点。

This could help you out: 这可以帮助您:

  1. 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解释器时,它将无法理解标签,因此将无法工作。

  2. __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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM