使用C#获取的onload隐藏字段值 [英] Get onload hidden field value using c#

查看:251
本文介绍了使用C#获取的onload隐藏字段值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是低于code的值分配给code隐藏control.But后面我不能让隐藏控件的值。请帮我弄this.I尝试更多的时间。

 脚本
=======
<脚本类型=文/ JavaScript的>
        功能load_value(){
            VAR VAL =的document.getElementById('<%= hf_xml.ClientID%GT;');
            val.value =海; //我想做的事情
            警报(v​​al.value); //警告消息显示文本海
        }
        在window.onload = load_value;
    < / SCRIPT>< ASP:HiddenField ID =hf_xml=服务器/>code的背后
=========== 保护无效的Page_Load(对象发件人,EventArgs的发送)
 {
    字符串值= hf_xml.Value; //总是空
 }


解决方案

有什么真正缺少/不正确在code。试着去了解发生的事件顺序。

在window.onload 预计,当网页加载完成得到执行。而的Page_Load 预计随着页面尚在过程中被称为更早。

这确实发生了。由于使用调试符号核实,的Page_Load 被称为第一和的window.onload 方法将在后面叫。这就是为什么你的 HiddenField 是显示为空值。

此外,如预期,因为当被请求页面的第一次,HiddenField值将是空的,但在接下来的起回传,值将为此HiddenField进行设置。

I am using the below code to assign the value to a hidden control.But in code behind i can't get the value of the hidden control. Please help me to get this.I tried more time.

Script
=======
<script type="text/javascript">
        function load_value() {
            var val = document.getElementById('<%= hf_xml.ClientID %>');
            val.value= "hai";//Whatever i want
            alert(val.value);//alert message show with text hai
        }
        window.onload = load_value;
    </script>

<asp:HiddenField ID="hf_xml" runat="server" />

Code Behind
===========

 protected void Page_Load(object sender, EventArgs e)
 {
    string value = hf_xml.Value;//Always Empty
 }

解决方案

There is nothing really missing/incorrect in your code. Try to understand the sequence of events that happen.

Window.Onload is expected to get executed when the Page finishes loading. Whereas Page_Load is expected to be called earlier as the Page is still under process.

And this is indeed happening. As verified using debug symbols, the Page_Load is called first and the window.onload method will be called later. This is why your HiddenField is showing empty value.

Also, as expected, for the very first time when page is requested, HiddenField value will be Empty, but on next postback onwards, Value will be set for this HiddenField.

这篇关于使用C#获取的onload隐藏字段值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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