开发者

Extending a DropDownList control

开发者 https://www.devze.com 2022-12-27 01:25 出处:网络
I have a rather large application that has literally a开发者_如何转开发 hundred DDLs with Yes / No ListItems. In an attempt to save myself some time, I created a custom control that extends the standa

I have a rather large application that has literally a开发者_如何转开发 hundred DDLs with Yes / No ListItems. In an attempt to save myself some time, I created a custom control that extends the standard DDL.

It all seems to work fine but I am having some issues when assigning the SelectedValue property in code where the selected value does not seem to have an affect on the control. I wonder if I should be adding my items during Init or PagePreLoad? Should I be calling base.OnInit before or after I add the list items? This mostly works but not 100%. (v3.5)

public class YesNoDropDownList : DropDownList
{
    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);

        if (!Page.IsPostBack)
        {
            base.Items.Add(new ListItem("Yes", "YES"));
            base.Items.Add(new ListItem("No", "NO"));
        }
    }
}

I think the issue is that if I load ListItems in Init, that is before viewstate is established and the ListItems are lost on postback. If I load them in OnLoad, that is after SelectedValue is applied and if I am setting SelectedValue, the selection is lost. My solution was wire up the Page InitComplete event in the OnInit override. This works but I am not sure that it is the best solution.

So, either Page_InitComplete as detailed below or OnInit but I have to load the items every time. Thoughts?

protected override void OnInit(EventArgs e)
{
    this.Page.InitComplete += new EventHandler(Page_InitComplete);

    base.OnInit(e);
}

private void Page_InitComplete(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        base.Items.Add(new ListItem("Yes", "YES"));
        base.Items.Add(new ListItem("No", "NO"));
    }
}


I would suggest adding the items in OnInit instead of on PreLoad, that way your control starts it's life with the state and whatnot properly set. Relevant bit of documentation (emphasis mine)

Raised after all controls have been initialized and any skin settings have been applied. The Init event of individual controls occurs before the Init event of the page.
Use this event to read or initialize control properties.

ViewState tracking also gets turned on between Init and PreLoad so it would be better if the control had all of it's options created before.

The code for SelectedValue is (thank you reflector):

    set
    {
        if (this.Items.Count != 0)
        {
            if ((value == null) || (base.DesignMode && (value.Length == 0)))
            {
                this.ClearSelection();
                return;
            }
            ListItem item = this.Items.FindByValue(value);
            if ((((this.Page != null) && this.Page.IsPostBack) && this._stateLoaded) && (item == null))
            {
                throw new ArgumentOutOfRangeException("value", SR.GetString("ListControl_SelectionOutOfRange", new object[] { this.ID, "SelectedValue" }));
            }
            if (item != null)
            {
                this.ClearSelection();
                item.Selected = true;
            }
        }
        this.cachedSelectedValue = value;
    }

If you try to set the SelectedValue before the list of items is populated, it's basically a no-op.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号