如何从.cs页调用javascript函数 [英] How to call a javascript function from .cs page

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

问题描述



我的.aspx页中有一个javascriipt函数.如何从.cs页调用此函数,以便该函数接受在页面加载时从.cs页发送的参数?请在这方面帮助我.

预先感谢.

解决方案



您可以使用以下函数从服务器端调用javascript函数.

已更新:
您的javascript函数:

函数初始化(文件名){ var  elem = document.getElementById("  aaa"); elem.filename = ' "+ fileName +"'; } 


您的aspx.cs页面:

 字符串 fileName = " ;
ScriptManager.RegisterStartupScript( .Page, .GetType(),  ScriptRegistration" 初始化('" + fileName +  ');"投票",如果这是正确的答案,请提供"接受答案".:rose:

谢谢,
Imdadhusen


您无法从.cs页面调用它,但是您可以将引用传递给c#代码,然后执行该代码并传递回JS脚本.

从任何事件(例如按钮单击或页面加载或任何其他事件)中调用以下代码段

字符串filename =分配值";

 sb = new System.Text.StringBuilder();
        sb.Append(@"脚本 语言  ='  javascript' > );
        sb.Append(@"Initialisation('" +文件名+');");
        sb.Append(@"/script  


Hi,

I have a javascriipt function in my .aspx page. How can I call this function from .cs page, so as that the function accepts a parameter that I send it from .cs page on page load? Please help me in this regard.

Thanks in advance.

解决方案

Hi,

You can call javascript function from server side using following function.

Updated:
your javascript function:

function Initialisation(fileName) { var elem = document.getElementById("aaa"); elem.filename = '"+fileName+"'; }


your aspx.cs page:

string fileName = "test.aspx";
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "ScriptRegistration", "Initialisation('"+ fileName +"');", true);





Please do let me know, if you have any doubt.

Please provide "Vote" if this would be helpful, and make "Accept Answer" if this would be correct answer.:rose:

Thanks,
Imdadhusen


You can''t call it from the .cs page, but you can pass the reference to the c# code which can then execute and pass back to the JS script.


Call below code snipet from any event like button click or page load or any other event

string filename="assign value";

 System.Text.StringBuilder sb = new System.Text.StringBuilder();
        sb.Append(@"<script language='javascript'>");
        sb.Append(@"Initialisation('" + filename + "');");
        sb.Append(@"</script>");
        System.Web.UI.ScriptManager.RegisterStartupScript(this, this.GetType(), "JCall1", sb.ToString(), false);


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

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