1

I have the need for a dropdownlist with the U.S. states in it. I could need this dropdownlist on various pages. Instead of always having to create a listitem for each state, I want a simple control. I don't want a UserControl - I want to extend the existing dropdownlist control. I know I have done this before at a previous employer, but for the life of me I can't remember how to do it!

Can't I just do something like this for each state? If so, where?

MyBase.Items.Add(New ListItem("Illinois","IL"))

Any ideas out there?

Thanks

4

4 回答 4

2

All you have to do is create a new class and inherit from the appropriate control:

/// <summary>
/// </summary>
[DefaultProperty("DataTable"),
     ToolboxData("<{0}:ExtendedDropDownlist runat=server></{0}:ExtendedDropDownlist>")]
public class ExtendedDropDownList : DropDownList


    /// <summary>
    /// Render this control to the output 
    /// parameter specified.
    /// </summary>
    /// <param name="output"> The HTML writer to 
    /// write out to </param>
    protected override void Render(HtmlTextWriter output)
    {
        //output.Write(Text);
        base.Render(output);
    }

In the constructor just add the appropriate listbox items like you have.

You may need to put it in a different project and reference the dll. I think I remember something about that, but it's been a while since I have had to do it.

于 2009-02-26T19:04:55.513 回答
0

Extend DropDownList control and override OnLoad event and add your items like that :

protected override void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        this.Items.Add(new ListItem("Illinois","IL"));
    }
}
于 2009-02-26T19:11:05.920 回答
0
<ToolboxData("<{0}:StateDropDownList runat=server></{0}:StateDropDownList>")> _
Public Class StateDropDownList
    Inherits DropDownList

    Protected Overrides Sub RenderContents(ByVal output As HtmlTextWriter)
        output.Write(Text)
    End Sub

    Protected Overrides Sub RenderChildren(ByVal writer As System.Web.UI.HtmlTextWriter)
        MyBase.RenderChildren(writer)
    End Sub


    Private Sub LoadStates()
        MyBase.Items.Add(New ListItem("Alabama", "AL"))
        'Additional states removed for size
        MyBase.Items.Add(New ListItem("Wyoming", "WY"))
    End Sub

    Public Sub New()
        'tried does not work
        ' LoadStates()
    End Sub

    Private Sub StateDropDownList_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        'tried does not work
        ' LoadStates()
    End Sub


    Private Sub StateDropDownList_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'tried does not work
        'If Not Page.IsPostBack Then
        '    LoadStates()
        'End If
    End Sub

    Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
        MyBase.Render(writer)
    End Sub
End Class
于 2009-02-26T19:21:28.373 回答
0
public class DropDownListStates : System.Web.UI.WebControls.DropDownList
{
    protected override void CreateChildControls()
    {
        if (Items.Count > 0) return;

        Items.Add(new System.Web.UI.WebControls.ListItem("AL"));

        // other states here...

        Items.Add(new System.Web.UI.WebControls.ListItem("WY"));

    }
}
于 2009-02-26T19:31:58.317 回答