Click here to Skip to main content
15,890,512 members
Home / Discussions / ASP.NET
   

ASP.NET

 
AnswerRe: get control id from the content page Pin
ca8msm16-Mar-07 3:07
ca8msm16-Mar-07 3:07 
AnswerRe: get control id from the content page Pin
Sylvester george16-Mar-07 3:08
Sylvester george16-Mar-07 3:08 
GeneralRe: get control id from the content page Pin
srinandan..16-Mar-07 3:14
srinandan..16-Mar-07 3:14 
GeneralRe: get control id from the content page Pin
Sylvester george16-Mar-07 3:23
Sylvester george16-Mar-07 3:23 
GeneralRe: get control id from the content page Pin
srinandan..16-Mar-07 3:50
srinandan..16-Mar-07 3:50 
GeneralRe: get control id from the content page Pin
Sylvester george16-Mar-07 3:59
Sylvester george16-Mar-07 3:59 
GeneralRe: get control id from the content page Pin
srinandan..16-Mar-07 4:03
srinandan..16-Mar-07 4:03 
AnswerRe: get control id from the content page Pin
Jesse Squire16-Mar-07 3:49
Jesse Squire16-Mar-07 3:49 
In order to ensure that controls have a unique id on the page, ASP.NET will rename your controls as it renders them. In order for you to retrieve the id of a server control on the client side, you will need to progratically reference the ClientID property of the button.

Using the ClientID property, you could refactor your code into something like:

Client Script:

<script language="javascript" type="text/javascript">

 

  function check(buttonId)

  {

    button = document.getElementById(buttonId);

 

    if (button)

    {

      button.style.visibility = "hidden";

    }

 

    return false;

  }

 

</script>


Server Code:

protected void Page_PreRender(object sender, EventArgs ea)

{

  button1.Attributes.Add("onclick", String.Format("javascript:return check({0});", button1.ClientID));   

}


You'll notice that I moved your setting of the button's attribute ito the PreRender event handler. One thing to keep in mind is that a control's client-side id is not final until it, and all of its parents, are added to the page's control tree. In order to accomodate control manipulation, I usually try to reference the ClientID as late in the render cycle as possible.

What I would recommend in your case, however, is simply to pass a reference to the button into your javascript function. This would eliminate any need to use getElementById at all, instead simply manipulating the reference. To do so, you could refactor into something like:

Client Script:

<script language="javascript" type="text/javascript">

 

  function check(button)

  {

    if (button)

    {

      button.style.visibility = "hidden";

    }

 

    return false;

  }

 

</script>


Server Code:

protected void Page_Load(object sender, EventArgs ea)

{

  button1.Attributes.Add("onclick", "javascript:return check(this);");   

}



Hope that helps. Smile | :)

  --Jesse
"... the internet's just a big porn library with some useful articles stuck in." - Rob Rodi

GeneralRe: get control id from the content page Pin
srinandan..16-Mar-07 3:58
srinandan..16-Mar-07 3:58 
GeneralRe: get control id from the content page Pin
Jesse Squire16-Mar-07 5:37
Jesse Squire16-Mar-07 5:37 
GeneralRe: get control id from the content page Pin
srinandan..18-Mar-07 21:08
srinandan..18-Mar-07 21:08 
QuestionMysterious Web User Controls Related Problem Pin
powered_by_xanax16-Mar-07 2:54
powered_by_xanax16-Mar-07 2:54 
QuestionHow to transfer data from HTML table to excel or through dataset to excel. Pin
AjayKrSh16-Mar-07 2:53
AjayKrSh16-Mar-07 2:53 
AnswerRe: How to transfer data from HTML table to excel or through dataset to excel. Pin
Jesse Squire16-Mar-07 3:27
Jesse Squire16-Mar-07 3:27 
GeneralRe: How to transfer data from HTML table to excel or through dataset to excel. Pin
AjayKrSh16-Mar-07 3:48
AjayKrSh16-Mar-07 3:48 
GeneralRe: How to transfer data from HTML table to excel or through dataset to excel. Pin
Jesse Squire16-Mar-07 3:54
Jesse Squire16-Mar-07 3:54 
Questionlistbox and arrays Pin
Kunal P16-Mar-07 2:47
Kunal P16-Mar-07 2:47 
Questionbrowser's back button Pin
yogita charhate16-Mar-07 2:27
yogita charhate16-Mar-07 2:27 
AnswerRe: browser's back button Pin
Kunal P16-Mar-07 2:40
Kunal P16-Mar-07 2:40 
GeneralRe: browser's back button Pin
yuvachandra16-Mar-07 2:48
yuvachandra16-Mar-07 2:48 
GeneralRe: browser's back button Pin
Kunal P16-Mar-07 4:45
Kunal P16-Mar-07 4:45 
GeneralRe: browser's back button Pin
yogita charhate16-Mar-07 4:13
yogita charhate16-Mar-07 4:13 
GeneralRe: browser's back button Pin
Kunal P16-Mar-07 4:48
Kunal P16-Mar-07 4:48 
QuestionSetting _DEBUG for web project Pin
Leo Smith16-Mar-07 2:26
Leo Smith16-Mar-07 2:26 
AnswerRe: Setting _DEBUG for web project Pin
Jesse Squire16-Mar-07 2:56
Jesse Squire16-Mar-07 2:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.