Thursday, April 21, 2011

Not able to access dynamic controls (inside ascx) using javascript (on aspx page)

Hello all,

I have a user control (ascx) which contains a control (hidden field). In the page_load event of ascx, i have the below code:

HyperLink.Attributes.Add("onclick", "JavaScript:return AccessControl('" + hdnField.UniqueID + "');"); Note: The hidden field resides in .ascx file.

I want to access this hidden field at runtime and i have the below javascript function in aspx page that loads the usercontrol.

function AccessControl(hdnFieldId) { var ctrl = document.getElementById(hdnFieldId); alert(ctrl);

I am getting 'null' value in the alert. I tried using 'ClientID' and 'this' instead of 'UniqueId'. I am wondering why this is not working. I am using .net 2.0.

From stackoverflow
  • Does the hidden field exist when the page renders? Try moving the code for adding the control into the page init, this is the safest place to dynamically add controls to the page.

0 comments:

Post a Comment