自定义 WebControls.DropDownList ViewState

发布于 2024-12-04 11:36:24 字数 793 浏览 1 评论 0原文

我正在尝试创建一个自定义 Web 控件,它只是一个简单的 DropDownList,它预加载了美国的所有州。它在大部分情况下都可以工作,但是在回发后控件将恢复到其初始状态。 (我可以在回发中获取所选值)。为了使列表在回发后保留其所选项目,我需要实施什么?

这是我的控件类:

[DefaultProperty("Text")]
[ToolboxData("<{0}:StateDropDownList runat=server></{0}:StateDropDownList>")]
public class StateDropDownList : DropDownList
{
    private void Populatedropdown()
    {
        this.Items.Clear();
        this.Items.Add(new ListItem("Alabama", "AL"));
        /* the rest .... */
        this.Items.Add(new ListItem("Wyoming", "WY"));

    }

    protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);
        Populatedropdown();
    }
}

编辑

这不是从数据源反弹控件的问题。此自定义控件的目标是避免不断绑定状态表中的下拉列表的需要。我试图解决的问题是如何在回发时维护此自定义下拉列表的选定值?

I'm trying to create a custom web control that's just a simple DropDownList which is preloaded with all the states in the US. It's working for the most part, however after post back the control reverts to its initial state. (I am able to get the selected value in the post back). What will I need to implement in order for the list to retain it's selected item after post back?

Here's my control class:

[DefaultProperty("Text")]
[ToolboxData("<{0}:StateDropDownList runat=server></{0}:StateDropDownList>")]
public class StateDropDownList : DropDownList
{
    private void Populatedropdown()
    {
        this.Items.Clear();
        this.Items.Add(new ListItem("Alabama", "AL"));
        /* the rest .... */
        this.Items.Add(new ListItem("Wyoming", "WY"));

    }

    protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);
        Populatedropdown();
    }
}

EDIT

This is not an issue with the control being rebound from a datasource. The goal of this custom control is to avoid the need to constantly bind drop down lists from a state table. The problem I'm trying to resolve is how do I maintain the selected value of this custom dropdown on postback?

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

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

发布评论

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

评论(3

彻夜缠绵 2024-12-11 11:36:24
private void Populatedropdown()
{
    if(this.Items.Count == 0)
    {
      this.Items.Clear();
      this.Items.Add(new ListItem("Alabama", "AL"));
      /* the rest .... */
      this.Items.Add(new ListItem("Wyoming", "WY"));
    }
}
private void Populatedropdown()
{
    if(this.Items.Count == 0)
    {
      this.Items.Clear();
      this.Items.Add(new ListItem("Alabama", "AL"));
      /* the rest .... */
      this.Items.Add(new ListItem("Wyoming", "WY"));
    }
}
可爱暴击 2024-12-11 11:36:24

您将 ListItems 添加到 控制生命周期

将您的 Populatedropdown 调用移至 OnInit 方法中,您的问题就会消失。

You are adding the ListItems to late in the control life cycle.

Move your Populatedropdown call into the OnInit method and your problem should go away.

萌梦深 2024-12-11 11:36:24

你在做这个吗?

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!Page.IsPostBack)
        {
            //only bind the control when the page is first loaded
            myControl.DataBind();
        } 
    }

Are you doing this?

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!Page.IsPostBack)
        {
            //only bind the control when the page is first loaded
            myControl.DataBind();
        } 
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文