1. Create a webcontrollibrary Project
2. Write controls
Code Namespace Dropdownlisttemp
{
[Defaultproperty ( " Text " )]
[Toolboxdata ( " <{0}: webdropdownlist runat = Server> </{0}: webdropdownlist> " )]
Public Class Webdropdownlist: compositecontrol
{
//////////////////////////////////////// ///////////////////////////// //
// 1. Define attributes
//////////////////////////////////////// ///////////////////////////// //
[Bindable ( True )]
[Category ( " Appearance " )]
[Defaultvalue ( "" )]
[Localizable ( True )]
Public Model
{
Get
{
Return Modeltemp;
}
Set
{
Modeltemp = Value;
}
}
Public Enum Model
{
Sex, City
}
Public Model modeltemp;
//////////////////////////////////////// ///////////////////////////// //
// 2. Define controls
//////////////////////////////////////// ///////////////////////////// //
Private Label lbtemp;
Private Dropdownlist ddltemp;
//////////////////////////////////////// ///////////////////////////// //
// 3. Add controls
//////////////////////////////////////// ///////////////////////////// //
Protected Override Void Createchildcontrols ()
{
Controls. Clear ();
// Add label title
Lbtemp = New Label ();
Lbtemp. ID = " Lbtemp " ;
Lbtemp. Text = " Dropdownlist example " ;
Controls. Add (lbtemp );
//Add line feed
Controls. Add (NewLiteralcontrol ("<Br>"));
// Add drop-down list
Ddltemp = New Dropdownlist ();
Ddltemp. ID = " Ddltemp " ;
Ddltemp. Width = 200 ;
Listitem list = New Listitem ( " " , " -1 " );
Ddltemp. Items. Add (list );
Switch (Modeltemp)
{
Case Model. Sex:
Listitem listman = New Listitem ( " Male " , " 0 " );
Ddltemp. Items. Add (listman );
Listitem listwoman = New Listitem ( " Female " , " 1 " );
Ddltemp. Items. Add (listwoman );
Break ;
Case Model. City:
Listitem listbj = New Listitem ( " Beijing " , " 0 " );
Ddltemp. Items. Add (listbj );
Listitem listtj = New Listitem ( " Tianjin " , " 1 " );
Ddltemp. Items. Add (listtj );
Break ;
}
Controls. Add (ddltemp );
Base. Createchildcontrols ();
}
}
}
3. Use Controls for testing
Create a Web page, add the control DLL to the toolbox, drag the control to the leaf area, and select sex or city as the model property. The default value is sex.