In my application I need to pass a string value from the server side(.aspx.cs) to a function in the client side (.aspx) page.
在我的应用程序中,我需要将一个字符串值从服务器端(.aspx.cs)传递到客户端(.aspx)页面中的函数。
Can anyone help me by providing ideas and sample code if possible?
任何人都可以通过提供想法和示例代码来帮助我吗?
3 个解决方案
#1
9
If you mean that you need to pass the value to a javascript function, what you can do is something like this...
如果你的意思是你需要将值传递给javascript函数,你可以做的就是这样......
In the .aspx file:
在.aspx文件中:
<script language="javascript" type="text/javascript">
var stringValue = '<%=GetStringValue();%>';
// For example:
alert(stringValue);
</sctipt>
In the .aspx.cs file:
在.aspx.cs文件中:
private string GetStringValue()
{
return "A string value";
}
This would then show a javascript messagebox saying "A string value" when the page loads
然后,这将在页面加载时显示一个javascript消息框,说“字符串值”
#2
1
You can either generate the script on the server side and embed the value in it:
您可以在服务器端生成脚本并在其中嵌入值:
StringBuilder sb = new StringBuilder();
sb.Append("<script type=\"text/javascript\"");
sb.Append("var someFunc = function(){");
sb.AppendFormat("alert('{0}');", importantServerSideValue);
sb.Append("};");
sb.Append("</script>");
Page.ClientScript.RegisterClientScriptBlock("genScript", sb.ToString());
Or place the value in a hidden form element on the page and access it from the client side Javascript.
或者将值放在页面上的隐藏表单元素中,并从客户端Javascript访问它。
<!-- In the Markup -->
<asp:HtmlInputHidden id="hiddenField" runat="server" />
// And in the code-behind
hiddenField.Value = importantServerSideValue;
#3
0
When I had to pass a variable value from PHP to JS, I used php to echo out the js with a variable set to the php variable's value. e.g.
当我必须将变量值从PHP传递给JS时,我使用php来回显js,并将变量设置为php变量的值。例如
//php code
echo 'var number = ' . $variable . ';';
#1
9
If you mean that you need to pass the value to a javascript function, what you can do is something like this...
如果你的意思是你需要将值传递给javascript函数,你可以做的就是这样......
In the .aspx file:
在.aspx文件中:
<script language="javascript" type="text/javascript">
var stringValue = '<%=GetStringValue();%>';
// For example:
alert(stringValue);
</sctipt>
In the .aspx.cs file:
在.aspx.cs文件中:
private string GetStringValue()
{
return "A string value";
}
This would then show a javascript messagebox saying "A string value" when the page loads
然后,这将在页面加载时显示一个javascript消息框,说“字符串值”
#2
1
You can either generate the script on the server side and embed the value in it:
您可以在服务器端生成脚本并在其中嵌入值:
StringBuilder sb = new StringBuilder();
sb.Append("<script type=\"text/javascript\"");
sb.Append("var someFunc = function(){");
sb.AppendFormat("alert('{0}');", importantServerSideValue);
sb.Append("};");
sb.Append("</script>");
Page.ClientScript.RegisterClientScriptBlock("genScript", sb.ToString());
Or place the value in a hidden form element on the page and access it from the client side Javascript.
或者将值放在页面上的隐藏表单元素中,并从客户端Javascript访问它。
<!-- In the Markup -->
<asp:HtmlInputHidden id="hiddenField" runat="server" />
// And in the code-behind
hiddenField.Value = importantServerSideValue;
#3
0
When I had to pass a variable value from PHP to JS, I used php to echo out the js with a variable set to the php variable's value. e.g.
当我必须将变量值从PHP传递给JS时,我使用php来回显js,并将变量设置为php变量的值。例如
//php code
echo 'var number = ' . $variable . ';';