自定义下拉列表,其中项目自动填充

发布于 2024-07-14 09:50:42 字数 297 浏览 6 评论 0原文

我需要一个包含美国各州的下拉列表。 我可能需要在各个页面上使用此下拉列表。 我不需要总是为每个状态创建一个列表项,而是想要一个简单的控件。 我不需要 UserControl - 我想扩展现有的下拉列表控件。 我知道我以前在以前的雇主那里做过这件事,但我一辈子都不记得该怎么做!

我不能为每个州做这样的事情吗? 如果是的话,在哪里?

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

有什么想法吗?

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

离线来电— 2024-07-21 09:50:42

您所要做的就是创建一个新类并从适当的控件继承:

/// <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);
    }

在构造函数中只需添加适当的列表框项即可。

您可能需要将其放在不同的项目中并引用 dll。 我想我记得一些关于那件事的事情,但自从我不得不这样做以来已经有一段时间了。

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.

撩起发的微风 2024-07-21 09:50:42

扩展 DropDownList 控件并覆盖 OnLoad 事件并添加您的项目,如下所示:

protected override void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        this.Items.Add(new ListItem("Illinois","IL"));
    }
}

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"));
    }
}
野稚 2024-07-21 09:50:42
<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
<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
素染倾城色 2024-07-21 09:50:42
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"));

    }
}
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"));

    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文