ASP.net's user control experience

Source: Internet
Author: User
Tags html page resource tagname
Asp.net| Control | Experience ASP. NET's user control
Brief introduction
Asp. NET's server-side controls make Web development work easier and more powerful. We have described how to use server-side controls in asp.net pages. But what if the server does not have the required controls?

Of course, ASP. NET will not give you a false control. In fact, you can replace it with your own control. NET provides a control. This kind of control is the user control, also is the topic which this article discusses.

Writing the first user control
Some people think that knowing how to use a server-side control does not necessarily mean that it is easy to write a user control.

In fact, writing a basic user control (sometimes called a pagelets) and making asp.net pages use them like server-side controls is a simple thing to do. Here's a simple example:

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

This is a user control! See here, I think you will say I am not drunk, the mind is not clear. But this code is indeed a user control that is easy to use. Although this control does nothing, it is a good illustration of what a user control is. Things are not as complicated as you think. Note the suffix. ascx, which tells the Web page that this is a user control. It doesn't have any special meaning, it just doesn't allow IIS to execute the code directly.

Now let's create a user control that looks at the following example:

Basic.aspx
<%@ Page language= "VB"%>
<%@ Register tagprefix= "Asp101samps" tagname= "Sometext"
Src= "Basic.ascx"%>

<title>asp.net User Control sample-basic</title>
<body bgcolor= "#FFFFFF" >

<asp101samps:sometext runat= "Server"/>

</body>

This code outputs the standard HTML page, displaying the text in the user control instead of the tag.
So how does it happen? The key is in the registration (register) description. To register a control, you first define three properties:

TagPrefix
Defines the namespace of the control's location. With namespace constraints, you can use a different feature of the same control in the same Web page.

TagName
Point to the name of the control you are using. The name of the control in the same namespace is unique. Control names generally indicate the functionality of a control.

Src
The resource file that points to the control. The resource file uses a virtual path ("Control.ascx" or "/path/control.ascx") and cannot use the physical path ("C:\path\control.ascx.").

After the control is registered, it can be used just like any other server-side control. By defining the target prefix (TagPrefix) and the target name (TagName), you can use it just as you would with a server-side built-in control. The use of server-side running (runat= "Servers") is also determined. The following are the basic ways that Web pages invoke user controls:
<tagprefix:tagname runat= "Server"/>


Adding properties and assigning values to user controls
Next I 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>

This allows you to use and change the color and text of the control. You can assign values at initialization time, and you can modify these two properties dynamically.

You can call the control repeatedly in the same Web page and use a different property value:
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>

<title>asp.net User Control sample-properties</title>
<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>


Want to be better, can the user control have an event handle?
User controls can do almost anything. The following code demonstrates how the control triggers the Page_Load event. With an event handle, you don't have to write additional maintenance code to control the control's operation. Control can trigger events on its own.

In the following code, an ASP TextBox control is encapsulated. I hook my control Name property to the contents of the 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>

<title>asp.net User control Sample-validation & events</title>
<body bgcolor= "#FFFFFF" >

<form runat= "Server" >

<asp101samps:textbox id= "txtname" runat= "Server"/>

<br/>

<asp:button id= "Btnsubmit"
text= "Submit" runat= "Server"/>

</form>

<asp101samps:sometext id= "Txtlabel" runat= "Server"/>

</body>

This is a description of the user control and the application. Whether you think it's simple or not, it's certainly easier than using a traditional ASP.


Related Article

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.