ASP.NET Code
  Home arrow ASP.NET Code arrow Page 2 - User Controls and Client Side Scripting
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Actuate Whitepapers 
Moblin 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ASP.NET CODE

User Controls and Client Side Scripting
By: JB Reddy
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 91
    2004-02-18

    Table of Contents:
  • User Controls and Client Side Scripting
  • Starting Our User Control
  • Utilizing the UserControls

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    User Controls and Client Side Scripting - Starting Our User Control


    (Page 2 of 3 )

    In your favorite editor write the code for UserControl. Add the control directive to indicate it is the UserControl.


    <%@ Control Language="Vb" %>
    <Asp:checkBox Id="MyCheckBox" runat="Server" onClick="javascript:check('MyTagCtl_MyText','Teal');"/>
    <Asp:TextBox Id="MyText" runat="Server" />

    Observe that I am not adding any java script to the TextBox at Declaration but for CheckBox. You can add other stuff for your control here, then add the server side scripting if necessary. I am using this area to add functionality to the TextBox.


    <Script Langugae="Vb" runat="Server">
    Public Sub Page_Load
    ()
     MyText
    .Text="Click Here"
          MyText
    .Attributes.Add("onClick","changeColor('MyTagCtl_MyText','Silver')")
    End Sub
    </Script>


    Notice that we can add script with Attribtes.Add method. This method has two parameters. The first one is the attribute name and the later is the value for the attribute. We are using this method to add java script function to our TextBox.

    The Load event is same as Page_load of the web form. In case our control needs some client side scripting we can add with Script Tag. I am adding the javascript functions which will change the TextBox BackGround Color.


    <script language="JavaScript" type="text/JavaScript">
    function changeColor
    (ctlIdnewColor)
     
    {
      document
    .getElementById(ctlId).style.backgroundColor newColor;
     
    }
    function check
    (ctlID,newColor)
    {
     document
    .getElementById(ctlID).style.backgroundColor newColor;  
    }
    </script>


    We can add the javascript for the controls in the Aspx form where it is called, but it will be more meaningful to add it to the control itself so that calling form need not bother about the implementation. While referring the control we are not only using the control ID, but the TagID, which we will use in the Aspx form. It might be confusing at first, but the simple explanation is when controls are rendered on the form, it acquires the TagId used in the form and concatenates with the Id used in the user control to avoid any ambiguity with the controls used on the form. We will see details in implementation soon. Finally save the file with “Ascx” extension (MyScriptControl.Ascx).

    More ASP.NET Code Articles
    More By JB Reddy


     

    ASP.NET CODE ARTICLES

    - How to Use the ListBox Control in ASP.NET 2.0
    - How to Load XML Documents in ASP.NET 2.0
    - DataGrid Code
    - ASP.NET Guestbook
    - User Controls and Client Side Scripting
    - ASP.NET Programming with Microsoft's AS...
    - ASP.NET Basics (part 3): Hard Choices
    - ASP.NET Basics (part 2): Not My Type
    - ASP.NET Basics (part 1): Nothing But .Net
    - Directory Tree Browser
    - How to get the confirmation of Yes/No from a...
    - Complete example using custom errors and wri...
    - Paging Certain # records per page .NET style
    - General Methods of formatting and Subtractin...
    - .NET LinkButton web control





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway