Reputation: 33
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
Upvotes: 3
Views: 2640
Reputation: 10754
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.
Upvotes: 6