Solved

Dropdownlist selection problem

Posted on 2004-10-17
17
278 Views
Last Modified: 2008-02-01
Hi experts,

I have the following code

app_f1_maj1.Items.FindByValue("   ").Selected = True
app_f1_maj1.ClearSelection()
app_f1_maj1.Items.FindByValue(Session("f1_maj1")).Selected = True

After executing the third line I get the message "A DropDownList cannot have multiple items selected"

I checked the selected value of app_f1_maj1 after the ClearSelection and it pointed to "   "...............any ideas???
0
Comment
Question by:sylo_y
  • 10
  • 6
17 Comments
 
LVL 6

Expert Comment

by:casstd
ID: 12336557
Hi,

       if Trim(session("f1_Maj1")) <> "" then
            app_f1_maj1.Items.FindByValue(Session("f1_maj1")).Selected = True
       else
              app_f1_maj1.Items.FindByValue("   ").Selected = True
       end if

Hope this would be helpful to you.
0
 

Author Comment

by:sylo_y
ID: 12336657
Thanks a lot but this will not solve the problem.....The lines are not after each other as in the code but when I trace the values even if I choose to clear the selection the value "   " is always selected and when I try to select another one I will have two selected and that's why I get the error!!!!!!!!!!!!!!!!!!!!!
0
 
LVL 1

Expert Comment

by:adktd
ID: 12337261
Hi. First of all,are we talking about a dynamic combo box or something non dynamic?

Show me the code of the combo box show i'll give u a spesified answer.
0
 

Author Comment

by:sylo_y
ID: 12344944
Well it is a dynamic dropdown list box....

  Bus = New BusObjVB
  dset = New DataSet
  dset = Bus.get_all_maj(app_fac1.SelectedItem.Value, app_type)
  app_f1_maj1.Items.Add(New ListItem("   ", "   "))
   For i = 0 To dset.Tables("m").Rows.Count - 1
         li = New System.Web.UI.WebControls.ListItem
         li.Value = dset.Tables("m").Rows(i)("mc").ToString
         li.Text = dset.Tables("m").Rows(i)("mt").ToString
         app_f1_maj1.Items.Add(li)
   Next

get_all_maj is a function that queries a table and return the records in a dataset

Function get_all_maj(ByVal fac As String, ByVal typ As String) As DataSet
      Dim da As OdbcDataAdapter
      Dim ds As New DataSet
      Dim myConnection As New OdbcConnection(_connStr)
      Dim myselectquery As String

      'Sorry but I cannot reveal the query
     
       myselectquery = "SELECT .............."
     
       da = New OdbcDataAdapter(myselectquery, myConnection)
       da.Fill(ds, "m")
       Return ds
End Function


Let me point out that the creation and filling of the dropdownlist is correct ...again I traced it and checked each value and the value of Session("f1_maj1") is between them but when this code

app_f1_maj1.Items.FindByValue("   ").Selected = True
app_f1_maj1.ClearSelection()
app_f1_maj1.Items.FindByValue(Session("f1_maj1")).Selected = True

is executed as if the third line has no effect ............. weird!!
0
 
LVL 1

Expert Comment

by:adktd
ID: 12344996
ok i whould advice u to do this the way i do it and it always works.No matter what are the values of your combo box leave the first line blank with the value -1.So instead of clearing the selection u should select the first line which has nothing.
0
 

Author Comment

by:sylo_y
ID: 12345247
Ok I did that but if I choose another line without clearing the selection I will have two values selected and the one that will display is the first which is an empty line ....and this is not what I want
0
 

Author Comment

by:sylo_y
ID: 12345461
Please i need a solution..........
0
 

Author Comment

by:sylo_y
ID: 12346199
Ok I'll remodify my question since I changed a bit in the code and what I have now is the following

I have two pages, the data entered in thoses pages are saved in session variables so that when I move from one to the other I can set the values of the various objects on those pages to what I selected.  

My problem is on one page where I have two DDL.  
One is dependant on the other.  
The first time I choose a value in the first DDL the values in the second are filled depending on the first and I choose an entry which is saved in a session variable called f1_maj1.  The second time I get to the same page --and here I am loading the page again since this is not a postback and filling the second DDL based on the value of the first DDL-- the value of the first drop down list is set correctly and the second one has the correct items in it but with the first element selected which has index 0 while the item I want selected has a different index.

To set the value of the second DDL this is the code

app_f1_maj1.ClearSelection()
app_f1_maj1.Items.FindByValue(Session("f1_maj1")).Selected = True

I debuged my code with Session("f1_maj1") having a value of "4".  In the second drop down list I have this value and it is being selected -- since I checked app_f1_maj1.items(5).selected and it had a value of TRUE-- but on page render the second DDL is showing the item with index 0....

Please this is very urgent...

Thanks for your reply.
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:sylo_y
ID: 12346410
I'll even increase the point value of this question
0
 
LVL 1

Expert Comment

by:adktd
ID: 12347315
Everytime i want to preselect a spesific value on addl i do the following :

Write a vbscript code on Window_OnLoad sub in the client and write a code like this :

if f_company.txtSelComType.value <> "" then
    for m = 0 to f_company.selComType.length - 1
       if LineParse("ID", f_company.selComType(m).value, "|") = f_company.txtSelComType.value   then ''That's the value i want
       f_company.selComType(m).selected = true
       exit for
      end if
   next
end if
0
 

Author Comment

by:sylo_y
ID: 12355539
This solution can't work in my case since first  have  asession varaibale to comapre to and second my form is dynamically generated
0
 
LVL 1

Expert Comment

by:adktd
ID: 12356225
yeah i know i that's why every time i enter the page the vars i want to compare are sores to a hidden field. In the prev code, f_company.txtSelComType.value  is a hidden field.
0
 

Author Comment

by:sylo_y
ID: 12356238
Ok I've tried something, originally setting this ddl value was done on Page_load so i placed the code in  another procedure that was triggered when I change the value of the first DDL......this works but what i need is to see the value in the 2nd ddl when the page loads how to do that?????????  Hope this was clear......
0
 
LVL 1

Expert Comment

by:adktd
ID: 12356737
if u did that with the one ddl why can't u do it with the 2nd one as well?
What do u need in order to make the 2nd ddl select the correct line?
Do u need a variable that u pass in that page?
0
 

Author Comment

by:sylo_y
ID: 12357575
Hi adktd.....I solved the problem and it was related to the way I was binding the data .....I use two ways and it seems that one of them in this case doesn't work so when I tried the second way it worked ......eventhough the first ddl uses the other method but works just fine........I know it is not a logical solution but it worked like  a charm ...........
Thanks for your help
0
 
LVL 1

Accepted Solution

by:
adktd earned 500 total points
ID: 12358895
I hope i helped u to find a solution.
What do u plan to do about the questions points?
0
 

Author Comment

by:sylo_y
ID: 12366808
I won't be stingy...........so I'll give you those 500 points and thanks for your help....
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
This video discusses moving either the default database or any database to a new volume.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now