• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 188
  • Last Modified:

select the value in the drop down list box coming from a session variable

Hi,

How can we select the value in the drop down list box coming from a session variable?

Thanks.

        dim lbloperatorid1 as label
   
         If Not (Session("varseloperatorid") Is Nothing) Then
         lbloperatorid1.text = Session("varseloperatorid").ToString
         ddloperatorid.Items.FindByText(lbloperatorid1.text).Selected=true
         end if
0
EdwardPeter
Asked:
EdwardPeter
  • 2
  • 2
  • 2
  • +2
1 Solution
 
soumya_paridaCommented:
If " lbloperatorid1.text" represents value of an item in the drop downdownlist then you can very well write,
ddloperatorid.Items.SelectedValue = lbloperatorid1.text
0
 
soumya_paridaCommented:
The problem with your logic is if that text is not in the list then it will throw an exception at the line
  ddloperatorid.Items.FindByText(lbloperatorid1.text).Selected=true
0
 
raterusCommented:
This is how I usually select an item, which would match the text displayed

Dim find as string = Session("somevalue")
Dim li as listitem = ddloperatorid.Items.FindByText(find)
if not li is nothing then
  li.ClearSelection
  li.selected = true
end if
0
Industry Leaders: 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!

 
EdwardPeterAuthor Commented:
raterus,

will the dropdownlistbox select the matching one using session var?

I'm not sure, is the listitem the one that's going to be selected?

Thanks.
0
 
b1xml2Commented:
if there is already a selection, then it would throw an exception unless the clear the selection be4 choosing another one:
to fine tune raterus's code

Dim text As String = Session("parameter")
Dim item As ListItem = List.Items.FindByText(text)
If Not item Is Nothing Then
      List.ClearSelection()
      item.Selected = True
End If
0
 
tusharashahCommented:
Try this
If Not (Session("varseloperatorid") Is Nothing) Then

   if not ddloperatorid.Items.FindByText(Session("somevalue")) is nothing then

       ddloperatorid.Items.FindByText(Session("somevalue")).selected = true

   end if

end if

if your session value is not nothing
then it will try to find the value inside the dropdown list.
But if it cant find from drop down list them
this will
 ddloperatorid.Items.FindByText(Session("somevalue"))
return nothing
and u can do plan B in else loop.

-tushar
0
 
raterusCommented:
Good point by b1xml2, I found that out the hard way once.  The error isn't thrown until the control renders, and all the times I've experienced it, it isn't very descriptive of which dropdownlist it was.
0
 
EdwardPeterAuthor Commented:
Thanks so much for the information
0

Featured Post

Industry Leaders: 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!

  • 2
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now