从javascript分配一个字段并从CodeBehind检索它 [英] Assign a field from javascript and retrieve it from CodeBehind

查看:55
本文介绍了从javascript分配一个字段并从CodeBehind检索它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是在做:

Javascript:

I''m just doing :

Javascript:

function assign(){
 var sHidVar = 'something';
 document.getElementById('<%=hdnPage.ClientID%>') = sHidVar ;
 alert(document.getElementById('<%=hdnPage.ClientID%>'));
}



ASPX



ASPX

...
 <asp:HiddenField ID=hdnPage runat=server  />
...



这是我执行此操作时遇到的问题:
ASPX:



Here is the problem when I do this:
ASPX :

<a href=#  önclick="Javascript:assign();">Test</a>


它运行正常,已分配了变量,我可以从CodeBehind访问以下对象:


it runs fine, the variable is assigned and I can access from CodeBehind by:

string s = hdnPage.Value;



但是,要在页面加载时运行此功能,例如:



but, to run this function on page load like :

protected void Page_Load(object sender, EventArgs e)
...
ClientScript.RegisterClientScriptBlock(this.GetType(), "assign", "assign();", true);
..
}



它给出一个错误,说document.getElementById(...)不是对象

在页面加载时我还能如何调用该函数,该函数有什么问题吗?

PS:还尝试了



it gives an error, saying document.getElementById(...) is not an object

how else can I call the function on page load, of is there something wrong with the function

PS: Also tried the

$('<%=hdnPage.ClientID%>')

无效

推荐答案

(' <%= hdnPage.ClientID%>')

,不起作用


放置您的JavaScript函数在标记和测试结束之前.
Place your JavaScript function before just before the end of the tag and test.


您无法在服务器端page_load上调用javascript函数.为此,您需要像这样使用javascript window.onload事件
You cannot call the javascript function on server side page_load. For that you need to user javascript window.onload event like this
window.onload=function assign(){
 var sHidVar = 'something';
 document.getElementById('<%=hdnPage.ClientID%>') = sHidVar ;
 alert(document.getElementById('<%=hdnPage.ClientID%>'));
}


这篇关于从javascript分配一个字段并从CodeBehind检索它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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