Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 290
  • Last Modified:

Dropdownlist selection problem

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
sylo_y
Asked:
sylo_y
  • 10
  • 6
1 Solution
 
casstdCommented:
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
 
sylo_yAuthor Commented:
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
 
adktdCommented:
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
sylo_yAuthor Commented:
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
 
adktdCommented:
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
 
sylo_yAuthor Commented:
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
 
sylo_yAuthor Commented:
Please i need a solution..........
0
 
sylo_yAuthor Commented:
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
 
sylo_yAuthor Commented:
I'll even increase the point value of this question
0
 
adktdCommented:
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
 
sylo_yAuthor Commented:
This solution can't work in my case since first  have  asession varaibale to comapre to and second my form is dynamically generated
0
 
adktdCommented:
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
 
sylo_yAuthor Commented:
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
 
adktdCommented:
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
 
sylo_yAuthor Commented:
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
 
adktdCommented:
I hope i helped u to find a solution.
What do u plan to do about the questions points?
0
 
sylo_yAuthor Commented:
I won't be stingy...........so I'll give you those 500 points and thanks for your help....
0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

  • 10
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now