简体   繁体   English

如何从 ASPX 控制事件中调用 Javascript function?

[英]How do you call a Javascript function from an ASPX control event?

How do you call a Javascript function from an ASPX control event?如何从 ASPX 控制事件中调用 Javascript function?

Specifically, I want to call the function from the SelectedIndexChanged event of a DropDownList.具体来说,我想从 DropDownList 的 SelectedIndexChanged 事件中调用 function。

I get a little nervous whenever I see this kind of question, because nine times out of ten it means the asker doesn't really understand what's going on.每次看到这种问题我都会有点紧张,因为十有八九这意味着提问者并不真正理解发生了什么。

When your SelectedIndexChanged event fires on the server, it fires as part of a full postback .当您的 SelectedIndexChanged 事件在服务器上触发时,它会作为完整回发的一部分触发。 That means that for that code to run, the entire rest of your page's load code also had to run.这意味着要运行该代码,您的页面加载代码的整个 rest 也必须运行。

More than that, the code runs as the result of a new http request from the browser.不仅如此,该代码作为来自浏览器的新 http 请求的结果运行。 As far as the browser is concerned, an entirely new page is coming back in the result.就浏览器而言,结果中将返回一个全新的页面。 The old page, and the old DOM, are discarded.旧页面和旧 DOM 被丢弃。 So at the time your SelectedIndexChanged event code is running, the javascript function you want to call doesn't even exist in the browser.因此,在您的 SelectedIndexChanged 事件代码运行时,您想要调用的 javascript function 甚至不存在于浏览器中。

So what to do instead?那么该怎么做呢? You have a few options:你有几个选择:

  • Change the page so the control doesn't post back to the server at all.更改页面,使控件根本不会回发到服务器。 Detect the change entirely in javascript at the client.在客户端完全检测 javascript 中的变化。 This is my preferred option because it avoids odd onload scripts in the browser page and it saves work for your server.这是我的首选选项,因为它避免了浏览器页面中奇怪的加载脚本,并为您的服务器节省了工作量。 The down side is that it makes your page dependent on javascript, but that's not really a big deal because if javascript is disabled this was doomed from the beginning.不利的一面是它使您的页面依赖于 javascript,但这并不是什么大问题,因为如果禁用 javascript,这从一开始就注定要失败。
  • Set your desired javascript to run onload in the SelectedIndexChanged event using the ClientScript.SetStartupScript().使用 ClientScript.SetStartupScript() 将您想要的 javascript 设置为在 SelectedIndexChanged 事件中运行 onload。
  • Apply the expected results of your javascript to the server-model of the page.将 javascript 的预期结果应用于页面的服务器模型。 This has the advantage of working even when javascript is turned off (accessibility), but at the cost of doing much more work on the server, spending more time reasoning about the logical state of your page, and possibly needing to duplicate client-side and server-side logic.这具有即使在 javascript 关闭(可访问性)时也能正常工作的优势,但代价是在服务器上做更多工作,花费更多时间推理页面的逻辑 state,并且可能需要复制客户端和服务器端逻辑。 Also, this event depends on javascript anyway: if javascript is disabled it won't fire.此外,此事件无论如何都取决于 javascript:如果 javascript 被禁用,它将不会触发。
  • Some combination of the first and third options are also possible, such that it uses javascript locally if available, but posts back to the server if not.第一个和第三个选项的某些组合也是可能的,例如它在本地使用 javascript 如果可用,但如果没有则回发到服务器。 Personally I'd like to see better, more intuitive, support for that built into ASP.Net.就我个人而言,我希望看到对 ASP.Net 中内置的更好、更直观的支持。 But again: I'm talking about the general case.但再说一遍:我说的是一般情况。 This specific event requires javascript to work at all.此特定事件需要 javascript 才能正常工作。

As Muerte said you have to just put the javascript, or a call to it on the page from the code behind.正如 Muerte 所说,您只需将 javascript 或从后面的代码调用到页面上即可。 Personally I use this:我个人使用这个:

ClientScript.RegisterClientScriptBlock("customscript", "<script>simple script here</script>")

Of you can call the function if you already have a more complex one on the page instead of the stuff I have.如果您在页面上已经有一个更复杂的而不是我拥有的东西,您可以调用 function。

You can't do it directly from an event, because ASPX control event is server side.您不能直接从事件中执行此操作,因为 ASPX 控件事件是服务器端的。

What you can do is emit a Javascript in the ASPX event which will call the JavaScript function when the page reloads.您可以做的是在 ASPX 事件中发出 Javascript,该事件将在页面重新加载时调用 JavaScript function。

For example, if in your ASPX page you have a Javascript function called "DoSomething()", in you ASPX control event, add the following:例如,如果在您的 ASPX 页面中有一个名为“DoSomething()”的 Javascript function,则在您的 ASPX 控件事件中添加以下内容:

protected void btnSubmit_Click(object sender, EventArgs e)
{
    Page.ClientScript.RegisterStartupScript(this.GetType(), "myEvent", "DoSomething()", true);
}

The last boolean parameter defines that tags are added automatically.最后一个 boolean 参数定义自动添加标签。

In the code behind, attach some markup to the server side control via its attributes collection.在后面的代码中,通过属性集合将一些标记附加到服务器端控件。 This assumes that the function is already in a client script file that is already available to the page.这假设 function 已经在页面可用的客户端脚本文件中。

MyServerDDLControl.Attributes.Add("SelectedIndexChanged", "MyClientSideFunction();");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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