ASP. NET Summary-Web custom controls, asp. netweb controls
The ASP. NET Video introduces user controls and custom controls.
Let's talk about the obvious difference between the two.
- The extension of the user control is. aspx, and the extension of the custom control is. dll.
- When using a user control, you need to put the file in the application directory. When using a custom control, you can add it to the toolbox.
Here, I will mainly introduce how to create Web custom controls.
In VS2010, there is no Web control library. What should I do?
Look at the type of the custom control. dll. Have you thought of anything? Remember our DAL. dll, BLL. dll ......, In essence, they are the same thing.
Therefore, the first step is to create a class library.
Mark"Note::This class library.. NET Framework and your Web application.. NET Framework version is the same. Otherwise, when the custom control is added to the toolbox, the control is displayed in gray and unavailable.
Step 2: Add a class.
Step 3: Add reference -- System. Web
Step 4: Introduce the namespace in the class: System. Web and System. Web. UI
Step 5: Add attributes to the custom control. The following Code adds a Text attribute to the control.
Using System; using System. collections. generic; using System. linq; using System. text; using System. web; using System. web. UI; // HtmlTextWriter in this namespace using System. componentModel; namespace ClassLibrary1 {// <summary> // summary of WebCustomControl1 /// </summary> [DefaultProperty ("Text "), toolboxData ("<{0}: WebCustomControl1 runat = server> </{0}: WebCustomControl1>")] public class WebCustomControl1: System. web. UI. webControls. webControl {private string text; // <summary> // Text attributes // </summary> public string Text {get {return text ;} set {text = value ;}} /// <summary> /// present the specified HTML writer to the control /// </summary> /// <param name = "output"> HTML writer to be written </param> protected override void Render (HtmlTextWriter output) {this. text = "<font size = 18px> <B> Hello </B> </font>"; output. write (Text );}}}
Step 6: generate the class library.
Step 7: Create an ASP. NET Program, create a Web form, right-click in the blank area of the toolbox -- select item -- browse -- find the Bin folder under the Debug file under the class library folder you just created (such: classLibrary1 \ Debug \ Bin \ ClassLibrary1.dll)
Find the file and check it before it.
In this case, you should be able to see it in the toolbox. If you cannot see it, right-click it in the blank space and choose show all.
Drag the control directly to the design window,
It seems that it is also very easy to create controls by yourself!
How to use C # To write an aspnet web custom control
Don't inspire RowEditing. Write it manually.
<% @ Control Language = "C #" AutoEventWireup = "true" CodeFile = "WebUserControl. ascx. cs" Inherits = "WebUserControl" %>
<Asp: GridView ID = "gv" runat = "server" AutoGenerateColumns = "False" OnRowDataBound = "gv_RowDataBound">
<Columns>
<Asp: TemplateField>
<ItemTemplate>
<Asp: TextBox ID = "tb" runat = "server" OnTextChanged = "textbox?textchanged" AutoPostBack = "True"> </asp: TextBox>
</ItemTemplate>
</Asp: TemplateField>
</Columns>
</Asp: GridView>
Using System;
Using System. Data;
Using System. Configuration;
Using System. Collections;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. Web. UI. WebControls. WebParts;
Using System. Web. UI. HtmlControls;
Using System. Collections. Generic;
Public partial class WebUserControl: System. Web. UI. UserControl
{
Protected void Page_Load (object sender, EventArgs e)
{
If (! IsPostBack)
{
List <string> ss = new List <string> ();
For (int I = 0; I <10; I ++)
{
Ss. Add (I. ToString ());
}
Gv. DataSource = ss;
Gv. DataBind ();
}
}
Protected void TextBox1_TextChanged (object sender, EventArgs e)
{
String str = (TextBox) sender). Text;
}
Protected void gv_RowDataBound (object sender, GridViewRowEventArgs e)
{
If (e. Row... the remaining full text>
How to place custom controls in asp net on a web page
You can view the source code of the web page to find this problem. Html controls are the most commonly used controls in the asp era. They all exist on the client. when data is submitted, the values of these controls in the form are obtained and then transmitted to the server for processing, before this operation is submitted, some basic data verification and other functions can be performed on these controls on the client. Due to data verification on the client, the pressure on the server is relatively small. In short, since you have started to enter. net, do not use the old ideas and methods to implement functions, it is best to use web controls, but note that web controls in order to implement server-side code, almost every control needs to refresh the page and interact with the server. This increases the pressure on the server.