从代码后面(c#)设置javascript变量的值
Setting value for javascript variable from code behind (C#)
我理解客户端脚本和服务器端脚本之间的区别。我有一个javascript函数和变量在我的MasterPage
:
<script language="JavaScript" type="text/javascript">
var needToConfirm = false;
window.onbeforeunload = confirmExit;
function confirmExit()
{
if (needToConfirm)
{
needToConfirm = false;
return "Currently in edit mode. If you leave the page now then you will lose unsaved changes."
}
}
</script>
考虑到我的ASP。. NET(客户端)我可以将我的needToConfirm
变量的值更改为true
onClientClick
,但默认情况下它是false。下面是一个例子。
<asp:Button ID="btnEdit" runat="server" Text=" Edit " onclick="btnEdit_Click" OnClientClick="needToConfirm = true;" />
现在的问题是,当在c#(服务器端)上,我必须将needToConfirm
在if-statement
下设置为true,但不一定在Page_Load
上设置:
private void SetDefault()
if (Session[def.ID_CUST] != null)
{
//I want to change the variable value here
}
}
谢谢。
我使用的是。net 2.0 Classic和WebForms
后面的代码:
ScriptManager.RegisterStartupScript(this, this.GetType(), "", "urFunction('urValHere');", true);
客户端:
function urFunction(urParam) {
//do what u want here
//use urParam
}
您可以使用隐藏输入,然后从服务器端将此输入设置为true
或false
。
客户端:
<input type="hidden" id="hdnConfirm" runat="server" value="false"/>
然后在服务器端:
if (Session[def.ID_CUST] != null)
{
//I want to change the variable value here
hdnConfirm.Value = "true";
}
然后在客户端:
var needToConfirm = $('#hdnConfirm').val();
如果我理解正确的话,您可以像在http://msdn.microsoft.com/en-us/library/z9h4dk8y.aspx的示例中那样注册客户端脚本。
ClientScriptManager cs = Page.ClientScript;
if (!cs.IsStartupScriptRegistered(this.GetType(), "EditMode")) {
cs.RegisterStartupScript(this.GetType(), "EditMode", "needToConfirm = true;", true);
}
这将写一个脚本到在Javascript中设置needToConfirm
值的页面
基于你的更新说它是。net 2.0,这是你如何设置一个javascript变量:
Page.RegisterStartupScript("SetVar", "var needToConfirm = true;");
http://msdn.microsoft.com/en-us/library/system.web.ui.page.registerstartupscript (v = vs.80) . aspx
仅供参考。下面是做类似事情的4.5种方法:
// Define the name and type of the client scripts on the page.
const String csname1 = "MyScriptName";
Type cstype = this.GetType();
// Get a ClientScriptManager reference from the Page class.
ClientScriptManager cs = Page.ClientScript;
// Check to see if the startup script is already registered.
if (!cs.IsStartupScriptRegistered(cstype, csname1))
{
StringBuilder cstext1 = new StringBuilder();
cstext1.Append("<script> var myVariable = true; </");
cstext1.Append("script>");
cs.RegisterStartupScript(cstype, csname1, cstext1.ToString());
}
http://msdn.microsoft.com/en-us/library/system.web.ui.clientscriptmanager.registerstartupscript (v = vs.110) . aspx
也许这有帮助?
<script type="text/javascript">
function test(confirm){
var needToConfirm = confirm;
window.onbeforeunload = confirmExit;
function confirmExit() {
if (needToConfirm) {
needToConfirm = false;
return "Currently in edit mode. If you leave the page now then you will lose unsaved changes."
}
}
}
</script>
<asp:Button ID="btnEdit" runat="server" Text="Edit" onclick="btnEdit_Click" OnClientClick="javascript:test(true);" />
- 通过Javascript设置Telerik RadTreeView属性,如OnClientNodeExpanded
- 使用javascript设置iframe的高度
- 通过JavaScript设置表单目标
- 如何确定相对较新版本的IE的高度和宽度(IE8不喜欢从JavaScript设置这种样式吗?
- 通过JavaScript设置的表单字段的值不会作为$_POST的一部分传递给PHP脚本
- 如何在Internet Explorer中使用javascript设置cookie
- Javascript设置日期不起作用,显示错误的时间
- iOS Safari Javascript设置超时问题
- 使用Javascript设置库中图像包装器的宽度
- 如何检查日期选择器和时间选择器元素是否使用JQuery/Javascript设置
- 如何使用javascript设置元素旋转和动态观察的动画
- 在 PHP 块中使用 Javascript 设置 HTML 文本框的值(使用 echo)
- 通过javascript设置IE特定的css样式
- Javascript-设置cookie并拉入ID
- ASP从Javascript设置控件值
- 尝试使用window.innerHeight和Javascript设置图像高度
- 使用JavaScript设置整个页面的字体大小
- 使用纯Javascript设置最小高度属性
- 如何通过javascript设置支柱列表2
- 使用 JavaScript 设置 的点击