开发者

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();           
      }
  }
}
}


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.


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;
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜