Saturday, September 3, 2011

Access server contol ID's in javascript without using <% %>

we offen seen this error
"The Controls collection cannot be modified because the control contains code blocks (i.e. <% … %>)."
Here is the fix for it.
For suppose TextBox server contol is like below:
 




In .cs file write the below code in Page_Load event.
  

protected void Page_Load(object sender, EventArgs e)
{
var javascript = new StringBuilder("var JSClientId = '" + TextBox1.ClientID + "';");
this.Page.ClientScript.RegisterStartupScript(this.GetType(), "ControlsClientIds", javascript.ToString(), true);
}

In javascript we can access the TextBox1 server control id as follows
 







No comments:

Post a Comment