适用于美国州/加拿大省份和国家的下拉列表






2.44/5 (16投票s)
2003年11月3日
2分钟阅读

132652

1787
本文讨论了继承(subclassing)DropDownList控件,以创建一个预加载了美国州和加拿大省份的DropDownList。

引言
我的任务是创建一个简单的DropDownList,其中包含美国州和加拿大省份,并且可以轻松地重用。要求是它应该完全自包含,并且应该像任何其他DropDownList一样运行。
因此,继承DropDownList组件,并在重载的OnInit事件中加载ItemList是最有意义的。
目录
此包包含两个控件,SDIddlStates和SDIddlCountries。本文包含来自SDIddlStates控件的代码片段。但是,SDIddlCountries控件几乎相同,只要你查看它就会发现。事实上,你可以很容易地将两者结合起来,并添加一个新的参数,用户可以在设计时设置该参数,以确定它是哪种类型。这将是一个很好的练习。
使用代码
要使用该代码,只需编译它。然后在Visual .NET IDE中,选择工具 | 添加/删除工具箱项...菜单选项,并浏览直到找到编译结果的DLL。然后,你应该能够将控件拖放到任何需要的Web Form上。
关注点
访问所需的类
代码需要使用某些类,因此我们必须获取它们的定义,如下所示
using System;
using System.Collections;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
继承DropDownList
我们需要做的下一件事是开始编写代码,声明新的类,我将其命名为SDIddlStates,如下所示
    public class SDIddlStates : DropDownList 
    {
    ...all of the code will go here...
    }
重写OnInit事件
为了预加载包含DropDownList控件的值和文本的ItemList,我编写了以下代码
    protected override void OnInit(EventArgs e)
    {
        // Create the DataSource that contains the data
        this.DataSource = CreateDataSource();
        
        // Tell the control what column in the DataSource contains the Text
        this.DataTextField = "StatesTextField";
        // Tell the control what column in the DataSource contains the Value
        this.DataValueField = "StatesValueField";
        // Bind the DataSource to the control
        this.DataBind();
        // Do whatever the control usually does OnInit
        base.OnInit(e);
    }
创建DataSource
为了创建在上述代码中使用的DataSource,我编写了以下代码
    protected ICollection CreateDataSource() 
    {
        // Create a table to store data for the DropDownList control.
        DataTable dt = new DataTable();
        
        // The first column of the DataSource contains the Text
        dt.Columns.Add(new DataColumn("StatesTextField", typeof(String)));
        // The second column of the DataSource contains the Value
        dt.Columns.Add(new DataColumn("StatesValueField", typeof(String)));
        // Populate the table with rows.
        dt.Rows.Add(CreateRow("0", "Choose a State/Province", dt));
        dt.Rows.Add(CreateRow("AL", "Alabama", dt));
        dt.Rows.Add(CreateRow("AK", "Alaska", dt));
        ...
        dt.Rows.Add(CreateRow("SK", "Saskatchewan", dt));
        dt.Rows.Add(CreateRow("YT", "Yukon Territories", dt));
        // Create a DataView from the DataTable to act as the 
        // DataSource for the DropDownList control.
        DataView dv = new DataView(dt);
        return dv;
    }
创建DataRow
为了创建在上述代码中使用的DataRow,我编写了以下代码
    protected DataRow CreateRow(String Value, String Text, DataTable dt)
    {
        // Create a DataRow using the DataTable defined in the 
        // CreateDataSource method.
        DataRow dr = dt.NewRow();
        // The first column of the DataSource contain the Text
        dr[0] = Text;
        // The second column of the DataSource contain the Value
        dr[1] = Value;
        return dr;
    }
将控件放入工具箱
为了能够从工具箱中拖放控件,我必须确保我能够首先将其放入工具箱。所以我编写了这段代码
    [
    ToolboxData("<{0}:SDIddlStates runat=server>")
    ]
请注意,工具箱中控件的名称必须与类名匹配。
历史
V1.01
- 删除了重复的省份。
