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
Related
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.
When using javascript/ajax to repopulate the options in ComboBox B based on the selection in ComboBox A, selecting an item in B with an index > original set of indexes causes the error:
"'...' has a SelectedIndex which is invalid because it does not exist in the list of items.
Parameter name: value "
Evidently comboboxes still link back to their original datasource regardless of what javascript does, so there are issues with it thinking item 1 is x when it's really y, or item 2 doesnt exist. At least that's what I'm getting out of similar posts. What i'm not getting is a solution, tho...
If your ComboBox B was originally populated by databinding, your ViewState is going to reflect that, and your ComboBox B is going to be repopulated from the information in ViewState on postback. So your SelectedIndex of greater than the original number of items is going to confuse the heck out of it.
(The contents of ComboBoxes don't post back: just the selected item. So it's not going to know anything about what you did client-side.)
Possible options include: using an UpdatePanel to update your combo boxes without refreshing the whole page, or making your ComboBox B an ordinary HTML select, populating it exclusively from the client side (through Ajax?) and fetching its value from the Forms collection on the server side after postback.
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
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
Based on my dataset row value , my drop drown will focus the particular list item in page load event.
ddparty.SelectedIndex = ddparty.Items.IndexOf(ddparty.Items.FindByValue(ds.Tables[2].Rows[0][1].ToString()));
i try the above code its not working.
Setting the property:
mydropdown.SelectedItem
or
mydropdown.SelectedValue
Make sure you do this AFTER databinding the dropdown control.
Don't know if you've set your DataValueField to the datatable's second column. On the other hand if you've set your DataTextField to it, try FindByText instead of FindByValue.
As a side note, try to quickwatch in the debugger, the value of the index you get.