无法访问JavaScript中的隐藏控件值 [英] Not able to access hidden control value in javascript

查看:63
本文介绍了无法访问JavaScript中的隐藏控件值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在背后的代码中

In Code Behind

protected void Button1_Click(object sender, EventArgs e)
   {
   set();
   }
public void set()
   {
       string s = "10";
       HiddenField1.Value = s;
   }

In Aspx Source

<script language="javascript" type="text/javascript">

function get() {
           var s = form1.HiddenField1.value;
           alert(s);
       }

</script>

<asp:Button ID="Button1" runat="server" onclick="Button1_Click" OnClientClick="get()" Text="Button" />


我有一个隐藏字段,并在上述代码的set方法中用iam设置了其值
现在我只想访问我的javascript函数Get
中的值
但是iam面临的diffretn输出几乎没有预期的

:输出是这样的

1.当我第一次单击该按钮时,我会收到一个空警报,但再次单击该按钮,我得到的是我赋予的值

2.当我对page_load中的值进行硬编码时,仅在剩余代码执行过程中不更新该值n.


i have a hidden field and iam setting its value in set method in the above code
now i just want to access that value into my javascript''s Function named Get

but iam faceing little diffretn output than expected

: the output is like this

1. when i click the button for the first time i get an empty alert but wen i click again am getting the the value i gave

2. When i hardcode the value in the page_load its taking only that value n not getting updated during remaining code execution

推荐答案

那是正确的.在回发事件之前会触发OnClientClick.因此,首先触发"get()",然后触发"set()".第一次"get()"无法找到任何东西,第二次第一次单击时的"set()"已触发并设置值,因此"get()"现在可以看到该值.

通过硬编码,您可以在每次页面加载时使用您提供的默认值覆盖您的hiddenfield内容.

因此,这两种行为都是标准的.NET.您到底想达到什么目的?
That''s correct. The OnClientClick is fired before the postback event. So first the ''get()'' is fired and then the ''set()''. The first time the ''get()'' is not able to find anything, the second time the ''set()'' from the first click has fired and set the values, so that the ''get()'' now sees the value.

With hardcoding you overwrite your hiddenfield content on every page load with the default value you gave.

So both behaviors are standard .NET. What are you exactly trying to achieve?


这篇关于无法访问JavaScript中的隐藏控件值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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