【问题标题】:Hidden Field looses it's value before/when submit button pushed隐藏字段在按下提交按钮之前/之后失去其价值
【发布时间】:2019-07-17 21:15:57
【问题描述】:

我在表单中使用 ASP.NEt hiddenfield:

 <form id="form1" runat="server">       

        <asp:HiddenField ID="ScreenWidth"  runat="server"  />
        <asp:HiddenField ID="ScreenHeight" runat="server" />

     <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
</form>

这是JS代码:

<script type="text/javascript">
$(document).ready(function() {
    alert(document.getElementById("ScreenWidth").val);
    document.getElementById("ScreenWidth").val = screen.width;
    document.getElementById("ScreenHeight").val = screen.height;
     alert(document.getElementById("ScreenWidth").val);

    });

但是当我访问 onsubmit 函数上的值时 - 它们是空的。 警报显示未定义,然后在触发时显示一个值 - 所以 JS.部分工作......

如何获取 c# 代码中的值?

  myheight = ScreenHeight.Value.ToString();
    mywidth = ScreenWidth.Value.ToString();

【问题讨论】:

  • 尝试添加 ClientIDMode="Static" 和 enableViewState = true 但没有帮助。
  • 尝试使用完整的单词value,而不是原版js的val。

标签: asp.net hidden-field


【解决方案1】:

使用 toString();在js中:

document.getElementById("hidScreenWidth").value = screen.width.toString();

代码隐藏:

string w = hidScreenWidth.Value;

【讨论】:

  • 这是 js 的标准语法。 val() 与 jquery 一起使用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-20
  • 1970-01-01
  • 2018-05-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多