Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / February 2006

Tip: Looking for answers? Try searching our database.

dropdownlist multiple selection problem

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
ads - 13 Feb 2006 09:45 GMT
hi,
after binding the dropdownlist to a datasource, ive experience this error
"Cannot have multiple items selected in a dropdownlist" after using the code:
dropdownlist.items.findbyvalue("value").selected = true
I didnt specify any selected item in the dropdownlist during binding to the
datasource. I use dropdownlist.clearselection() but still error occurs.

I need information on this. Thanks.

Ads
Karl Seguin [MVP] - 13 Feb 2006 12:24 GMT
Can we see your complete code? I'm not familiar with ClearSelection (don't
see it in the docs right now for some reason??)

Karl
Signature

http://www.openmymind.net/

> hi,
> after binding the dropdownlist to a datasource, ive experience this error
[quoted text clipped - 8 lines]
>
> Ads
ads - 14 Feb 2006 02:22 GMT
//build the datasource of the 2 dropdownlists on page load

if(!ispostback)
{
for (int i = DateTime.Now.Year; i > DateTime.Now.Year - 50; i--)
       {
           ListItem li = new ListItem(i.ToString(), i.ToString());
           ddlYrJoined.Items.Add(li);
           ddlYrLeft.Items.Add(li);

       }
       ddlYrLeft.Items.Insert(0, "SELECT");
       ddlYrJoined.Items.Insert(0,"SELECT");
}

//on a separate function

ddlYrJoined.Items.FindByValue(dr["yrstart"].ToString()).Selected = true;

//where dr["yrstart"].ToString() is a datareader getting its values from a
database

> Can we see your complete code? I'm not familiar with ClearSelection (don't
> see it in the docs right now for some reason??)
[quoted text clipped - 12 lines]
> >
> > Ads
Karl Seguin [MVP] - 14 Feb 2006 12:19 GMT
Is the separate function also called when !ispostback? You had also
mentioned ClearSelection() but I don't see that anywhere..

KArl

Signature

http://www.openmymind.net/

> //build the datasource of the 2 dropdownlists on page load
>
[quoted text clipped - 36 lines]
>> >
>> > Ads
ads - 15 Feb 2006 05:56 GMT
yes.the separate function is called when !ispostback too. Heres the complete
code:

if (!IsPostBack)
       {
           if (Request.QueryString["id"]!= null)
           {
               BindDDL();
               
LoadSelectedAffiliation(int.Parse(Request.QueryString["id"].ToString()));
           }
             
       }

private void LoadSelectedAffiliation(int id)
   {
       SqlDataReader dr =
Microsoft.ApplicationBlocks.Data.SqlHelper.ExecuteReader(System.Configuration.ConfigurationSettings.AppSettings["connectionstring"],
CommandType.Text, "select * from jbaffiliates where affiliateid=" + id);
       ddlYrJoined.ClearSelection();
       ddlYrLeft.ClearSelection();
       while (dr.Read())
       {
           orgname.Text = dr["orgname"].ToString();
           orgloc.Text = dr["orglocation"].ToString();
           ddlYrJoined.Items.FindByValue(dr["yrstart"].ToString()).Selected
= true;
           ddlYrLeft.Items.FindByValue(dr["yrend"].ToString()).Selected =
true;
           position.Text = dr["highestpositiontitle"].ToString();
           responsibilities.Text = dr["responsibilities"].ToString();
       }
       dr.Close();
   }

private void BindDDL()
   {
       for (int i = DateTime.Now.Year; i > DateTime.Now.Year - 50; i--)
       {
           ListItem li = new ListItem(i.ToString(), i.ToString());
           ddlYrJoined.Items.Add(li);
           ddlYrLeft.Items.Add(li);

       }
       ddlYrLeft.Items.Insert(0, "SELECT");
       ddlYrJoined.Items.Insert(0,"SELECT");
   }

> Is the separate function also called when !ispostback? You had also
> mentioned ClearSelection() but I don't see that anywhere..
[quoted text clipped - 41 lines]
> >> >
> >> > Ads
Karl Seguin [MVP] - 15 Feb 2006 12:22 GMT
well that clears that up.  I didn't think you were setting multiple values.
You can't do that in a dropdownbox, use a listbox instead.

Karl

Signature

http://www.openmymind.net/

> yes.the separate function is called when !ispostback too. Heres the
> complete
[quoted text clipped - 96 lines]
>> >> >
>> >> > Ads
ads - 16 Feb 2006 03:14 GMT
Hmmm...can u please elaborate your statement? I do receive the error message
"Cannot have multiple items selected in a dropdownlist." Why cant i use the
code for dropdownlist if its provided there?

> well that clears that up.  I didn't think you were setting multiple values.
> You can't do that in a dropdownbox, use a listbox instead.
[quoted text clipped - 101 lines]
> >> >> >
> >> >> > Ads
Karl Seguin [MVP] - 16 Feb 2006 11:51 GMT
Well, you are trying to have multiple values selected. The DropDownList
simply doesn't allow this to happen. To be able to have multiple values
selected, you need to use a ListBox instead of a DropDownBox.

Karl

Signature

http://www.openmymind.net/

> Hmmm...can u please elaborate your statement? I do receive the error
> message
[quoted text clipped - 113 lines]
>> >> >> >
>> >> >> > Ads
ads - 17 Feb 2006 02:01 GMT
In the first function BindDDL() im just binding the datasource to the
dropdownlists and in the second function,im trying to select an item for each
dropdownlist. Please take note that there are 2 dropdownlist in my code
(ddlYrJoined and ddlYrLeft). Im a bit confuse. I know its not allowed to have
multiple selection in a dropdownlist. Which part of the code am i trying to
select multiple values?

> Well, you are trying to have multiple values selected. The DropDownList
> simply doesn't allow this to happen. To be able to have multiple values
[quoted text clipped - 119 lines]
> >> >> >> >
> >> >> >> > Ads
Karl Seguin [MVP] - 17 Feb 2006 12:05 GMT
while (dr.Read())
{
  orgname.Text = dr["orgname"].ToString();
  orgloc.Text = dr["orglocation"].ToString();
  ddlYrJoined.Items.FindByValue(dr["yrstart"].ToString()).Selected = true;
  ddlYrLeft.Items.FindByValue(dr["yrend"].ToString()).Selected = true;
  position.Text = dr["highestpositiontitle"].ToString();
  responsibilities.Text = dr["responsibilities"].ToString();
}

if your data reader contains more than 1 record, u'll be looping through it
and trying to assign a different dr["ystart"] and dr["yrend"] to each
dropdown

Karl
Signature

http://www.openmymind.net/

> In the first function BindDDL() im just binding the datasource to the
> dropdownlists and in the second function,im trying to select an item for
[quoted text clipped - 137 lines]
>> >> >> >> >
>> >> >> >> > Ads
ads - 28 Feb 2006 01:19 GMT
Though im assuming there's only 1 record, i'll check if theres more record.
Better if i remove the while loop.
Thanks Karl

> while (dr.Read())
> {
[quoted text clipped - 152 lines]
> >> >> >> >> >
> >> >> >> >> > Ads

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.