DropDownList populate method affects postback behaviour?

I've started to rewrite code in an existing project and changed the way how two dropdownlists are populated. When using DataBind the SelectedValuehas the correct value in the Page_Load after a postback. But when using AddRange the SelectedValue is always the first value in the dropdownlist.

How can the way how the dropdownlists are filled affect the postback behaviour? Yes, there are java scripts in the page also, but I can't see how they could interfere with this.

The way that works (SelectedValue is correct in Page_Load):

protected void Page_Init(object sender, EventArgs e)
{
    IList<ListItem> list = new List<ListItem>();
    list.Add(new ListItem(Resources.Site.OriginalStructure, "0"));
    list.Add(new ListItem("5", "5"));
    list.Add(new ListItem("10", "10"));
    list.Add(new ListItem("15", "15"));
    list.Add(new ListItem("20", "20"));
    list.Add(new ListItem("25", "25"));

    DropDownList1.DataSource = list;
    DropDownList1.DataTextField = "Text";
    DropDownList1.DataValueField = "Value";
    DropDownList1.DataBind();

    DropDownList2.DataSource = list;
    DropDownList2.DataTextField = "Text";
    DropDownList2.DataValueField = "Value";
    DropDownList2.DataBind();   
}

The way that doesn't work (SelectedValue is always the first value in ddl in Page_Load):

protected void Page_Init(object sender, EventArgs e)
{
    var numberQuestionsPerPageDdlValues = new[]
    {
        new ListItem(Resources.Site.OriginalStructure, "0"),
        new ListItem("5", "5"),
        new ListItem("10", "10"),
        new ListItem("15", "15"),
        new ListItem("20", "20"),
        new ListItem("25", "25")
    };
    DropDownList1.Items.AddRange(numberQuestionsPerPageDdlValues);
    DropDownList2.Items.AddRange(numberQuestionsPerPageDdlValues);
}

Upvotes: 0

Views: 494

Answers (1)

Tim Schmelter
Tim Schmelter

Reputation: 460118

With "correct SelectedValue" I mean the value that is selected by the user on the page

You are binding the DropDownList to it's DataSource on every postback. You should do that only if(!Page.IsPostBack). Otherwise events will not be triggered and the SelectedValue of the user will be overridden.

I would also suggest to use page_load instead.

protected void Page_Load(object sender, EventArgs e)
{
    if(!Page.IsPostBack)
    {
        IList<ListItem> list = new List<ListItem>();
        list.Add(new ListItem(Resources.Site.OriginalStructure, "0"));
        list.Add(new ListItem("5", "5"));
        list.Add(new ListItem("10", "10"));
        list.Add(new ListItem("15", "15"));
        list.Add(new ListItem("20", "20"));
        list.Add(new ListItem("25", "25"));

        DropDownList1.DataSource = list;
        DropDownList1.DataTextField = "Text";
        DropDownList1.DataValueField = "Value";
        DropDownList1.DataBind();

        DropDownList2.DataSource = list;
        DropDownList2.DataTextField = "Text";
        DropDownList2.DataValueField = "Value";
        DropDownList2.DataBind(); 
    }   
}  

Upvotes: 1

Related Questions