Differences between user controls and custom server controls

Source: Internet
Author: User
Tags md5 hash

I. The main difference between user controls and custom controls lies in the ease of creativity and ease of use during design.
User Controls are easy to create and provide limited support for users using visual design tools. Each application requires a separate copy of the control and cannot be added to the Visual Studio toolbox. This is suitable for static layout, they may be inconvenient to use in advanced solutions.
Custom server controls are compiled code, which makes them easy to use but difficult to create. It provides users with full visual design tool support. You can add a single copy of the control only in the Global Assembly Cache to the toolbox in Visual Studio for dynamic layout.
If your control contains a large number of static la S, the user control may be a better choice. If your control is mainly generated dynamically, the custom control is a better choice.

II. (1) Overview of user controls and Custom Controls
User Control: the extension is *. ascx, followed *. aspx is similar in structure. It refers to the function block loaded in the page. However, a user control cannot run as a page separately and must be embedded in *. used in the ASPX page or other user controls.
The custom control is similar to htmlcontrol or webcontrol. After compilation, you can add the reference to the toolbar and drag it with your mouse.
 
(2) Use
In a large system, sometimes only a few *. aspx pages are allowed, and the rest are made into *. ascx pages,
This enhances the coupling between pages. A user control *. ascx serves as an independent functional block.
Custom Controls are compiled and can be directly used in the toolbox, just like TextBox and DataGrid.
In the designer, you can drag the mouse to the page.
Custom server controls are divided into two types:
1. Use the *. aspx code and *. cs code to compile and generate the DLL, and then add the reference to the toolbox.
It is generally used in WebForm.
2. Another method is to use only *. cs, compile the DLL, and add it to the toolbox.
It is generally used in WinForm.
To generate a custom control, follow these steps:
For example:
1. Drag a Button from the designer to the page,
Set a fixed value for the button size, color, or text. Save the file name as a. CS.
2. CSC/R: system. dll/T: Library/out:... \ .. \ A. dll A. CS
3. Right-click the blank area in the toolbox, right-click the menu, and select "add remove item ".
Add it to the toolbox. In this way, you can drag and use it like a normal button.

3. (1) The user control is represented by the. ascx file. Click "vveb user control" in "Add new project". It is not a compilation code and compilation is carried out dynamically along with the webpage.

 

The custom control is represented in the DLL file, which is the compilation code. Click "Web control library" in the "New Project" module. The user control will not appear in the toolbox, and the custom control will appear in the toolbox.

User Controls Support caching, while custom controls do not support caching.

User Controls provide limited support for users who use visual design, while custom controls provide comprehensive support.

User Controls can be reused in an application, but cannot be reused across applications.

Custom Users can use it across applications. It is placed in a central library called Global Assembly Cache so that all applications on that server can use it.

The user control cannot exist and be used independently. It requires the asp.net page as the container.

The following is an example of a login user control.

Public partial class Login: System. Web. UI. UserControl
{
Protected SqlConnection conn;
Protected SqlCommand cmd;
Protected SqlDataReader dr;

/// <Summary>
/// Txtusername is the property of the user control. To access it in aspx, it must be encapsulated as public first.
/// </Summary>
Public string Txtusername
{
Get {return this. TextBox1.Text ;}
Set {this. TextBox1.Text = value ;}
}

Protected void Page_Load (object sender, EventArgs e)
{

}

Protected void Button1_Click1 (object sender, EventArgs e)
{
String strname = this. TextBox1.Text. Trim ();
String strpwd = this. TextBox2.Text. Trim ();
// Use the hash algorithm to convert a specified password into an MD5 hash Password
// This. Response. Write (System. Web. Security. FormsAuthentication. HashPasswordForStoringInConfigFile (strpwd, "MD5 "));

Conn = new SqlConnection (System. Configuration. ConfigurationManager. ConnectionStrings ["DBuserConnectionString"]. ToString ());
Conn. open ();
Cmd = new sqlcommand ("select * From userdetails where username = @ username and userpwd = @ userpwd", Conn); // system. Web. configuration. formsauthpasswordformat. MD5
Cmd. Parameters. Add (New sqlparameter ("@ username", strname ));
// Note that if the password in the database is encrypted, MD5 encryption is required before assigning values based on the condition query.
Cmd. Parameters. Add (New sqlparameter ("@ userpwd", system. Web. Security. formsauthentication. hashpasswordforstoringinconfigfile (strpwd, "MD5 ")));
Dr = cmd. executereader (commandbehavior. closeconnection );
// Forward the next record
If (dr. Read ())
{
// Status management. In asp.net, the default status is saved using cookies.
// Save the user to the Cookie every time the user logs in successfully, and use a check box to check whether the user has been saved for a long time.
HttpCookie cookie = new HttpCookie ("username", strname );

If (this. CheckBox1.Checked)
{
// Set the cookie expiration date
Cookie. Expires = DateTime. Now. AddMonths (1 );
// Add the response to the cookies set
This. Response. Cookies. Add (cookie );
}
// Automatically jump to the page after 3 seconds, which is equivalent to the html mate tag
This. Response. AppendHeader ("refresh", "3; url = DataListDemo1.aspx ");
}
Else
{
This. Response. Write ("username or password are wrong! ");
}
Conn. Close ();
}
}

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.