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