简体   繁体   English

从asp.net c#页面调用客户端javascript

[英]call clientside javascript from asp.net c# page

I have ac# asp.net page and an update function which will update the database. 我有一个ac#asp.net页面和一个更新功能,它将更新数据库。 In this function I would like to call some client side javascript. 在此功能中,我想调用一些客户端javascript。 I've read a lot about registering a start up script in page_load() but this is always trigger on page load (funny that!) 我已经阅读了很多有关在page_load()中注册启动脚本的信息,但这总是在页面加载时触发(很有趣!)

How would I register then call a script inside my update function? 如何注册然后在更新函数中调用脚本? Triggered when a user clicks the "update" button. 当用户单击“更新”按钮时触发。 I have tried the following (inside my function) 我尝试了以下内容(在我的功能内)

protected void doUpdate(object sender, EventArgs e) {

    string jScript;
    jScript = "<script type=text/javascript>alert('hello');<" + "/script>";

    ClientScript.RegisterStartupScript(GetType(), "Javascript", jScript);
}

but it isn't fired. 但它没有被解雇。 Any ideas? 有任何想法吗? Many thanks. 非常感谢。

[update] [更新]

It's now working - the function looks like this 现在正在工作-函数看起来像这样

protected void doUpdate(object sender, EventArgs e) {
    ScriptManager.RegisterStartupScript(this, GetType(),"Javascript", "cleanup();",true);
}

Cleanup() is the javascript function in my HTML. Cleanup()是我HTML中的javascript函数。 Thanks for the help guys :) 谢谢你们的帮助:)

如果导致回发的控件在UpdatePanel内部,则需要使用

ScriptManager.RegisterStartupScript

You can't 'execute' client side scripts from the web server (the client knows who the server is, but not the other way around). 您不能从Web服务器“执行”客户端脚本(客户端知道服务器是谁,但反之则不知道)。

The only way to overcome this limitation is by a. 克服此限制的唯一方法是。 create a long-polling process that requests something from the server, the server doesn't complete the request till it has something to return (then client side it makes another request). 创建一个从服务器请求某些内容的长轮询过程,服务器直到有需要返回的内容时才完成请求(然后客户端发出另一个请求)。

What you are really looking for is websocket (duplex) enabled communication. 您真正要寻找的是启用了websocket(双工)的通信。 You can check out alchemy websockets or SignalR (has a pretty nice library with dynamic proxy generation). 您可以检出alchemy websockets或SignalR(有一个不错的库,具有动态代理生成)。

The reason why that 'script always works on Page_Load' is because it effectively injects your script tag into the html returned for the page requested. 该“脚本始终可在Page_Load上运行”的原因是,它可以将脚本标记有效地注入到为请求的页面返回的html中。

Your Update button is likely using the standard ASP Button behavior, meaning it is type="submit" when it is rendered. 您的“更新”按钮可能使用标准的“ ASP按钮”行为,这意味着呈现按钮时,其type="submit" Since that's the case, you can just use: 既然如此,您可以使用:

Page.ClientScript.RegisterOnSubmitStatement

Keep in mind that will register a script for every postback, not just the Update button. 请记住,这将为每个回发注册一个脚本,而不仅仅是“更新”按钮。 So, if you only want some javascript run on clicking Update, you would need to check if the EventTarget is UpdateButton.ClientID . 因此,如果只希望在单击“更新”时运行某些JavaScript,则需要检查EventTarget是否为UpdateButton.ClientID Also, RegisterOnSubmitStatement always adds the <script> tags, so don't include those in the javascript statement. 另外, RegisterOnSubmitStatement始终添加<script>标记,因此不要在javascript语句中包括这些标记。

An even easier solution, the ASP Button itself also has an OnClientClick property. 一个甚至更简单的解决方案,ASP Button本身也具有OnClientClick属性。 This will run client-side code (javascript) when the button is clicked in the browser. 当在浏览器中单击按钮时,它将运行客户端代码(javascript)。

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

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