如何从.Net Page_Load事件调用Javascript函数 [英] How Do I Call A Javascript Function From A .Net Page_Load Event

查看:135
本文介绍了如何从.Net Page_Load事件调用Javascript函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi


i目前正在将Jquery图像滑块编码到我的网页应用程序页面上。



我有一个网站使用的具有正文标记的母版页。


此页面上的b $ b我想使用< body onload事件>但它不实用,因为我已经在主页面上添加了body标签。我想知道如果我在Asp.net页面上创建一个javascript,我能够从Page_Load事件中调用javascript吗?如果是这样,请帮助我告诉我如何。

hi
i am currently coding a Jquery image slider onto a page of my web application.

I have a master page that the site uses that has a body tag.

on this page i would like to use the <body onload event> but it is not practical as i already have the body tag being added by the master page. I would like to know if i create a javascript on the Asp.net page would i be able to call the javascript from the Page_Load event? if so please could you assist in showing me how.

推荐答案

1. Page_Load 在服务器上执行事件当页面第一次加载然后在每个页面上回发。在所有页面事件执行结束时,页面将以HTML格式发送回Web浏览器,然后Web浏览器将页面解释并显示给客户端。



2.另一方面, Javascript 代码旨在直接在浏览器上执行;



3 。在你的情况下,你可以通过使用 ClientScript.RegisterStartupScript()来动态地将Javascript链接到你的身体 onload 事件在下一个示例中:

1.Page_Load event is executed on the server when the page is loading first time and then on each page post back. At the end of all page events execution the page is send it back in HTML format to the web browser and then the web browser interpret and display the page to the client.

2.On the other side the Javascript code is designed to be executed directly on the browser;

3.In your case, you could link the Javascript dynamically with your body onload event by using ClientScript.RegisterStartupScript() like in the next example:
StringBuilder script = new StringBuilder();
script.Append("<script language=JavaScript>");
script.Append("document.body.onload=function(){alert('Test!')}</script>");
ClientScript.RegisterClientScriptBlock(this.GetType(), "Pop", script.ToString());


Page.ClientScript.RegisterStartupScript(this.GetType(),UrScriptName ,javascript:functionname(),false)



如果你使用更新面板使用

ScriptManager.RegisterStartupScript(Page,this.GetType (),UrScriptName,javascript:functionname(),false);
Page.ClientScript.RegisterStartupScript(this.GetType(),"UrScriptName","javascript:functionname()",false)

if u r using update panel use
ScriptManager.RegisterStartupScript(Page,this.GetType(), "UrScriptName","javascript:functionname()", false);


这篇关于如何从.Net Page_Load事件调用Javascript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆