Create and use ASP. NET User Controls

Source: Internet
Author: User

User Controls are the embodiment of code reusability.

A user control is a simple ASP. NET page, but it is included in another ASP. NET page. The user control file has the following features:

1. The extension is. ascx.

2. the user Control does not contain the "@ page" command, but contains the "@ Control" command, which defines the configuration and its attributes.

3. user controls cannot be run as independent files, but must be added to the Asp.net page just like the control.

4. the user control does not contain html, body, or form elements. These elements are not allowed in the host.

As follows:

Copy to ClipboardReference: [www.bkjia.com] <% @ Control Language = "C #" AutoEventWireup = "true" CodeBehind = "Sample. ascx. cs "Inherits =" ASP. NET_3._5.UC. sample "%>
<Asp: Label ID = "ColumnName" runat = "server" asp: Label>
<Asp: TextBox ID = "Condition" runat = "server"> </asp: TextBox>
<Asp: Button ID = "Search" runat = "server" Text = "Search"/>

Create a user control for data search:

1. Add a Sample. ascx file.

2. Drag a Lable control from the toolbox and set the ID to ColumnName.

3. Drag a TextBox Control from the toolbox and set its property ID to Condition,

4. Drag a Button control from the toolbox and set its attribute ID to Search and Text to Search.

Check the "Source" file as follows:

Copy to ClipboardReference: [www.bkjia.com] <% @ Control Language = "C #" AutoEventWireup = "true" CodeBehind = "Sample. ascx. cs "Inherits =" ASP. NET_3._5.UC. sample "%>
<Asp: Label ID = "ColumnName" runat = "server" asp: Label>
<Asp: TextBox ID = "Condition" runat = "server"> </asp: TextBox>
<Asp: Button ID = "Search" runat = "server" Text = "Search"/>

5. Open the Search.ascx.cn file to view the post code and define the following attributes:

Copy to ClipboardReference content: [www.bkjia.com] public string lableText {get; set;} // you are prompted to enter the query conditions.
Public string connectionString {get; set;} // connect to the database
Public GridView resultGridView {get; set;} // The GridView control to be filled
Public string tableName {get; set;} // name of the data table to be queried in the database
Public string columnCondition {get; set;} // which one to query
Public string errorMessage {get; set;} // error message

6. Define a function, SearchResult (). The function will query data based on the query conditions entered by the user and return the dataset. The function uses the database access knowledge. The Code is as follows:

Copy to ClipboardReference: [www.bkjia.com] private DataTable SearchResult (){
System. Data. OleDb. OleDbConnection conn = new System. Data. OleDb. OleDbConnection (connectionString );
String strsql = "select * from" + tableName + "where" + columnCondition + "like '%" + this. Condition. Text. ToString () + "% '";
Conn. Open ();
System. Data. OleDb. OleDbDataAdapter ada = new System. Data. OleDb. OleDbDataAdapter (strsql, conn );
System. Data. DataTable dataTable = new DataTable ();
Ada. Fill (dataTable );
Conn. Close ();
Return dataTable;
}

7. Open the Search. ascx file and double-click the Search button. A button event is generated in the Search. ascx. cs file, which binds data to the GridView.

Copy to ClipboardReference: [www.bkjia.com] protected void Search_Click (object sender, EventArgs e)
{
ResultGridView. DataSource = SearchResult (). DefaultView;
ResultGridView. DataBind ();
}

8. Add the code for initializing the ColumnName tag to the Page_Load event,

Copy to ClipboardReference: [www.bkjia.com] protected void Page_Load (object sender, EventArgs e)
{
This. ColumnName. Text = this. lableText;
}

  • 2 pages in total:
  • Previous Page
  • 1
  • 2
  • Next Page

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.