Creation and use of custom controls in ASP.NET
Creating and using custom controls in ASP.NET brings flexibility for our writing programs and protects the copyright of the program source code. The idea is: establish a CS file, the control created in this file must inherit the System.Web.ui.Control class, which can create properties, methods, and events for the control. Then compile into an assembly, put it in a bin directory, and how to use it.
This article describes the creation and use of custom controls in this article. The program mainly implements access to table Employees in SQLServer2000, which can be arranged or designed in an ascending or descending order of any field in the table.
The following is a detailed creation process:
1. Create a DataSetClass.cs class file and enter the following:
Using system;
Using system.Web;
Using system.Web.ui;
Using system.data;
Using system.data.sqlclient;
Using system.reflection;
[Assembly: AssemblyTitle ("WEB Control for Data")]
[assembly: assemblyDescription ("You can sort any data returned")]
[Assembly: AssemblyConfiguration ("No Configuration"]]
[Assembly: AssemblyCompany ("ACCP")]]]
[Assembly: AssemblyProduct ("DatasetClass")]]]
[Assembly: AssemblyCopyright ("Li Zanhong")]
[assmbly: assemblytrademark ("")]
[assmbly: assemblyculture ("")]
[Assembly: AssemblyVersion ("1.1.33.222")]]
Namespace Teachshow.Charpter9.Returndataset
{
///
// The field to be sorted
/// summary>
Public Enum SortType
{
Employeeid,
Lastname,
Firstname,
Title,
TitleOfcourtesy,
Birthdate
}
///
/// sort mode, liter or descending order
/// summary>
Public Enum SortStyle
{
DESC,
ASC
}
///
/// DataSetClass's summary description.
/// summary>
Public Class DatasetClass: Control
{
Private SortType psorttype; // indicates the fields in the table.
Private SortStyle PsortStyle; // Indicates that the way is rising or drop.
Public SortType SortType
{
get
{
Return this.psorttype;
}
set
{
this.psosttype = value;
}
}
Public SortStyle SortStyle
{
get
{
Return this.psortstyle;
}
set
{
this.psortstyle = value;
}
}
Public DataSetClass ()
{
//
// TODO: Add constructor logic here
//
This.psorttype = sorttype.employeid; this.psortstyle = SortStyle.Desc;
}
///
/// Get record set
/// summary>
///
Public DataSet getData ()
{
String straType = NULL;
String strsty = null;
String SQL;
Switch (psorttype)
{
Case SortType.employeeiD:
Strtype = "EmployeeID";
Break;
Case SortType.birthdate:
Strtype = "birthdate";
Break;
Case SortType.FirstName:
Strtype = "firstname";
Break;
Case SortType.lastname:
Strtype = "lastname";
Break;
Case SortType.title:
Strtype = "Title";
Break;
Case SortType.TitleOfCourtesy:
Strtype = "TitleOfcourteesy";
Break;
DEFAULT:
Strtype = "undefine";
Break;
}
Switch (psortstyle)
{
Case SortStyle.asc:
strStyle = "ASC";
Break;
Case SortStyle.Desc:
strStyle = "DESC";
Break;
DEFAULT:
strStyle = "undefine";
Break;
}
SQL = "SELECT * from Employees Order by" strtype " strStyle;
SqlConnection Con = New SqlConnection ("Server = Accp-lzh; UID = SA; PWD = Sasa; Database = Northwind");
SQLCommand cmd = con.createcommand ();
cmd.commandtype = commandtype.text;
cmd.comMandText = SQL;
SqlDataAdapter ada = new sqldataadapter ();
Ada.selectcommand = cmd;
DataSet DS = New Dataset ();
C.Open ();
Ada.fill (DS, "Employees");
C. close ();
Return DS;
}
}
}
2. After saving, compile this file as the assembly, compile command to: CSC / T: library /r :system.dll ,system.web.dll c: /inetpub/wwroot/teachshow/charpter9/ReturndataSet/datasetClass.cs. Where: c: /inetpub/wwwroot/teachshow/charpter9/retataSet/DataSetClass.cs is the path where the file is located. If the compilation is successful, a DataSetClass.dll file will be generated.
3. Create a new web form to join DataSetClass.dl into the toolbox. The method is: Open the toolbox, find the Web Form tab, click Right click, select Add / Delete Item, click the "Browse" button, find the DataSetClass.dll file, and then automatically add it to the tab. Control name is class name: datasetClass4, start using this control: Draw a list of two lines, put a DataSetClass on a row, and put a DataGrid control to display data.
5, the code is as follows:
Test.aspx content:
<% @ Register tagprefix = "cc2" namespace = "teachshow.charpter9.ReturndataSet" Assembly = "DatasetClass"%>
<% @ Page language = "c #" codebehind = "test.aspx.cs" autoeventwireup = "false" inherits = "teachshow.charpter9.returndataset.test"%>
HEAD>