User Controls & Custom controls----. NET Re-learning

Source: Internet
Author: User
Tags md5 encryption md5 hash

Start learning. NET to now two years fast, began to learn Java also has the big six months, the middle does the project, starts again. Net of learning. Do it this time. NET project, the background code is not too much to knock, focus on the UI, this is the blog to write about the content, do the UI is the use of the control. The main UI for this time is to extract the public, abstract controls.

When you knock the code, you can't leave the control, that's the first time. NET after learning, the understanding of the control is very superficial, through this study, but also make up for the UI control awareness. Here's what this blog is about: User controls and custom controls. When it comes to these two controls, it is not strange for the children to be programmed, but when it comes to use, the difference between them, is it clear to the partners ?

Let's just compare these two controls:

first, easy to creativity and ease of use

The main difference is that they are easy to be creative and easy to use at design time.

1, the creation of

User controls are easy to create and provide limited support for users who use visual design tools-it can have a visual editing environment like editing a normal page, as well as an. aspx and. cs code file that contains the page part as a normal page.

Custom controls do not have visual editing, which is compiled code, which makes them easy to use but difficult to create. only handwritten code. Contains only a. cs code file.

2. Ease of Use

User Control development is convenient, custom control development is cumbersome, the use of user controls, each application requires a separate copy of the control, can not be added to the Visual Studio Toolbox, for static layout, They may not be easy to use in advanced scenarios.

Custom controls can be compiled into. DLL files, which can be used by other projects. The compilation can be loaded into the toolbox, just like Textbox,datagrid, which can be dragged to the page using the mouse in the designer . Custom controls are highly reusable and can be customized with personalized controls, and tasks that are frequently used and cannot be completed by existing controls are resolved.

3. Scope of Use

Custom control, which provides full visual design tool support to the consumer, requires only a single copy of the control in the global assembly cache, and can be added to the toolbox in Visual Studio for dynamic layout.
If your control contains a large number of static layouts, the user control might be a better choice.

4. Use

user Control (UserControl): extension *.ascx, similar to *.aspx in structure, refers to a function block loaded on a page, except that the user control cannot be run alone as a page and must be embedded in a *.aspx page or other user control. It requires an ASP. NET page as a container.
Custom controls, similar to HTMLControl or WebControl, can be added to the toolbar after compilation, directly using the mouse drag.

Ii. user Controls and custom controls

1), Custom server controls

It is also divided into two types:
1. One is to build the DLL after compiling with the *.aspx code and the *.cs code, and then add the reference to the Toolbox for use.
Generally used in WebForm.
2. Another is to use only *.cs implementation, and then compile the build DLL, added to the toolbox.
Generally used in WinForm.
Build steps:
Like what:
1. Drag a button from the designer to the page,
Set a fixed value for the button size, color, or text, save file name: A.cs
2. Csc/r:system.dll/t:library/out:. /.. /a.dll A.cs
3. Right-click on the left-hand side of the toolbox, pop-up the right-click menu, select "Add Remove Items", add the DLL you just generated to the toolbox, so you can drag and use like a normal button.

2) Then look at the user control.

The user control is represented by an. ascx file, and in Add New item, click Web User Control. It is not compiled code, and compilation is performed dynamically with the Web page.

There are some features that are checked from the Internet and have not been tested, as follows:
User controls support caching, while custom controls do not support caching;
User controls can be reused in one application, but not across applications.
Custom users can be used across applications, which are placed in a central library called the Global assembly cache, so that all applications on that server can use it.


Iii. Examples of user controls

This is someone else's example of a user control, for your reference only.

public partial class login:system.web.ui.usercontrol{    protected SqlConnection conn;     protected SqlCommand cmd;    protected SqlDataReader dr;     //<summary >   ///txtUserName is a property of the user control that must be encapsulated to be public before it can be accessed in ASPX    //</summary>     public  string txtusername    {        get {return this. TextBox1.Text; }        set {this. TextBox1.Text = value; }   }        protected void Page_Load (object sender, EventArgs e)  & nbsp;  {   }        protected void Button1_click1 (object sender, EventArgs e)     {        string strname = this. TextBox1.Text.Trim ();        string strpwd = this. TextBox2.Text.Trim ();       //Use hash algorithm to convert to a MD5 hash password based on the specified 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 [email  Protected] and [email protected] ", conn);//system.web.configuration.formsauthpasswordformat.md5         cmd. Parameters.Add (New SqlParameter ("@username", strname));       // Note that if the password in the database is encrypted, then the MD5 encryption is the         cmd before assigning the value according to the conditional query. Parameters.Add (New SqlParameter ("@userpwd", System.Web.Security.FormsAuthentication.HashPasswordForStoriNginconfigfile (Strpwd, "MD5")));        dr = cmd. ExecuteReader (commandbehavior.closeconnection);       //forward Next record          if (Dr. Read ())         {            //State management, the default state in ASP. is saved with a cookie            // Save the user to a cookie every time the user logs in, and use a check box to manage how long the user is saving the state              HttpCookie cookie = new HttpCookie ("username", strname);             if (this. checkbox1.checked)             {                //Set the expiration date of the cookie                  cookies. Expires = DaTeTime.Now.AddMonths (1);                //response added to the cookie collection                  this. RESPONSE.COOKIES.ADD (cookie);           }            //3 seconds after auto jump page, equivalent to the HTML mate tag              this. Response.appendheader ("Refresh", "3;url=datalistdemo1.aspx");       }         else        {             this. Response.Write ("Username or password is wrong!");        }        Conn. Close ();   }}

Summary: A recent MVC project, using HTML to make a public page, or half a page, so it appears to be a custom control, in fact, the user control has never been used.

Not just. Net, there should be a similar concept in Java, including other pages through the include, I'm not sure, the next Java can be turned over.

User Controls & Custom controls----. NET Re-learning

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.