Javascript in User controls
-
Hello, I am creating a usercontrol which contains two DropDownList boxes, I want to access these controls in the javascript (i.e in ascx file) because I wrote two validation function for those two DropDownList boxes. I am able to do this in aspx file but I want to do it in .ascx file only. Please suggest me how to do this. Regards, Chakravarthy.
-
Hello, I am creating a usercontrol which contains two DropDownList boxes, I want to access these controls in the javascript (i.e in ascx file) because I wrote two validation function for those two DropDownList boxes. I am able to do this in aspx file but I want to do it in .ascx file only. Please suggest me how to do this. Regards, Chakravarthy.
Hi there, If you are able to make it run when you put the client-side script in a web page .aspx, you also can do that when you move the script code to a web user control's content file .ascx. There's one thing that you need to pay your attention to is that the client script will be running on the client machine, so the id of the dropdownlist used in the validation function should be the
clientid
value but not theid
of the dropdownlists declared in the web user control. -
Hi there, If you are able to make it run when you put the client-side script in a web page .aspx, you also can do that when you move the script code to a web user control's content file .ascx. There's one thing that you need to pay your attention to is that the client script will be running on the client machine, so the id of the dropdownlist used in the validation function should be the
clientid
value but not theid
of the dropdownlists declared in the web user control.Hi, Thanks for your reply. I am attaching the code that I written and the exception that got. I declared two dropdownlist boxes in the MyControl.ascx i.e DropDownList1, DropdDownList2. I attached an attribute to the DropDownList1 i.e. this.DropDownList1.Attributes.Add("onchange"," findName (DropDownList1,DropDownList2)"); In the code behind of javascript block I written this code. function findName(List1,List2) { alert(List1.length); alert(List2.length); } The exception that I got is DropDownList1 is Undefined(JavaSript Exception); Please help me.
-
Hi, Thanks for your reply. I am attaching the code that I written and the exception that got. I declared two dropdownlist boxes in the MyControl.ascx i.e DropDownList1, DropdDownList2. I attached an attribute to the DropDownList1 i.e. this.DropDownList1.Attributes.Add("onchange"," findName (DropDownList1,DropDownList2)"); In the code behind of javascript block I written this code. function findName(List1,List2) { alert(List1.length); alert(List2.length); } The exception that I got is DropDownList1 is Undefined(JavaSript Exception); Please help me.
Hi there, You'd better use the
clientid
of the control as the method parameter, the sample code looks something like this:this.DropDownList1.Attributes.Add("onchange"," findName('"+ this.DropDownList1.ClientID +"','"+ this.DropDownList2.ClientID +"')");
In the client side function, you should first get a reference to an html element before accessing any properties and methods supported by this element.
function findName(List1,List2)
{
alert(window.document.getElementById(List1).length);
alert(window.document.getElementById(List2).length);
}For more information, see Control.ClientID Property [^]
-
Hi there, You'd better use the
clientid
of the control as the method parameter, the sample code looks something like this:this.DropDownList1.Attributes.Add("onchange"," findName('"+ this.DropDownList1.ClientID +"','"+ this.DropDownList2.ClientID +"')");
In the client side function, you should first get a reference to an html element before accessing any properties and methods supported by this element.
function findName(List1,List2)
{
alert(window.document.getElementById(List1).length);
alert(window.document.getElementById(List2).length);
}For more information, see Control.ClientID Property [^]
Thank you very much for your help.It solved my problem. Regards, Chakravarthy.