简体   繁体   中英

Reload a dropdown on postback

I am populating a dropdown on page load and the user selects an item, click a button (to create a new site in sharepoint) I then want this new site to be shown in the dropdown but I can't get it to work properly. Either the items are duplicated or the selected value is null.

so on page_load I have

getSites();

if the page is a postback I want to reload the dropdown with the new item

if(Page.IsPostBack)
{
    getSites();
}

but this of course duplicates all the values so I tried

if(Page.IsPostBack)
{
      ddlSites.Items.Clear();
      getSites();
}

But even if the site is reloaded with all the items and I select one the value is null, why is that and what should I do to fix this problem?

Cheers

Your code in Page_Load executes before your button click, so when you clear the items, there is no selected item when you get to your button click event, hence why it is null.

You could add ddlSites.Items.Clear() to your getSites() method and then call getSites() after your button click.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM