简体   繁体   English

无法读取ascx.cs页中的隐藏字段值

[英]Unable to read hidden field value in ascx.cs page

I have a hidden field like this: 我有一个这样的隐藏字段:

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

I am assigning some value to this hidden field in java script as follows: 我为Java脚本中的此隐藏字段分配了一些值,如下所示:

function controlSearchBar() {
     if ($("#MainContent_ProjectListControl_searchBar").is(":hidden")) {
        $("#MainContent_ProjectListControl_showHideFlag")[0].value = "showing";
        } else {
        $("#MainContent_ProjectListControl_showHideFlag")[0].value = "hiding";
        }
      }

I am trying to read this hidden field in ascx.cs page as follows: 我正在尝试阅读ascx.cs页中的此隐藏字段,如下所示:

string hdnValue = this.showHideFlag.Value;

But this hdnValue is not getting the value of that hidden field. 但是,此hdnValue无法获取该隐藏字段的值。

Can someone help on this? 有人可以帮忙吗?

Hidden as type="hidden" 隐藏为type="hidden"

$("#MainContent_ProjectListControl_searchBar").attr('type') == 'hidden'

Hidden as display: none 隐藏为display: none

$("#MainContent_ProjectListControl_searchBar").is(":hidden")

Gets the control ID for HTML markup that is generated by ASP.NET. 获取由ASP.NET生成的HTML标记的控件ID。

<asp:Label ID="SelectedSport" runat="server" ClientIDMode="Static" ClientID="showHideFlag">

javascript javascript

$("#showHideFlag").text("found");

You are saying that you can get the value in javascript So I think the the problem is with the hidden field. 您是说可以在javascript中获取值,所以我认为问题出在隐藏字段。 try to set value by Client id as follows- 尝试按以下客户ID设置值-

 var hd = document.getElementById('<%= showHideFlag.ClientID%>');
            hd.value = "hi";

And my another question is in which event you are accessing value? 我的另一个问题是,您在哪种情况下获得价值? because If you are setting value in javascript and accessing in Page Load event then it will not work because first of all Page load event gets fired and then Javascript function executes. 因为如果您在javascript中设置值并在Page Load事件中进行访问,则它将无法正常工作,因为首先会触发Page Load事件,然后执行Javascript函数。

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

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