Not able to set dropdownlist at page load - c#

I am getting first time this kind of problem
I am setting value of dropdownlist at page load from dataset
But this is auto setting 0 index .....
My code :
ddlEbitda.SelectedValue = objDataset.Tables[0].Rows[0]["acq_ebitda"].ToString();
I checked at immediate windows ...
objDataset.Tables[0].Rows[0]["acq_ebitda"].ToString();
"Between 40-60 %" ( come value from data set is right)
ddlEbitda.SelectedValue
"Up to 10 Million Dollar " ( this is default value setting )
I tried many different code to solve this problem :
ddlEbitda.ClearSelection();
string Ebitda = objDataset.Tables[0].Rows[0]["acq_ebitda"].ToString();
ddlEbitda.Items.FindByValue(Ebitda).Selected = true;
string Ebitda = objDataset.Tables[0].Rows[0]["acq_ebitda"].ToString();
ddlEbitda.SelectedIndex = ddlEbitda.Items.IndexOf(ddlEbitda.Items.FindByValue(Ebitda));
But still not able to solve this problem ....truth is I am not able to understand what is problem .....

This should work
string value=objDataset.Tables[0].Rows[0]["acq_ebitda"].ToString();
ddlEbitda.Items.FindByValue(value).Selected = true;
But make sure when you are binding the dropdown with DataSource, at that time acq_ebitda should be DataValueField.
If it is DataTextField then you should try something like this
ddlEbitda.Items.FindByText(value).Selected = true;

Ensure that the DropDownList is populated/bound inside this code block in the Page_Load method
If (!IsPostBack)
{
}
and immediately after set the selected item.
ddlEbitda.SelectedValue

Make sure whether your dropdown list is correctly bound to the dataset and data is populated . specially make sure the value you are assigning is in the item list. you can easily check with quick watch in VS so you can make sure the value you are assigning is inside the dropdown items. because the way you are doing is OK

Related

Combobox.SelectedValue wont select the actual value

I have a problem that I'm struggling with.
I fill a combobox this way:
RDTA_cb_provincias.DataSource = provincias;
RDTA_cb_provincias.DisplayMember = IdiomaBD.ObjCI.ToString().Equals("eu-ES")
? "TTEXNOMBRPROV_EU"
: "TTEXNOMBRPROV_ES";
RDTA_cb_provincias.ValueMember = "TCODIDTERRITORIO";
There's some stuff depending on the language of the user, but i guess that's not important here.
Well, when i want to select depending on the value member i do this:
RDTA_cb_provincias.SelectedValue = provincia2.TCODIDTERRITORIO;
But for some reason it won't show up the actual item I want to show. While debugging i can see that RDTA_cb_provincias.SelectedValue matches provincia2.TCODIDTERRITORIO.
For example, I've set SelectedValue to 51, but when i see the SelectedItem inside the ComboBox once I've changed the value, It shows up an object that has 47 as its TCODIDTERRITORIO.
What can it be? Or how can I bypass this problem (Maybe iterating the whole ComboBox item list until i find the one i want to select?)
Thanks in advance!
You should use SelectedItem instead:
RDTA_cb_provincias.SelectedItem = provincia2;
Or you can use SelectedIndex as follows:
RDTA_cb_provincias.SelectedIndex = RDTA_cb_provincias.FindStringExact(provincia2.TCODIDTERRITORIO)

How to Remove the selected index of a DropDownList?

I have an ASP.Net web for that inserts values into a database, I want to reset the selected items in the dropdownList, how do I do it properly?? I know with textBoxes I can use TextBox1.Text = String.empty but it doesnt seem to work with the dropdownLists
If you want to clear all Items means you can use
aspDrop.Items.Clear(); // whill clear all the list items
If you want to change the selected index means you have to use
aspDrop.SelectedIndex = someIndex; // where index must be in the list
Or even you can use .Insert() method to insert an item to a particular index and make it as selected item.
For DropDownList it would be better to first add an empty string at first position of the DropDownList by using yourDropDownList.Items.Insert method and then select it. Something like this:
yourDropDownList.Items.Insert(0, new ListItem(string.Empty, string.Empty));
yourDropDownList.SelectedIndex = 0;
Well if you want to clear dropdownList fully so you can clear it's items source such in this way:
ddItems.Clear();
But if you only want to clear selected dropdownListItem than S.Akbari solution:
ddItems.Items.Insert(0, new ListItem(string.Empty, string.Empty));
ddItems.SelectedIndex = 0;
I guess, everyone has given answers. But remember one thing to do as follows:
AppendDataBoundItems="False"
Set it to false as this will cause the rebound data to be appended to the existing list which will not be cleared prior to binding. The above is applicable when data is bound to the DropDownList. The rest will be done clearing it.

Allow empty/no selection in DevExpress' LookUpEdit

I have a form containing a DevExpress LookUpEdit (Windows Forms) which is bound to a list of objects with several displayed properties. The EditValue property is set to another object's property which will receive the selected value.
The user may select any item from the list of objects, but I also want to allow empty selections i.e. the EditValue would become null and the displayed text should be the default [No entry] then.
How could this be accomplished the easiest way?
Currently there's no way to clear the value after it has been set once.
There are two options:
1. User can press Ctrl+Del to clear the value
2. However, this is not intuitive. What I do is add another value to the bound list.
var list = GetOriginalList(); // <- get all possible values
list.Add(new MyItem("[empty]", null)); // <- display name and ID
Try this one :
In form load :
LookUpEditName.Properties.AllowNullInput = true ;
LookUpEditName.Properties.NullText = "No entry";
and use LookUpEditName.EditValue = null; to clear the value
Add Cancel Button to your Lookup Edit and add reset code in Button Click event
Dim editor As LookUpEdit = CType(sender, LookUpEdit)
If editor.Properties.Buttons.IndexOf(e.Button) = 0 Then
YourLookUpEdit.EditValue = DBNull.Value
End If

System.Web.HttpException: Cannot have multiple items selected in a DropDownList

During page load, index 0 was already selected. Then this code statement selected index 1:
dropDownList.Items.FindByValue(myValue).Selected = true;
// assume myValue is found at index 1 of dropDownList.Items
On completion of page load, the page reads: "System.Web.HttpException: Cannot have multiple items selected in a DropDownList."
Why did I get the exception? And how can I fix it?
I noticed that both index 0 and index 1 had the properties "Selected" set to true (dropDownList.Items[0].Selected and dropDownList.Items[1].Selected both were true). However, dropDownList.SelectedIndex was still 0, even though index 1 was set most recently.
I tried resolving this by clearing the list selection beforehand.
dropDownList.ClearSelection();
dropDownList.Items.FindByValue(myValue).Selected = true;
But that didn't help. Same exception occurred.
What did help, was setting the selected value another way:
dropDownList.SelectedIndex = dropDownList.Items.IndexOf(dropDownList.Items.FindByValue(myValue));
Now the selection change propogates throughout the list.
So, don't use dropDownList.Items[x].Selected = true/false to change the selected value of a DropDownList. Instead, use dropDownList.SelectedIndex = x;
I just had this problem, and found out it was caused by something different. I was adding the same ListItem instance to multiple dropdowns:
ListItem item = new ListItem("Foo", "1");
ListItem item2 = new ListItem("Bar", "2");
ddl1.Items.Add(item);
ddl2.Items.Add(item);
ddl1.Items.Add(item2);
ddl2.Items.Add(item2);
Then setting the SelectedValue:
ddl1.SelectedValue = "1"; //sets the Selected property of item
ddl2.SelectedValue = "2"; //sets the Selected property of item2
Switching to adding separate instances of ListItem fixed the problem.
My guess is that when you set the SelectedValue of the DropDownList, it sets the Selected property on the appropriate ListItem in its Items collection. So in this case, at the end of the second code block, both items are selected in both dropdowns.
I had a similar problem but under a slightly different scenario. I thought I should post it and the resolution here as it may help someone save time if they happen to be in my similar scenario.
First the error message:
AMError: Sys.WebForms.PageRequestManagerServerErrorException:
Sys.WebForms.PageRequestManagerServerErrorException:
Cannot have multiple items selected in a DropDownList.
My Scenario:
I was using using VisualStudio 2010 to step through the application (ASP VB Net) when I encountered the problem. I looked over the 2 dropdownlists on the page, checked the internet and wasted several hours w/o any resolution.
Resolution:
Then I got feedup and exited VS 2010 and took a break. When I came back. I reran the application and there was no problem. That's when I realized my costly mistake: I had setup an expression that set the
SelectedValue in the Debugger Watch Window! Hence the multiplicity!
I removed the expression and all was well again --- Visual Studion 2010 able to get past the dropdownlist section onto the another area of focus of the application.

Getting a selected value from the drop down list inside a GridView on Update

I have a GridView, each row has Edit button. After it's clicked, one of the columns turns into a drop down list where users can select value. Edit button becomes Update - so very simple usual scenario.
Now, I don't seem to be able to grab the selected drop down list after Update is clicked. Here is my code:
protected void gv_UpdateRow(string arg)
{
int currentIndex = gv.EditIndex;
gv.EditIndex = -1;
GridViewRow currentRow = gv.Rows[currentIndex];
try
{
string value2 = ((DropDownList)currentRow.FindControl("ddlValueTwo")).SelectedItem.ToString();
}
catch
{
Response.Write("error");
}
BindGridView();
}
So basically, the program execution always ends up at the catch statement. I have checked and drop down list is found, the exception is thrown when selected item is not found.
What gives?
I use c# asp.net 2.0 web forms
Looks like a databinding error, you are trying yo access data that is not present yet...
got it!
it was the IsPostback, I was missing it, so the gridview was being rebound every page load, and since the drop down list is inside the grid, the data was lost.
However, one thing I forgot to mention here is that all this code sits inside the user control (ascx file) and IsPostBack property applies to the page not the control, which is useless in my case. For example, in my circumstances I add the control manually, so IsPostback will ALWAYS be true, so to avoid this problem I had to implement a session based solution. Hope this helps someone.
There also usercontrol.IsPostBack property but it didn't perform as expected, perhaps they got it right for 3.0
First thought is that you should probably do SelectedItem.Value rather than SelectedItem.ToString().

Categories