Friday, July 20, 2007

Control.ClientID in ASP.NET

Hello everyBody,
Today i'll describe a very important things for developers who are working with "ASP.NET" and handle some Codes with "JavaScript " this problem is the [Controls ID on Client Side] when they use (Master Page) or (User Controls) Cause on run time controls take an ID Mostly different to the ID which them specified it at design time
EX:
Design Time:
<input type="button" value="Test" ID="btn_test"/>
______________________________
Run Time:
------------------
Case: Directly Page
{
<input type="button" value="Test" id="btn_test"/>
}
______________________________
Case: UserControl
{
<input type="button" value="Test" id="WebUserControl_btn_test"/>
}
______________________________
Case: Master Page
{
<input type="button" value="Test" id="ct100_Defult_btn_test"/>
}
-----------------------------------------------------------------------------
So if you typed a java script code like that


<script type="text/javascript" language="javascript">
function message()
{
alert(document.getElementById("btn_Test").id);
}
(/script)
------------------------------------------------
it will give us this JavaScript Error
[Object Expected]
Cause the (id) of the control changed so now there is no control with this id, i wish it's clear now.
___________________________
ok io think now you are asking,
what's the solution?
The solution is so simple i think who use ASP classic know it it's one line or less

< % = ControlName in design Time.ClientID % > :)
=================
Tip:
Remove Spaces<%=ControlName in desgin Name.ClientID%>
<%=ControlName in desgin Name.ClientID%>
<%=ControlName in desgin Name.ClientID%>
=================
realy it's so powerful solution for this issue ;)
wish i could help u :)