ASP. net2.0 webcontrol development-custom dropdownlist

Source: Internet
Author: User
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.

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.