简体   繁体   English

asp.net隐藏字段值不在窗体加载事件上

[英]asp.net hidden field value not getting on form load event

on body load i am calling this function 在身体负荷我正在调用此功能

<body  onload="lotclick('1');">

in this function i am assigning value to hidden field 在此功能中,我正在为隐藏字段分配值

function lotclick(lotid) 
{
    var dt = new Date();

    document.getElementById("lblindTime").value = dt.toString();

and in code behind file on form load event i am trying to get value of that hidden field.but i am unable to get that value. 并在表单加载事件中文件后面的代码中,我试图获取该隐藏字段的值。但是我无法获取该值。

 protected void Page_Load(object sender, EventArgs e)
        {

            string sfsf = lblindTime.Value.ToString();
    }

but i am not getting any value. 但我没有任何价值。 but on button click i am getting value. 但在按钮上单击我正在获得价值。 what will be the issue.?? 会有什么问题。

您无法以这种方式获取它,因为Page_Load在服务器端执行,并且在onload在客户端执行之前就被执行了。

i am not getting any value. 我没有任何价值。 but on button click i am getting value. 但在按钮上单击我正在获得价值。

Are you expecting the value to be there the first time you load the page? 您是否期望一次加载页面时存在该值? Because that's not possible. 因为那是不可能的。 When you click the button, you post back to the server and that post-back includes the values which were set client-side. 当您单击按钮时,您回发到服务器,并且该回发包括在客户端设置的值。 Which is why your button click is "working." 这就是为什么您的按钮单击“有效”的原因。

However, when you first load the page, your server-side code can't see client-side values which haven't been calculated yet . 但是,当您第一次加载页面时,您的服务器端代码看不到尚未计算的客户端值。 The order of operations in your case is roughly: 您的情况下的操作顺序大致为:

  1. User requests page 用户请求页面
  2. Page_Load executes Page_Load执行
  3. Page is loaded in browser 页面已加载到浏览器中
  4. JavaScript executes JavaScript执行
  5. Form field is updated 表单字段已更新
  6. User clicks button 用户点击按钮
  7. Form fields are sent to the server 表单字段被发送到服务器
  8. Page_Load executes Page_Load执行
  9. Click handler executes 点击处理程序执行
  10. and so on... 等等...

Step 2 can't use a value that isn't going to be calculated until step 4. ASP .NET doesn't support prescience (yet). 步骤2不能使用直到步骤4才要计算的值。ASP.NET目前尚不支持科学性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM