Drop Down List not getting populated on partial post back

為{幸葍}努か 提交于 2019-12-12 02:57:09

问题


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.



来源:https://stackoverflow.com/questions/16436882/drop-down-list-not-getting-populated-on-partial-post-back

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!