|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
"Cannot have multiple items selected in a DropDownList" ErrorI'm getting the error "Cannot have multiple items selected in a DropDownList" when I'm trying to select a specific value in a System.Web.UI.WebControls.DropDownList by using the code: ddl.SelectedValue = "3"; I'm not setting it to any other value anywhere in the code. All I'm doing with that list is populating it by DataBind-ing it to a SqlReader and then adding an additional ListItem to it: ddl.Items.Insert(0, oItem); I guess ddl.Items.Insert() also selects the inserted value, since if I comment out this line I don't get the error anymore. Does anybody know how to fix this or should I just create some client side code that will run after the page loads and will make all the selectios? Thanks Before you set the selection in a dropdownlist call the clearSelection, e.g.
ddl.ClearSelection(); Show quoteHide quote "Vi" wrote: > Hi, > I'm getting the error "Cannot have multiple items selected in a > DropDownList" when I'm trying to select a specific value in a > System.Web.UI.WebControls.DropDownList by using the code: > ddl.SelectedValue = "3"; > > I'm not setting it to any other value anywhere in the code. All I'm doing > with that list is populating it by DataBind-ing it to a SqlReader and then > adding an additional ListItem to it: > > ddl.Items.Insert(0, oItem); > > I guess ddl.Items.Insert() also selects the inserted value, since if I > comment out this line I don't get the error anymore. > > Does anybody know how to fix this or should I just create some client side > code that will run after the page loads and will make all the selectios? > > Thanks Thanls for your reply, Philip, but it did not work - I get the same error.
This is what I did: lstWhatBrings.ClearSelection(); lstWhatBrings.SelectedValue = "2"; And I still get "Cannot have multiple items selected in a DropDownList" Thanks again. Show quoteHide quote "Phillip Williams" wrote: > Before you set the selection in a dropdownlist call the clearSelection, e.g. > ddl.ClearSelection(); > -- > HTH, > Phillip Williams > http://www.societopia.net > http://www.webswapp.com > > > "Vi" wrote: > > > Hi, > > I'm getting the error "Cannot have multiple items selected in a > > DropDownList" when I'm trying to select a specific value in a > > System.Web.UI.WebControls.DropDownList by using the code: > > ddl.SelectedValue = "3"; > > > > I'm not setting it to any other value anywhere in the code. All I'm doing > > with that list is populating it by DataBind-ing it to a SqlReader and then > > adding an additional ListItem to it: > > > > ddl.Items.Insert(0, oItem); > > > > I guess ddl.Items.Insert() also selects the inserted value, since if I > > comment out this line I don't get the error anymore. > > > > Does anybody know how to fix this or should I just create some client side > > code that will run after the page loads and will make all the selectios? > > > > Thanks Well, the short answer is that it should not be happening. If you have not
found the cause yet; post the code (including that for databinding). Something in your code causes the problem. Show quoteHide quote "Vi" wrote: > Thanls for your reply, Philip, but it did not work - I get the same error. > This is what I did: > lstWhatBrings.ClearSelection(); > lstWhatBrings.SelectedValue = "2"; > > And I still get "Cannot have multiple items selected in a DropDownList" > Thanks again. > > > "Phillip Williams" wrote: > > > Before you set the selection in a dropdownlist call the clearSelection, e.g. > > ddl.ClearSelection(); > > -- > > HTH, > > Phillip Williams > > http://www.societopia.net > > http://www.webswapp.com > > > > > > "Vi" wrote: > > > > > Hi, > > > I'm getting the error "Cannot have multiple items selected in a > > > DropDownList" when I'm trying to select a specific value in a > > > System.Web.UI.WebControls.DropDownList by using the code: > > > ddl.SelectedValue = "3"; > > > > > > I'm not setting it to any other value anywhere in the code. All I'm doing > > > with that list is populating it by DataBind-ing it to a SqlReader and then > > > adding an additional ListItem to it: > > > > > > ddl.Items.Insert(0, oItem); > > > > > > I guess ddl.Items.Insert() also selects the inserted value, since if I > > > comment out this line I don't get the error anymore. > > > > > > Does anybody know how to fix this or should I just create some client side > > > code that will run after the page loads and will make all the selectios? > > > > > > Thanks Here's the code:
if(!IsPostBack) { System.Web.UI.WebControls.ListItem oItem = new System.Web.UI.WebControls.ListItem(); oItem.Value = "0"; oItem.Text = "-- Please Select --"; dr = slDatabase.ExecuteReader1("sp_get_all_products"); ddl.DataSource = dr; ddl.DataTextField = "product_name"; ddl.DataValueField = "product_id"; ddl.DataBind(); ddl.Items.Insert(0, oItem); ////ddl.SelectedValue = "0"; dr.Close(); ddl.ClearSelection(); ddl.SelectedIndex = 1; //// I also tried ddl.SelectedValue = "1"; but I get the same error } thanks again, Phillip. Show quoteHide quote "Phillip Williams" wrote: > Well, the short answer is that it should not be happening. If you have not > found the cause yet; post the code (including that for databinding). > Something in your code causes the problem. > -- > HTH, > Phillip Williams > http://www.societopia.net > http://www.webswapp.com > > > "Vi" wrote: > > > Thanls for your reply, Philip, but it did not work - I get the same error. > > This is what I did: > > lstWhatBrings.ClearSelection(); > > lstWhatBrings.SelectedValue = "2"; > > > > And I still get "Cannot have multiple items selected in a DropDownList" > > Thanks again. > > > > > > "Phillip Williams" wrote: > > > > > Before you set the selection in a dropdownlist call the clearSelection, e.g. > > > ddl.ClearSelection(); > > > -- > > > HTH, > > > Phillip Williams > > > http://www.societopia.net > > > http://www.webswapp.com > > > > > > > > > "Vi" wrote: > > > > > > > Hi, > > > > I'm getting the error "Cannot have multiple items selected in a > > > > DropDownList" when I'm trying to select a specific value in a > > > > System.Web.UI.WebControls.DropDownList by using the code: > > > > ddl.SelectedValue = "3"; > > > > > > > > I'm not setting it to any other value anywhere in the code. All I'm doing > > > > with that list is populating it by DataBind-ing it to a SqlReader and then > > > > adding an additional ListItem to it: > > > > > > > > ddl.Items.Insert(0, oItem); > > > > > > > > I guess ddl.Items.Insert() also selects the inserted value, since if I > > > > comment out this line I don't get the error anymore. > > > > > > > > Does anybody know how to fix this or should I just create some client side > > > > code that will run after the page loads and will make all the selectios? > > > > > > > > Thanks I ran this code without getting any error!
Show quoteHide quote "Vi" wrote: > Here's the code: > if(!IsPostBack) > { > System.Web.UI.WebControls.ListItem oItem = new > System.Web.UI.WebControls.ListItem(); > oItem.Value = "0"; > oItem.Text = "-- Please Select --"; > dr = slDatabase.ExecuteReader1("sp_get_all_products"); > ddl.DataSource = dr; > ddl.DataTextField = "product_name"; > ddl.DataValueField = "product_id"; > ddl.DataBind(); > ddl.Items.Insert(0, oItem); > ////ddl.SelectedValue = "0"; > dr.Close(); > > ddl.ClearSelection(); > ddl.SelectedIndex = 1; > //// I also tried ddl.SelectedValue = "1"; but I get the same error > } > > thanks again, Phillip. > > > "Phillip Williams" wrote: > > > Well, the short answer is that it should not be happening. If you have not > > found the cause yet; post the code (including that for databinding). > > Something in your code causes the problem. > > -- > > HTH, > > Phillip Williams > > http://www.societopia.net > > http://www.webswapp.com > > > > > > "Vi" wrote: > > > > > Thanls for your reply, Philip, but it did not work - I get the same error. > > > This is what I did: > > > lstWhatBrings.ClearSelection(); > > > lstWhatBrings.SelectedValue = "2"; > > > > > > And I still get "Cannot have multiple items selected in a DropDownList" > > > Thanks again. > > > > > > > > > "Phillip Williams" wrote: > > > > > > > Before you set the selection in a dropdownlist call the clearSelection, e.g. > > > > ddl.ClearSelection(); > > > > -- > > > > HTH, > > > > Phillip Williams > > > > http://www.societopia.net > > > > http://www.webswapp.com > > > > > > > > > > > > "Vi" wrote: > > > > > > > > > Hi, > > > > > I'm getting the error "Cannot have multiple items selected in a > > > > > DropDownList" when I'm trying to select a specific value in a > > > > > System.Web.UI.WebControls.DropDownList by using the code: > > > > > ddl.SelectedValue = "3"; > > > > > > > > > > I'm not setting it to any other value anywhere in the code. All I'm doing > > > > > with that list is populating it by DataBind-ing it to a SqlReader and then > > > > > adding an additional ListItem to it: > > > > > > > > > > ddl.Items.Insert(0, oItem); > > > > > > > > > > I guess ddl.Items.Insert() also selects the inserted value, since if I > > > > > comment out this line I don't get the error anymore. > > > > > > > > > > Does anybody know how to fix this or should I just create some client side > > > > > code that will run after the page loads and will make all the selectios? > > > > > > > > > > Thanks me too :)
Show quoteHide quote "Phillip Williams" <WEBSWAPP@newsgroups.nospam> wrote in message news:BAF6352F-7599-4445-8A93-DA6547AF0E98@microsoft.com... >I ran this code without getting any error! > -- > HTH, > Phillip Williams > http://www.societopia.net > http://www.webswapp.com > > > "Vi" wrote: > >> Here's the code: >> if(!IsPostBack) >> { >> System.Web.UI.WebControls.ListItem oItem = new >> System.Web.UI.WebControls.ListItem(); >> oItem.Value = "0"; >> oItem.Text = "-- Please Select --"; >> dr = slDatabase.ExecuteReader1("sp_get_all_products"); >> ddl.DataSource = dr; >> ddl.DataTextField = "product_name"; >> ddl.DataValueField = "product_id"; >> ddl.DataBind(); >> ddl.Items.Insert(0, oItem); >> ////ddl.SelectedValue = "0"; >> dr.Close(); >> >> ddl.ClearSelection(); >> ddl.SelectedIndex = 1; >> //// I also tried ddl.SelectedValue = "1"; but I get the same error >> } >> >> thanks again, Phillip. >> >> >> "Phillip Williams" wrote: >> >> > Well, the short answer is that it should not be happening. If you have >> > not >> > found the cause yet; post the code (including that for databinding). >> > Something in your code causes the problem. >> > -- >> > HTH, >> > Phillip Williams >> > http://www.societopia.net >> > http://www.webswapp.com >> > >> > >> > "Vi" wrote: >> > >> > > Thanls for your reply, Philip, but it did not work - I get the same >> > > error. >> > > This is what I did: >> > > lstWhatBrings.ClearSelection(); >> > > lstWhatBrings.SelectedValue = "2"; >> > > >> > > And I still get "Cannot have multiple items selected in a >> > > DropDownList" >> > > Thanks again. >> > > >> > > >> > > "Phillip Williams" wrote: >> > > >> > > > Before you set the selection in a dropdownlist call the >> > > > clearSelection, e.g. >> > > > ddl.ClearSelection(); >> > > > -- >> > > > HTH, >> > > > Phillip Williams >> > > > http://www.societopia.net >> > > > http://www.webswapp.com >> > > > >> > > > >> > > > "Vi" wrote: >> > > > >> > > > > Hi, >> > > > > I'm getting the error "Cannot have multiple items selected in a >> > > > > DropDownList" when I'm trying to select a specific value in a >> > > > > System.Web.UI.WebControls.DropDownList by using the code: >> > > > > ddl.SelectedValue = "3"; >> > > > > >> > > > > I'm not setting it to any other value anywhere in the code. All >> > > > > I'm doing >> > > > > with that list is populating it by DataBind-ing it to a SqlReader >> > > > > and then >> > > > > adding an additional ListItem to it: >> > > > > >> > > > > ddl.Items.Insert(0, oItem); >> > > > > >> > > > > I guess ddl.Items.Insert() also selects the inserted value, since >> > > > > if I >> > > > > comment out this line I don't get the error anymore. >> > > > > >> > > > > Does anybody know how to fix this or should I just create some >> > > > > client side >> > > > > code that will run after the page loads and will make all the >> > > > > selectios? >> > > > > >> > > > > Thanks you're right, the sample code I send does work. I should've sent a bigger
part of my code as sample, because I have several DropDowns on the same page and the error is generated when I add the common ListItem object to the second DropDown. Here's the code: if(!IsPostBack) { System.Web.UI.WebControls.ListItem oItem = new System.Web.UI.WebControls.ListItem(); oItem.Value = "0"; oItem.Text = "-- Please Select --"; dr = slDatabase.ExecuteReader1("sp_get_all_products"); ddl.DataSource = dr; ddl.DataTextField = "product_name"; ddl.DataValueField = "product_id"; ddl.DataBind(); ddl.Items.Insert(0, oItem); ////ddl.SelectedValue = "0"; dr.Close(); ddl.ClearSelection(); ddl.SelectedIndex = 1; dr = slDatabase.ExecuteReader1("sp_get_all_companies"); ddl2.DataSource = dr; ddl2.DataTextField = "company_name"; ddl2.DataValueField = "company_id"; ddl2.DataBind(); //// ddl2.Items.Insert(0, oItem); -- THIS LINE GENERATES THE ERROR } I'm still not sure why this lines generates "Cannot have multiple items selected in a DropDownList" error, since I'm not making any selections here. Thanks Show quoteHide quote "Phillip Williams" wrote: > I ran this code without getting any error! > -- > HTH, > Phillip Williams > http://www.societopia.net > http://www.webswapp.com > > > "Vi" wrote: > > > Here's the code: > > if(!IsPostBack) > > { > > System.Web.UI.WebControls.ListItem oItem = new > > System.Web.UI.WebControls.ListItem(); > > oItem.Value = "0"; > > oItem.Text = "-- Please Select --"; > > dr = slDatabase.ExecuteReader1("sp_get_all_products"); > > ddl.DataSource = dr; > > ddl.DataTextField = "product_name"; > > ddl.DataValueField = "product_id"; > > ddl.DataBind(); > > ddl.Items.Insert(0, oItem); > > ////ddl.SelectedValue = "0"; > > dr.Close(); > > > > ddl.ClearSelection(); > > ddl.SelectedIndex = 1; > > //// I also tried ddl.SelectedValue = "1"; but I get the same error > > } > > > > thanks again, Phillip. > > > > > > "Phillip Williams" wrote: > > > > > Well, the short answer is that it should not be happening. If you have not > > > found the cause yet; post the code (including that for databinding). > > > Something in your code causes the problem. > > > -- > > > HTH, > > > Phillip Williams > > > http://www.societopia.net > > > http://www.webswapp.com > > > > > > > > > "Vi" wrote: > > > > > > > Thanls for your reply, Philip, but it did not work - I get the same error. > > > > This is what I did: > > > > lstWhatBrings.ClearSelection(); > > > > lstWhatBrings.SelectedValue = "2"; > > > > > > > > And I still get "Cannot have multiple items selected in a DropDownList" > > > > Thanks again. > > > > > > > > > > > > "Phillip Williams" wrote: > > > > > > > > > Before you set the selection in a dropdownlist call the clearSelection, e.g. > > > > > ddl.ClearSelection(); > > > > > -- > > > > > HTH, > > > > > Phillip Williams > > > > > http://www.societopia.net > > > > > http://www.webswapp.com > > > > > > > > > > > > > > > "Vi" wrote: > > > > > > > > > > > Hi, > > > > > > I'm getting the error "Cannot have multiple items selected in a > > > > > > DropDownList" when I'm trying to select a specific value in a > > > > > > System.Web.UI.WebControls.DropDownList by using the code: > > > > > > ddl.SelectedValue = "3"; > > > > > > > > > > > > I'm not setting it to any other value anywhere in the code. All I'm doing > > > > > > with that list is populating it by DataBind-ing it to a SqlReader and then > > > > > > adding an additional ListItem to it: > > > > > > > > > > > > ddl.Items.Insert(0, oItem); > > > > > > > > > > > > I guess ddl.Items.Insert() also selects the inserted value, since if I > > > > > > comment out this line I don't get the error anymore. > > > > > > > > > > > > Does anybody know how to fix this or should I just create some client side > > > > > > code that will run after the page loads and will make all the selectios? > > > > > > > > > > > > Thanks I wonder if you can add the same ListItem to several DropDownLists.
Try the following to see if your exception goes away: ddl.Items.Insert(0, new ListItem("-- Please Select --", 0); ddl.SelectedIndex = 0; '... ddl2.Items.Insert(0, new ListItem("-- Please Select --", 0); ddl2.SelectedIndex = 0; Regards. I've seen this error.
I realise that this thread is old, but perhaps you still need a solution. I've found that inserting/adding items to a DropDown can create problems (especially inserting a ListItem after performing a Databind!). To get around this, I retrieve the database info I'm using for the DropDown into a DataTable. Then I insert the Default entry into the DataTable at the 0 index. Then, bind the DataTable to your DropDown. This has helped me resolve a lot of weird errors. e.g., Dim dt as DataTable = (some routine that returns a datatable) Dim dr As DataRow = dt.NewRow dr(0) = 0 dr(1) = "-- Please Select --" dt.Rows.InsertAt(dr, 0) dd1.DataSource = dtDD dd1.DataTextField = "Text" dd1.DataValueField = "Value" dd1.DataBind() *** Sent via Developersdex http://www.developersdex.com *** I set DropDownList.SelectedIndex to select a specific item in the
dropdownlist rather than set DropDownList.SelectedValue. eg. 'Select the first Item in DropDownList. ddl.SelectedIndex = 0 Hope that helps. Sorry, I did not see that others have posted the same suggestion
already.
How to get ListItemValue and ListItemText from a Combo using SendMessage
edit attribute values of a web control just before control rendered to the page (control's Load even checkbox bind with 0 = checked? error using .NET CrystalReportViewer (.NET 1.1 VS.NET 2003) Removing columns from Gridview(2.0) databinding on templatefileds dont work? Problems with dynamically created GridView/DetailsView Rendering a Treeview Control Problems Updating GridView when bound to datatable InputAttributes in RowCreated (gridview) GridView XmlDataSource |
|||||||||||||||||||||||