dropdownlist selectedvalue - c#

I've used a webservice to create a dropdownlist of countries and I'm trying to add the dropdownlist selection to the sql database, but when I do that by using ddlCountry.SelectedValue in my insert statement, only the first value in the dropdownlist is showing up in the table. Should I use onselectedindexchanged to somehow store the value? What code should I be using?

Try using ddlCountry.SelectedItem.Value instead.

You need to make sure you're not repopulating your dropdown from the web service on a postback. That will cancel out your selection.

If you are firing some sort of save function on button click's even then you can go ahead with ddl.SelectedItem.Text (if you wan't to store the text in your table) if you want to store the value part of the country list then go ahead with ddl.SelectedItem.Value
SelectedIndexChanged should be only used if you want to perform some action at the time when user selects an item in the drop down

Related

How to maintain state of drop down list on post back in asp.net VB

I have got a radio button list and based on selection of the radio button list , the drop-downs will populate. Important thing is here the radiobutton list is set to autopostback=true.
And also when i move to next page by button click, And when i come back. Drop down button not able to maintain state. It is losing values. It is important for me to maintain state until i reach the last page. How can i approach this problem. I have used sessions but was not successful. Could you tell me how to implement sessions.
hi #Newyork167 this will not work as he mentioned above that "based on selection of the radio button list , the drop-downs will populate." so you have to store the "RadioButtonList" Selected value and accordingly fillDropDownlist Values and set selected after returning back to the page.
Ok. As per my understanding you need to save a page state so that when you come back then you get the page in previous state where you left. So to do this you have two ways.
1. Before jumping to next page, store everything in session like "Radiobutton selected value", dropdown selected value and other settings if any.
2. Pass these values "Radiobutton selected value", "dropdown selected value" and other if any as the query string and when you coming back then read the same query string.
In either way when you will come back then you will have the previous data. In the page_load event, just check whether you have that data or not. If yes then populate your controls with previous data else populate your controls for first load.
Here is some link for your reference.
http://www.codeproject.com/Articles/5876/Passing-variables-between-pages-using-QueryString
Passing Session[] and Request[] to Methods in C#
If you want to use sessions, you could check the session variable with all of the values of the list
if(Session["selectedList"] != Null){
var check = Session["selectedList"].ToString();
foreach(ListItem item in yourList.Items){
if(item.Value.Equals(check))
// set it as selected
}
}
For storing, when you click the button
Session["selectedList"] = yourList.SelectedValue;
You could also use the indexes instead of the values. You can also create a session variable for each dropdown/radiobuttonlist and you just make a loop for each.
UPDATE
Thanks to Shekhar for pointing this out. You need to go through all of the lists that you want to store and save them with these loops, not just the dropdowns. Then, you need to restore the radio button values, rebind the dropdowns, and then set the selected item for each.

Creating and accessing control values dynamically

I have a DropDownList that gets populated from SQL on page load.
A user will select an item from the list and click a button.
Based upon the item selected, I need to fetch data from SQL. The results are then used to dynamically create a table with textboxes (for user input).
After filling out some textboxes, the user will click another button. I will need to loop through the dynamically created table and do some data processing with the values entered in the textboxes.
I'm having a hard time knowing when to create the dynamic table. If on Page_Init, how do I get the DropDownList selected value to pass to SQL? If on Page_Load, how am I going to re-create the the table with the user-entered values?
This is in C# .NET Framework 4.0 using Webforms
Found the solution.
I'm using Request.Form[ddl.UniqueID] to get the DDL's selected value.

Can values from dropdownlists in detailsview be updated without autopostback?

I have a details view that is in "insert Mode" so the user just sees blank spaces to enter values. I have two drop down lists and I wanted to have the second ddl change its value by what was selected in the first ddl. I tried setting ddl1 to a label so ddl2 would change when the label changed. The problem I am having now is that I need autopostback to update the value of the label, but selecting "autopostback" on the ddl1 makes my code throw a data binding error.
I was wondering if there was any way I could get around using autopostback and still update values selected in the first ddl to the label.
Thank you.
Try using AjaxControlToolkit. It has a feature to cascade ddlists. Use updatepanel as container for both of ddls so you can omit autopostback.
Your query is not completely clear. But if you want to change the dd2 value on change event of dd1, you can use the following code:
$("#<%= statusDDL.ClientID %>").val($("#<%= dd1.ClientID %>option:selected").text() );
It is not clear whether you want value or text property.
Also I am not 100% that this syntax will work. But obviously it can be done using this concept searching on net for your requirement

Getting a value in a textbox from a dropdown list

I have a textbox named textbox1 and a dropdownlist. Dropdownlist contains the list of branches and i want that when i select a branch from dropdown i want to get the respective branch code to be generated in a textbox from the Database or from c# coding. How will it be possible ?
Am I missing something, is it more complex that just putting an event handler on the dropdown so that when it's value changes it calls your method that can do whatever it needs to do to generate the string for the textbox?
Skipping passed the "database" portion of your question (and assuming your data isn't bound to the DropDownList)..
One way in ASP.NET, to accomplish this, I believe, is to have your code print DropDownList.SelectedValue's value in the textbox during an event.
Here is a link showing how to bind an event to DropDownList in jQuery

Dropdown list selected item always set to default value

I wish to pass values of the selected items into a database and however noticed that the selected item is not what is sent into the database you can see in the snap shot below.
during run time the following value is recorded.
Where did it all go wrong with the dropdown lists selected item?
Counting on your intelligence...thanks.
Put your call to load the dropdownlist in
if (!Page.IsPostBack)
{
//LoadDropdownListHere();
}
Try DBNull.Value instead of null.
Edit:
I think you need to specify the type using this overload: http://msdn.microsoft.com/en-us/library/cc491445.aspx
CSMDataSource.InsertParameters.Add("CaseID", DBType.Int32, CaseIDDropDownList.SelectedItem.Text);
I found the problem for the dropdownlist always selecting the first value. It appears the dropdown list is always rebinded anytime i click a button that requires a post pack as such the data tha was binded at page load re binds a gain before the selected item gets picked as such the default first value gets picked all the time. to solve my problem i first disabled enable auto postback on the dropdownlist and in the code behind that is my .cs file during the page load, where i first binded the data to the dropdown list, i used a condition like if(!ispostback) to bind my data. what this does is when the page loads first time the dropdownlist is binded and if i should select an item from the drop down list, the "auto post back" that i disabled earlier on keeps the selected item selected and when i click and button that requires a post to the server, the (!ispostback) prevents the dropdownlist to be binded again before selection. so in effect, the selection is done first and afterwards if the page should load anew, the drop down list is binded again.
i was in a bit of a rush while typing so please bare with my typo errors. do call in anytime for more clarification...peace

Categories

Resources