Link to home
Start Free TrialLog in
Avatar of zameer21
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
Avatar of samtran0331
samtran0331
Flag of United States of America image

put your javascript functions in a separate *.js file,
then put a link to it on your aspx page in the head:
                  <script language="javascript" src="yourJavascriptFile.js" type="text/javascript"></script>

any user control should be able to use the functions inside the yourJavascriptFile.js file.
Avatar of igor_alpha
igor_alpha

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.IsClientScriptBlockRegistered("clientScript"))
              this.RegisterClientScriptBlock("clientScript", 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>
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.BackColor =
             System.Drawing.Color.FromName("Red");

      }
 
      </script>

<asp:Calendar id="Calendar1" ShowGridLines="True" ShowTitle="True" runat="server" />

<asp:CheckBox id="CheckBox2" runat="server"  AutoPostBack="True" OnCheckedChanged="Selection_Change" runat="server"></asp:CheckBox>


Cheers,
Avatar of zameer21

ASKER

Igor alpha i tried what you said and i get a message
saying the method
protype.usercontrol.PanelTrace cannot find the definition for method IsClientScriptBlockRegistered cannot be found.
protype.usercontrol.PanelTrace cannot find the definition for method RegisterClientScriptBlockcannot be found.
Thanks
Can i access a textbox control in page which has a table with Textbox in it and no form tag present using javascript
Thanks
Change this lines:

if(!this.IsClientScriptBlockRegistered("clientScript"))
              this.RegisterClientScriptBlock("clientScript", scriptString);

with:

 if(!Page.IsClientScriptBlockRegistered("clientScript"))
              Page.RegisterClientScriptBlock("clientScript", scriptString);
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.textboxname.value,how can i do that now if there is no form tag in user control.
Thanks
ASKER CERTIFIED SOLUTION
Avatar of igor_alpha
igor_alpha

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial