Javascript in web user control
-
Hello, Have anyone used javascript in an web user control(.ascx) (Visual Studion 2005, .NET Framework 2.0)? Alert works fine, but for example, I cant seem to get the ID of an asp checkbox. function CheckBoxChecked(id) { 1. alert(id); var elem = document.getElementById(id); var temp = document.getElementById('CheckBox1'); 2. alert(elem); 3. alert(temp); } results in: 1. Undefined 2. null 3. null I invoke the script as follows: asp:CheckBox ID="CheckBox1" runat="server" onclick="javascript:CheckBoxChecked()" (I have used javascripts with .ascx components before, though they were predifined by Telerik, which contols I were using at the time) //LL
-
Hello, Have anyone used javascript in an web user control(.ascx) (Visual Studion 2005, .NET Framework 2.0)? Alert works fine, but for example, I cant seem to get the ID of an asp checkbox. function CheckBoxChecked(id) { 1. alert(id); var elem = document.getElementById(id); var temp = document.getElementById('CheckBox1'); 2. alert(elem); 3. alert(temp); } results in: 1. Undefined 2. null 3. null I invoke the script as follows: asp:CheckBox ID="CheckBox1" runat="server" onclick="javascript:CheckBoxChecked()" (I have used javascripts with .ascx components before, though they were predifined by Telerik, which contols I were using at the time) //LL
try asp:CheckBox ID="CheckBox1" runat="server" onclick="javascript:CheckBoxChecked(this)" function CheckBoxChecked(ctl) { alert(ctl.id) } :)
-
Hello, Have anyone used javascript in an web user control(.ascx) (Visual Studion 2005, .NET Framework 2.0)? Alert works fine, but for example, I cant seem to get the ID of an asp checkbox. function CheckBoxChecked(id) { 1. alert(id); var elem = document.getElementById(id); var temp = document.getElementById('CheckBox1'); 2. alert(elem); 3. alert(temp); } results in: 1. Undefined 2. null 3. null I invoke the script as follows: asp:CheckBox ID="CheckBox1" runat="server" onclick="javascript:CheckBoxChecked()" (I have used javascripts with .ascx components before, though they were predifined by Telerik, which contols I were using at the time) //LL
Hi there, Since the checkbox control is placed in a user control, so the
ClientID
of the control is not as simple as the"CheckBox1"
. You can view source the web page to have an idea on the real value of the id property of the control at the client side, you will find that it contains the web user control's id as the prefix. -
Hello, Have anyone used javascript in an web user control(.ascx) (Visual Studion 2005, .NET Framework 2.0)? Alert works fine, but for example, I cant seem to get the ID of an asp checkbox. function CheckBoxChecked(id) { 1. alert(id); var elem = document.getElementById(id); var temp = document.getElementById('CheckBox1'); 2. alert(elem); 3. alert(temp); } results in: 1. Undefined 2. null 3. null I invoke the script as follows: asp:CheckBox ID="CheckBox1" runat="server" onclick="javascript:CheckBoxChecked()" (I have used javascripts with .ascx components before, though they were predifined by Telerik, which contols I were using at the time) //LL
Hi there,
Lex Luthor wrote:
asp:CheckBox ID="CheckBox1" runat="server" onclick="javascriptCheckBoxChecked()"
I think you have missed the colon ":" and control in above line i.e. you should write asp:CheckBox ID="CheckBox1" runat="server" onclick="javascript:CheckBoxChecked(this)" Pankaj Kulkarni
-
Hello, Have anyone used javascript in an web user control(.ascx) (Visual Studion 2005, .NET Framework 2.0)? Alert works fine, but for example, I cant seem to get the ID of an asp checkbox. function CheckBoxChecked(id) { 1. alert(id); var elem = document.getElementById(id); var temp = document.getElementById('CheckBox1'); 2. alert(elem); 3. alert(temp); } results in: 1. Undefined 2. null 3. null I invoke the script as follows: asp:CheckBox ID="CheckBox1" runat="server" onclick="javascript:CheckBoxChecked()" (I have used javascripts with .ascx components before, though they were predifined by Telerik, which contols I were using at the time) //LL
As one of the answers (minhpc_bk) also implied I had to go through the .aspx which uses the control to get the information I required. I use this function each time I wan't an id or so for one of the controls on the .ascx. function GetIdString() { var id = new String("<%= this.ClientID %>_"); return id; } Now I can easily do something like this in another function: document.getElementById(GetIdString()+"CheckBox1") Thanx everybody for the response... //LL
-
As one of the answers (minhpc_bk) also implied I had to go through the .aspx which uses the control to get the information I required. I use this function each time I wan't an id or so for one of the controls on the .ascx. function GetIdString() { var id = new String("<%= this.ClientID %>_"); return id; } Now I can easily do something like this in another function: document.getElementById(GetIdString()+"CheckBox1") Thanx everybody for the response... //LL
Hi, It's really cool man. I also tried. Thanks minhpc_bk and Lex Luthor Pankaj Kulkarni