challengeAccepted
challengeAccepted

Reputation: 7600

Javascript does not take hidden text box value?

I am using this javascript function to show different popups if location count varies. Here the txthiddenloccount value is null if the txtbox's visibility is false. If the visibility is true, it works fine. What strange is this??? Can someone help me out.

function isPageValid()
{
var validated = Page_ClientValidate('groupProfile');
var loccount = document.getElementById("ctl00_ContentPlaceHolder1_txthiddenloccount").value;
if(validated)
{
   if(loccount == '1') 
   {
     var mdlPopup = $find('<%= ModalPopupExtendersavechanges.ClientID %>');
     if(mdlPopup)
     {
       mdlPopup.show();           
     }
  }
   else
   {
      var mdlPopup = $find('<%= ModalPopupExtenderMerchantUpdate.ClientID %>');

      if(mdlPopup)
      {
         mdlPopup.show();           
      }
  }
}
}

Upvotes: 1

Views: 3554

Answers (3)

ctorx
ctorx

Reputation: 6941

If the Visible property of the control is set as false via ASP.NET, it will be part of the control tree but will never actually get rendered to the page. If it doesn't get rendered to the page, JavaScript can't access it.

If you want to hide it using ASP.NET, you could do it this way in C#...

txthiddenloccount.Style.Add("display", "none");

That will not prevent the control from rendering on the page AND it will use CSS to hide it. Alternatively, you could do this, but it might not be what you want, visually...

txthiddenloccount.Style.Add("visibility", "hidden");

Hope that helps.

Upvotes: 1

Sukhjeevan
Sukhjeevan

Reputation: 3156

Here you are trying to get txthiddenloccount control's value which hasn't rendered on the page because its visibility is false. so first you have to check if it is null i.e you can write code like this.

var loccount='';
if(document.getElementById("ctl00_ContentPlaceHolder1_txthiddenloccount") != null)
{
 loccount = document.getElementById("ctl00_ContentPlaceHolder1_txthiddenloccount").value;
}

Upvotes: 0

John Boker
John Boker

Reputation: 83709

if txthiddenloccount is an asp:TextBox that has the Visible property set to false then it does not exist on the page that is readable by javascript. It will be stored in the ViewState.

For something like this you're probably better off using an asp:HiddenField and setting the value, that will create an input type='hidden' that will be accessible through javascript.

Upvotes: 5

Related Questions