ASP. NET User Control

Source: Internet
Author: User
This article describes how to create a user control in ASP. NET, dynamically modify the control attributes, and set the event departure mechanism of the control.
========================================================== ======================================
Introduction
ASP. NET Server controls make web development easier and more powerful. We have introduced how to use server controls on ASP. NET pages. However, what should I do if the server does not have the required control?

 

Of course, ASP. NET won't give you an unexpected control. In fact, you can use your own controls to replace the controls provided by. net. This control is a user control, which is also the topic discussed in this article.

Write the first user control
Some people think that it is easy to write user controls without knowing how to use server-side controls.

In fact, writing a basic user control (sometimes called pagelets) and making ASP. NET pages use these controls as if they were using server-side controls is indeed simple. Here is a simple example:

Basic. ascx
<P>
This is a user control... really!
</P>

This is a user control! Here, I think you will say that I should not be drunk. I don't know. However, this code is indeed a user control that is easy to use. Although this control does not do anything, it is a good description of what is a user control. Things are not as complicated as you think. Note that the suffix. ascx tells the webpage that this is a user control. It has no special meaning, but does not allow IIS to directly execute this code.

Now let's create a user control. Let's look at the example below:

Basic. aspx
<% @ Page Language = "VB" %>
<% @ Register tagprefix = "asp101samps" tagname = "sometext"
Src = "Basic. ascx" %>

<HTML>
<Head>
<Title> Asp. Net user control sample-Basic </title>
</Head>
<Body bgcolor = "# ffffff">

<Asp101samps: sometext runat = "server"/>

</Body>
</Html>

This code outputs the standard HTML page, showing the text in the user control rather than the mark.
So how is it implemented? The key is the registration description. To register a control, you must first define three attributes:

Tagprefix
The namespace that defines the control location. With namespace restrictions, you can use the same name controls with different functions on the same web page.

Tagname
The name of the control to be used. The control name in the same namespace is unique. The control name generally indicates the function of the control.

SRC
Point to the resource file of the control. Resource files use virtual paths ("control. ascx" or "/path/control. ascx") and cannot use physical paths ("C:/path/control. ascx .").

After the control is registered, it can be used like other server controls. By defining the tagprefix and the tagname, you can use it like using the built-in control on the server. At the same time, it is also determined to use the server running (runat = "server") method. The following describes how to call a user control on a webpage:
<Tagprefix: tagname runat = "server"/>

Add attributes to the user control and assign values
Next I will add two attributes to the control, one is color and the other is text.

Properties. ascx
<Script language = "VB" runat = "server">
Public color as string = "black"
Public text as string = "this is a user control... really! "
</SCRIPT>

<P>
<Font color = "<% = color %>">
<% = Text %>
</Font>
</P>

In this way, you can use and change the color and text of the control. You can assign values during initialization and modify these two attributes dynamically.

You can call this control repeatedly on the same webpage and use different property values:
Properties. aspx
<% @ Page Language = "VB" %>
<% @ Register tagprefix = "asp101samps" tagname = "sometext"
Src = "properties. ascx" %>

<Script language = "VB" runat = "server">
Sub page_load (sender as object, e as eventargs)
Userctrl1.color = "green"
Userctrl1.text = "this control's properties were "_
& "Set programmatically! "
End sub
</SCRIPT>

<HTML>
<Head>
<Title> Asp. Net user control sample-properties </title>
</Head>
<Body bgcolor = "# ffffff">

<Asp101samps: sometext runat = "server"/>

<Asp101samps: sometext color = "red" runat = "server"/>

<Asp101samps: sometext text = "this is quite cool! "Runat =" server "/>

<Asp101samps: sometext color = "blue" text = "ain't it? "Runat =" server "/>

<Asp101samps: sometext id = "userctrl1" runat = "server"/>

</Body>
</Html>

Even better, can the user control have event handles?
User Controls can do almost anything. The following code demonstrates how the control triggers the page_load event. With the event handle, you do not need to write other maintenance code to control the operation of the control. Controls can trigger events by themselves.

The following code encapsulates an ASP Textbox Control. I hook the property of my control name with the content of textbox.

Events. ascx
<Script language = "VB" runat = "server">
Sub page_load (SRC as object, e as eventargs)
Dim strinitialtext as string = "Please enter a name! "

If page. ispostback then
If txtname. Text = strinitialtext
Txtname. Text = ""
End if
Else
Txtname. Text = strinitialtext
End if
End sub

Public property name as string
Get
Return txtname. Text
End get
Set
Txtname. Text = Value
End set
End Property
</SCRIPT>

Name: <asp: textbox id = "txtname" runat = "server"/>

<Asp: requiredfieldvalidator controltovalidate = "txtname"
Id = "valtxtname" display = "dynamic" runat = Server>
Please enter a name!
</ASP: requiredfieldvalidator>

Events. aspx
<% @ Page Language = "VB" clienttarget = "downlevel" %>
<% @ Register tagprefix = "asp101samps" tagname = "sometext"
Src = "properties. ascx" %>
<% @ Register tagprefix = "asp101samps" tagname = "textbox"
Src = "events. ascx" %>

<Script language = "VB" runat = "server">
Sub page_load (sender as object, e as eventargs)
Txtlabel. Text = ""

'The Textbox Control handles it's own stuff
'In it's own page_load event handler.
End sub

Sub btnsubmit_click (sender as object, e as eventargs)
'Sets the label to the textbox's text
Txtlabel. Text = txtname. Name

'I don't need to worry about validation since
'My user control does it for me.
End sub
</SCRIPT>

<HTML>
<Head>
<Title> Asp. Net user control sample-validation & events </title>
</Head>
<Body bgcolor = "# ffffff">

<Form runat = "server">

<Asp101samps: textbox id = "txtname" runat = "server"/>

<Br/>

<Asp: button id = "btnsubmit" onclick = "btnsubmit_click"
TEXT = "Submit" runat = "server"/>

</Form>

<Asp101samps: sometext id = "txtlabel" runat = "server"/>

</Body>
</Html>

This is a description of user controls and applications. Whether you think it is simple or not, it is certainly easier than using traditional ASP.

You can download the code below.

Http://www.ChinaOK.net/down/200204221838030.zip
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.