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.
Related
I have a List (named actors) of "Actors", a custom class to be displayed in a ListBox (named listBoxActors) in C#. The user will be able to click an item to highlight and select it, after which they can press a button that Removes that Actor from the list. Here is my code for the button when it is pressed:
Actor current = (Actor) listBoxActors.SelectedItem;
actors.Remove(current);
listBoxActors.DataSource = null;
listBoxActors.DataSource = actors;
However, even after pressing the button, the Actor still displays in the list box as if it had never been removed at all. Setting the DataSource to null and back to the actors list should refresh it (works fine for that purpose when I add actors), but the list remains the same. What should I add/remove? What am I doing wrong?
You can simply use Remove function:
listBoxActors.Items.Remove(listBoxActors.SelectedItem);
Hope it helps!!
I have an asp page with a GridView control. One of the columns in the grid view is a dropdownlist. the drop down list are filled using the RowDataBound event on the page load event. However, one of the options in the list, when selected, will popup a dialog that will allow the user to add a new item to the list, and that newly added item becomes the selected item for that drop down. This part I have working fine, but I want to be able to add the new item to the drop down list for every row in the grid. How do I, in a way, re bind the drop down list in all rows after the page has loaded without having to loop through each field?
If all the DDL's have a shared DataSource then it should just be a matter of forcing a postback after adding the new listitem. But how are you adding the new listitem?
If your popup is simply adding a new html <option> tag to an html <select> tag, this is not the same thing as updating the DDL DataSource.
For all the DDL's to show the new listitem your popup would have to perform a postback that updates the DDL datasource(s) and all the DDL controls in the GridView would need to be rebound.
This would happen automatically on a postback assuming that the DDL controls are connected to their datasource via the DataSourceID property. If you are making use of the DataSource property then you will have to call DataBind() explicitly for each DDL control, which I think you said you are doing in the GridView RowDataBound event.
Alternatively you could loop through all your ddl controls clientside and add the same item to each control, but you would lose all those changes the next time the page refreshed and I don't think that's what you want.
I have a page with a dropdownlist, gridview, a button (with c#, asp.net).
Whenever I change the selecteditem in the ddList and click on the button my gridview is populated with the data related to the selecteditem and when I click on any cell (cells contain the data with hyperklinks to a low level page).
When this high level page loads with the ddlselected value from a more hig level page it load the data related to the selected item but after I change the selecteditem in ddlist and click on the button it loads the new data and when I clik on the hyperlink in the cell data it redirects me to a new low level page and after that when i click on the back button (in the browser iexplorer), i am redirected to the previous page but it always loses that ddlist latest selected item and displays the old ddlist select item's data.
I am generating the breadcrumbs using hyperlinks with Javascript: history.go(-1).
I believe the cache on the client is not updated when I change the ddlist selected item. but during the debugging when I check in the immediate window I see that it always updates the ddlist selecteditem.
Any help would be appreciated a lot.
thank you.
ASP.NET using C#:
I've got a DropdownList and a Button.
In Page_Load I fill the DropdownList manually via SQL-Query with some items from the database.
After click on the button I want to Alert the selected item of the list but every time the first item alerts instead of the selected item.
So simple, but what am I doing wrong?
Without your code it's hard to say, but I'm guessing on your page load, your reloading your dropdownlist, which is removing the selected item, and giving you the first item every time.
If this is the case, check for the request being a postback, and don't repopulate the drop down list.
when click on button happen postback and run page_load and fill dropdown list agin then show first item that selected.
if (!IsPostBack)
{
//fill the dropdownlist
}
Let's say I have a DropDownList of product categories and a ListView of products based on the category selection in the DropDownList. When a user visits the page, there is a possibility of conconrency issue as new product belonging to a new category may be added to the inventory as the user is browsing.
When the user selects a different category to view(a SelectedIndexChanged event) and causes a postback, I want the DropDownList to update the list of categories to include the new category being concurrently added and at the same time still able to make a change of selected index. The DropDownList does not seem to support this updating while postback. So how do I handle this concurrent issue?
You need to bind and rebind regardless of checking postback dropdown in Page_Init event. It will not cause any change in selected index of the dropdown and you can continue your work properly, the page init method called before loading view state. means your dropdown index will be selected after this method and you can access it in dropdown_selectedIndexChanged method.
For more information on ASP.Net page life cycle
Edit 1: have a look on sample code
protected void Page_Init(object sender, EventArgs e)
{
// here you bind your dropdown
// don't check IsPostBack
DropdownList1.DataSource = db.GetCategories();
DropdownList1.DataTextField = "TextField";
DropdownList1.DataValueField = "ValueField";
DropdownList1.DataBind();
}
ASP.Net loads controls viewstate after Page_Init and before Page_Load event, so DropDownList1's selectedIndex will not be affected, and you got desired results.
just check if the page-call is a postback with IsPostBack and then call dataBind() on your dropdownlist
Of course you can very well change the data source after the post-back. You can even refresh the data in it by calling DataBind() method on the DropDownListbox.
Another way of doing this if you can't bind in Page_Init for some reason (as in my situation):
var selected = ddList.SelectedValue;
ddList.DataSource = DBHelper.GetCategories();
ddList.DataBind();
ddList.SelectedValue = selected;
You'll still need to handle situation when selected category was deleted.