Drop Down List not getting populated on partial po

2019-09-05 21:58发布

问题:

I am trying to populate a drop down list on partial post back, not sure why it's not working.

this works,

protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                 populatemyDropDownList();
            }

this doesn't work,

protected void Page_Load(object sender, EventArgs e)
            {
                if (IsPostBack)
                {
                     populatemyDropDownList();
                }

Scenario

*I click on button_1 in UpdatePanel_1, which then triggers a partial post back (no page refresh) and tries to populate DropDownList which is in UpdatePanel_2*

when I debug, I can see code behind method is triggering and going through this code but no gain, I think partial post back resets DropDownList ????

using (SqlDataSource sqlds = new SqlDataSource(ConnectionString(), SelectCommand()))
            {
                drop1.DataSource = sqlds;
                drop1.DataTextField = "UserName";
                drop1.DataBind();
            }

回答1:

You could use

ScriptManager.GetCurrent(Page).IsInAsyncPostBack

to check if you're in an asynchronous postback.

However, i would not rely your logic on postbacks(or !IsPostBack) and IsInAsyncPostBack. Instead i would use the correct events. In this case you want to handle button_1's click event to fill the DropDownList in UpdatePanel2.

Note that you should make UpdatePanel2's UpdateMode Conditional. Then you can call UpdatePanel2.Update() manually after you've filled the DropDownList.