zameer21
asked on
Javascript in ASP.net web user control
How can use javascript in Web user control.I have a check box in User control which when checked should disable a text box on the user control.I want to use javascript to solve this.How can i do that in web user control.
Thanks a bunch
Thanks a bunch
Hi, zameer21!
Also you can use RegisterClientScriptBlock method.
Here is example, where your chekbox control the state of textbox
<html>
<head>
<script language="C#" runat="server">
public void Page_Load(Object sender, EventArgs e) {
// Form the script that is to be registered at client side.
String scriptString = "<script language=JavaScript> function DoClick() {";
scriptString += "myForm.txt.disabled = myForm.chk.checked;}<";
scriptString += "/";
scriptString += "script>";
if(!this.IsClientScriptBlo ckRegister ed("client Script"))
this.RegisterClientScriptB lock("clie ntScript", scriptString);
}
</script>
</head>
<body topmargin="20">
<form id="myForm" runat="server">
TextBox <INPUT type="text" id="txt" value="sometext">
<br>
<INPUT type="checkbox" id="chk" onclick="DoClick()"> Disable It
</form>
</body>
</html>
Also you can use RegisterClientScriptBlock method.
Here is example, where your chekbox control the state of textbox
<html>
<head>
<script language="C#" runat="server">
public void Page_Load(Object sender, EventArgs e) {
// Form the script that is to be registered at client side.
String scriptString = "<script language=JavaScript> function DoClick() {";
scriptString += "myForm.txt.disabled = myForm.chk.checked;}<";
scriptString += "/";
scriptString += "script>";
if(!this.IsClientScriptBlo
this.RegisterClientScriptB
}
</script>
</head>
<body topmargin="20">
<form id="myForm" runat="server">
TextBox <INPUT type="text" id="txt" value="sometext">
<br>
<INPUT type="checkbox" id="chk" onclick="DoClick()"> Disable It
</form>
</body>
</html>
Place this code in html
<script runat="server">
void Selection_Change(Object sender, EventArgs e)
{
// Set the background color for days in the Calendar control
// based on the value selected by the user from the
// DropDownList control.
Calendar1.DayStyle.BackCol or =
System.Drawing.Color.FromN ame("Red") ;
}
</script>
<asp:Calendar id="Calendar1" ShowGridLines="True" ShowTitle="True" runat="server" />
<asp:CheckBox id="CheckBox2" runat="server" AutoPostBack="True" OnCheckedChanged="Selectio n_Change" runat="server"></asp:Check Box>
Cheers,
<script runat="server">
void Selection_Change(Object sender, EventArgs e)
{
// Set the background color for days in the Calendar control
// based on the value selected by the user from the
// DropDownList control.
Calendar1.DayStyle.BackCol
System.Drawing.Color.FromN
}
</script>
<asp:Calendar id="Calendar1" ShowGridLines="True" ShowTitle="True" runat="server" />
<asp:CheckBox id="CheckBox2" runat="server" AutoPostBack="True" OnCheckedChanged="Selectio
Cheers,
ASKER
Igor alpha i tried what you said and i get a message
saying the method
protype.usercontrol.PanelT race cannot find the definition for method IsClientScriptBlockRegiste red cannot be found.
protype.usercontrol.PanelT race cannot find the definition for method RegisterClientScriptBlockc annot be found.
Thanks
saying the method
protype.usercontrol.PanelT
protype.usercontrol.PanelT
Thanks
ASKER
Can i access a textbox control in page which has a table with Textbox in it and no form tag present using javascript
Thanks
Thanks
Change this lines:
if(!this.IsClientScriptBlo ckRegister ed("client Script"))
this.RegisterClientScriptB lock("clie ntScript", scriptString);
with:
if(!Page.IsClientScriptBlo ckRegister ed("client Script"))
Page.RegisterClientScriptB lock("clie ntScript", scriptString);
if(!this.IsClientScriptBlo
this.RegisterClientScriptB
with:
if(!Page.IsClientScriptBlo
Page.RegisterClientScriptB
ASKER
I got that done,but i have a problem because i am using this user control on a main page and the main page has already got form tag in it,I am getting error if i am putting the form tag in user control as well.How can i access each control on user control,take for exaple using javascript if on main page we use Document.formname.textboxn ame.value, how can i do that now if there is no form tag in user control.
Thanks
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
then put a link to it on your aspx page in the head:
<script language="javascript" src="yourJavascriptFile.js
any user control should be able to use the functions inside the yourJavascriptFile.js file.