Web Studyaspnet.blogspot.com Babyneed.blogspot.com

ASP.Net Web Developer Guide

Power By: eXTReMe Tracker Powered by Blogger  
 
 
 
 

 

 
Important Links
 
   
     
 
Sitemap
 
   
   
 
Reference
 
   

Wednesday, March 29, 2006

Creating a Simple Web User Control

Suppose that we want to build the control as shown in Figure 3.88. If a host page embeds this control, it will automatically display the current time in the server’s time zone. Once we build this control, we can use it in any subsequent page.We will provide a step-by-step procedure to build this control.
Figure 3.88 A Sample User Control

  1. Develop the necessary code for the control.The code for this example is shown in Figure 3.89.The code is essentially very simple.We are using use a <table> tag with an embedded <asp:Label> control. In the Page_Load event, we will display the current time in the label.
    Figure 3.89 The Code for the User Control (TimeUserControl.ascx)
    <!— Chapter3/TimeUserControl.ascx —>
    <table border ="5" cellpadding="5" rules="none"
    bgcolor="lightyellow" bordercolor="orange">
    <tr valign="middle"><td><h3>The time in server land is</h3></td>
    <td><h3><asp:Label id="lblDateTime" runat="server"/></h3></td>
    </tr>
    </table>
    <script Language="vb" runat ="server">
    Sub Page_Load(s As Object, e As EventArgs)
    If Not Page.IsPostBack Then
    ' lblDateTime.Text=System.DateTime.Now.ToLongTimeString()
    lblDateTime.Text=Format(Now,"hh:mm:ss")
    End If
    End Sub
    </script>
  2. Save the code with an extension of *.ascx in your virtual directory.
  3. Test the User Control: A control cannot be tested unless it is hosted in an ASPX page.Thus, start a new page, and enter the code shown in Figure 3.90. First, a host page needs to register a user control using the Register directive.The Register directive has three major attributes.We provide a prefix in the tagprefix attribute (it can be any prefix of your choice).Then we need to provide a name of the registered control in the tagname attribute. Finally, we must also specify the name of the source code (of the .ascx file) using the Src attribute. Can you believe that you are done? Go ahead and open the page in your browser.You will see a page very similar to the one shown in Figure 3.91.
    Figure 3.90 Testing the User Control (TestTimeUserCntrol1.aspx)
    <!— Chapter3/TestTimeUserControl1.aspx —>
    <%@ Register tagprefix ="utoledo" tagname="Time"
    Src="TimeUserControl.ascx" %>
    <html><head></head><form><body>
    <b>I am a host page. Suppose that I don't know how to show the time.
    Hence, I will use the TimeUserControl. I am using an instance of the
    TimeUserControl below:<p>
    <utoledo:Time runat="server" /><br/>
    Now I can do my other work... <b/>
    </body></form></html>
    Figure 3.91 Using a User Control

1 Comments:

Blogger Unknown said...

Hi, The type of browsers visitors use for Web Design Cochin, the kind of Internet connections used to visit your site and the computers used are other important design factors.Thanks.....

10:50 PM  

Post a Comment

<< Home