admin管理员组文章数量:1368007
I want to pass a string value to a javascript function from code behind. As it is a the moment I get an uncaught reference error explaining that the value is not defined.
var variable= txtVariable.Value;
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "Registering", "RegisterTheUser("+variable+");", true);
Advice perhaps on the correct syntax
This is the function
function RegisterTheUser(val) {
alert(val);
}
regards
I want to pass a string value to a javascript function from code behind. As it is a the moment I get an uncaught reference error explaining that the value is not defined.
var variable= txtVariable.Value;
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "Registering", "RegisterTheUser("+variable+");", true);
Advice perhaps on the correct syntax
This is the function
function RegisterTheUser(val) {
alert(val);
}
regards
Share Improve this question edited Sep 18, 2013 at 11:28 Benoit Blanchon 14.6k5 gold badges79 silver badges91 bronze badges asked Sep 18, 2013 at 11:13 ArianuleArianule 9,06345 gold badges122 silver badges179 bronze badges2 Answers
Reset to default 6What you have posted looks fine. I've used the following without any issue:
ScriptManager.RegisterStartupScript(this, typeof(string), "Registering", String.Format("RegisterTheUser('{0}');", myVariable), true);
Can you try using the example I have posted?
The problem might be that your RegisterStartupScript
is being executed before loading the function RegisterTheUser
.
You need to sort out the order of the loading of the scripts or add some logic to call this function only after the document
is ready.
In jQuery is done like this:
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "Registering", "$(document).ready(function(){ RegisterTheUser("+variable+"); });", true);
As sbhomra pointed in his answer, if the value is a string you might need to add single qoutes. Not needed if it's a number.
本文标签: cpassing a variable to javascript function from code behindStack Overflow
版权声明:本文标题:c# - passing a variable to javascript function from code behind - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743709699a2525718.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论