Click here to Skip to main content
15,894,284 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I am creating row in gridview dynamicaly, having problem in geting clientid of server control (textbox) to use in javascript.

have a look at the code to understand prblem.

error : The name 'txt_date' does not exist in the current context

in line :
ASP.NET
document.getElementById('<%= txt_date.ClientID %>'),'mm/dd/yyyy',this

                       <asp:TemplateField HeaderText="Date">
                                    <ItemTemplate>
                                        <div style="float:left;">
                                            <asp:TextBox ID="txt_date" runat="server"  CssClass="textbox"></asp:TextBox>
                                            <img alt="Calender" id="img" height="16" src="images/calender.jpg" width="16" 
                                          oncick="displayCalendar( document.getElementById('<%= txt_date.ClientID %>'),'mm/dd/yyyy',this);" />
                                          </div>
                                       <div style="float:left; margin:4px;">
                                        </div>
                                        <div style="float:left;">
                                            <asp:CustomValidator ID="CustomValidator2" runat="server" 
                                                ErrorMessage="* Invalid Date" OnServerValidate="custDate_ServerValidate"></asp:CustomValidator>
                                        </div>
                                    </ItemTemplate>
                                    <ItemStyle Width="25%" />
                                </asp:TemplateField>
Posted
Updated 8-Sep-11 23:48pm
v5

Use this function :

C#
function GetClientId(strid)
{
     var count=document.getElementsByTagName ('*').length; //<-- gets all elements, instead of Forms as this only returns FORM elements
     var i=0;
     var eleName;
     for (i=0; i < count; i++ )
     {
       eleName=document.getElementsByTagName ('*')[i].id;
       pos=eleName.indexOf(strid);
       if(pos>=0)  break;
     }
    return eleName;
}


Pass txt_date as input to it, you will get the clientId for the same. You can keep the Breakpoint on the last line of this function and evaluate eleName.

Sometimes, it becomes very troublesome to find the client ID for the server side controls, especially for user controls, data grid elements. In such scenarios you can deploy this function to find the client ID for the server control in question. Just the Server Side Name of the control and you will get the client ID for the same.

Hope this helps.
All the best.
 
Share this answer
 
v2
In JS you can get the server control like

document.getElementById('<%=txt_date.ClientID%>')
 
Share this answer
 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900