asp.net using c# DropDownList item not selected - c#

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
}

Related

ComboBox Event works with ListBox but not with GridView

The function should be simple:
I select an item, which is added to a list, then i manage that item using the list.
As first try i put some buttons next to the listbox and it worked fine.
But the objective is to add two buttons in each row, so i replaced the listbox with a gridview.
What's the problem? The event SelectedIndexChanged of the ComboBox (which is used to add the item to the list) does trigger once but not the second time.
The page correctly goes through page load, but the combobox's event is ignored.
Code here:
https://pastebin.com/Bmg2qjTf
I think i must say the combobox is filled using an SQL: the first time the SQL Query is executed and applied to the ComboBox's DataSource and in a Session, on PostBacks instead it gets refilled thru the Session variable.
Solution: Adding a txtNumSpedizione.Text = "";
Troubleshooting:
I tried to make both (listbox and gridview) work at the same time and this way it worked, also the gridview was getting all the selections.
So i figured out the difference between before and after was in the Text field "reset".

ASP GridView and DropDownList

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.

keep radio button selected across postback

I have a gridview with radio buttons to select a single row.
The gridview is populated from a SQL DB according to a value selected from a drop down list.
What I want to do is keep the selected radio button when the user moves to a different category, so if he wants to move back via the DDL the value he selected previously is still selected.
What is the best way to do this?
The application will have users and I have read sessions could help me with this but I have no idea how it works!
Thanks in advance
I'm not sure really why you'd want to do it this way but you'd then save it in session this way:
String selectedValue = rblYourRadioButtons.SelectedValue;
Session["SelectedRadioButtonValue"] = selectedValue;
Then, on postback or whenever, on the Item Databound of the gridview you check if the current row Id matches the value stored in the session, if it does then select that value in the radiobutton list.
Personally I'd store the value in the DB rather than session.

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.

ASP.net disable refresh when dropdownlist is open

i have a webpage that loads data into a gridview and refreshes the gridview every few seconds. I do this via a asp:Timer which runs a C# function every few seconds to requery the database and databind the gridview.
I also have a few dropdown lists to filter data from the gridview. These dropdown lists get their data from the same dataset as the gridview (e.g. if the gridview shows the stats of all apples being plucked from trees, then the list may contain e.g. all distinct apple types). How i refresh these dropdown lists is again to requery the dataset and reset the selected index to be one selected at time of refresh. So this causes a problem where the timer is up when the dropdown list is open - the index on the dropdown list is selected and refreshes the gridview, the dropdown list also refreshes with the current selected index and closes.
So the question i have how to disable my timer refresh from going off while the dropdown lists are active - or maybe how do i do this better?
edit: forgot to mention that i'm using ajax / UpdatePanel for the refresh
The first thing you need to do is define some event to capture when the dropdown is open. I dont think there is one, but you could use focus() events ... maybe.
Then when the dropdown list is open, you need to disable the timer client-side. This article explains it (though using a checkbox)
http://weblogs.asp.net/aboschin/archive/2007/10/06/ajax-how-to-control-an-lt-asp-timer-gt-on-client-side.aspx

Categories