Generally, when writing modules, some modules can be used repeatedly on different pages. It is really annoying to repeatedly write the same large number of identical control code. Although copying and pasting can be quickly solved, however, I personally do not feel very good. It is a pleasure to combine the controls of these modules into a control for easy use!
Recently I have read about the control development method. I still understand the basic process, mainly using the. ascx file.
I am writing the user login control here, not the one that comes with. net!
Let's take a look at the following:
Here is my solution:
Now we are developing controls:
1. Create a web project and create a new user control file (. ascx file)
The Code is as follows:
Loginon. ascx
<% @ Control Language = "C #" autoeventwireup = "true" codebehind = "loginon. ascx. CS "inherits =" usercontroltry. loginon "%> <SCRIPT runat =" server "> Public String username {set {tbusername. TEXT = value;} get {return tbusername. text ;}</SCRIPT> <div> <asp: Label id = "lbusername" runat = "server" text = "username: "width =" 100 "> </ASP: Label> <asp: textbox id =" tbusername "runat =" server "> </ASP: textbox> </div> <br/> <div> <asp: Label id = "lbpassword" runat = "server" text = "password: "width =" 100 "> </ASP: Label> <asp: textbox id =" tbpassword "runat =" server "textmode =" password "> </ASP: textbox> </div> <br/> <div> <asp: button id = "btnloginon" runat = "server" text = "login" Height = "30" width = "60"/> <asp: button id = "btnreset" runat = "server" text = "reset" Height = "30" width = "60" onclick = "btnreset_click"/> </div>
2. Create a new webpage file (. aspx file) and apply the control. Is it easy!
Touseucontrol_loginon.aspx
<% @ Page Language = "C #" autoeventwireup = "true" codebehind = "touseucontrol_loginon.aspx.cs" inherits = "usercontroltry. touseucontrol_loginon" %> <! -- If the user control is not registered in Web. config, you must use register to register it in the webpage file (. aspx file. --> <% -- <% @ Register tagprefix = "YCL" tagname = "loginon" src = "~ /Loginon. ascx "%> -- %> <! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <HTML xmlns = "http://www.w3.org/1999/xhtml">
3. If you do not want to always register your own web control in the web control, you must register it in the web. config configuration file.
Web. config
<? XML version = "1.0" encoding = "UTF-8"?> <! -- Access the http://go.microsoft.com/fwlink/ for detailed messages about how to configure ASP. NET applications? Linkid = 169433 --> <configuration> <system. Web> <pages> <controls> <! -- If you register a user control in Web. config, you do not need to register the control using register in the webpage file (. aspx file. --> <! -- The user control file (. ascx file) and webpage file cannot be in the same directory. Otherwise, an error similar to the following will be reported --> <! -- The "/touseucontrol_loginon.aspx" Page cannot use the user control "/loginon. ascx" because the control has been registered in Web. config and is located in the same directory as the page. --> <Add tagprefix = "YCL" tagname = "loginon" src = "~ /Controls/loginon. ascx "/> </controls> </pages> <compilation DEBUG =" true "targetframework =" 4.0 "/> </system. web> </configuration>
Source code download
PS: user control development-I started to see the control development method here. Although the article was earlier, the knowledge is permanent.