简体   繁体   中英

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:

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:

string hdnValue = this.showHideFlag.Value;

But this hdnValue is not getting the value of that hidden field.

Can someone help on this?

Hidden as type="hidden"

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

Hidden as display: none

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

Gets the control ID for HTML markup that is generated by ASP.NET.

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

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. try to set value by Client id as follows-

 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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