Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Set SelectedValue on ListBox

Posted on 2004-09-01
11
Medium Priority
?
1,600 Views
Last Modified: 2010-05-19
I have a listbox on my webpage that has a couple of hundred client names in it.  I store the last selected client name from this listbox in the Session.   This same listbox is available on a number of pages, so I want to have the listbox show this previously selected client name. So, for example, I use:  lb.SelectedValue = mySessionValue   when I bind the data to the listbox, which works, but the listbox does not "scroll down" to show this selected client's name.  If I scroll down to the client, they are indeed selected, but this seems counter-intuitive.   Is this a bug/feature, or is there a way to have the listbox "scroll" itself to the selected item?

I can post my code, if you like, but this seems to me like something weird is going on with the listbox...
0
Comment
Question by:mantarae
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +1
11 Comments
 
LVL 9

Expert Comment

by:Rodney Helsens
ID: 11956428
c# code here. basically you use the dropdownlist items collection, findByValue method..

ddlYourDropDownList.Items.FindByValue(SessionVariable).Selected = true;
0
 
LVL 8

Expert Comment

by:shovavnik
ID: 11956781
Just for kicks, see if it works right when you only put in 5 items.

If it works, it means the browser is having difficulty scrolling to the list item.

If it doesn't work, we'll see what we can do from there.
0
 
LVL 7

Author Comment

by:mantarae
ID: 11957306
I tried rodnex's suggestion, but it has the same result.  

I tried pulling in only 5 items, with a size=4 on the box, and if I select the 5th item, which is "off screen", the item is selected, but the list box still doesn't scroll to it.

I tried running the page from a Windows 2003 Server with IE 6, and got the same result, however I just ran the page from Netscape 7.0, and it works fine, so this must be some type of IE 6.0 bug (even with SP 1 and the hotfixes).
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!

 
LVL 9

Expert Comment

by:Rodney Helsens
ID: 11957440
mantarae, don't be so quick to assume it is an IE bug, there are plenty of us using asp.net with IE 6 and listboxes.. if you don't mind posting your code, i think we can get this figured out for you.
By the way, what does the output html look like for the 5 item select box..? can you post that?

One thing to keep in mind, if you are selecting multiple rows, it will cause this type of error... see if the first item has a "selected" value
0
 
LVL 8

Expert Comment

by:shovavnik
ID: 11957450
I did something similar yesterday, but I didn't have this problem at all.

I can only suggest at the moment to see if there's a difference if you set the SelectedValue property of the list box, or the Selected property of the ListItem or setting the SelectedIndex property.  There shouldn't really be a difference at all (especially between the first and last), but it's worth a try.
0
 
LVL 7

Author Comment

by:mantarae
ID: 11957817
This code is in a function that is called in PageLoad(), if !IsPostBack:


DataTable tableClt = [my query];
DataView dvClt = tableClt.DefaultView;

lbRunForEntity.DataSource = dvClt;
lbRunForEntity.DataTextField = "ClientName";
lbRunForEntity.DataValueField = "pkClient";
lbRunForEntity.DataBind();

//CurrClientId is set to 0 when a user logs into the site
if (!Session["CurrClientId"].ToString().Equals("0")){
  try{
    lbRunForEntity.SelectedValue = Session["CurrClientId"].ToString();
  }
  catch{
    lbRunForEntity.SelectedIndex = 0;
  }                        
}
else if( dvClt.Count != 0 ) lbRunForEntity.SelectedIndex = 0;
0
 
LVL 7

Author Comment

by:mantarae
ID: 11957835
And here is the html output for 5 records:


<select name="lbRunForEntity" size="4" multiple="multiple" id="lbRunForEntity">
      <option value="22471">LastName1, FirstName1</option>
      <option value="21710">LastName2, FirstName2</option>
      <option value="16894">LastName3, FirstName3</option>
      <option value="22003">LastName4, FirstName4</option>
      <option selected="selected" value="21353">LastName5, FirstName5</option>

</select>
0
 
LVL 9

Accepted Solution

by:
Rodney Helsens earned 2000 total points
ID: 11957994
mantarae, the issue here is that you have multiple selected for this listbox, so it will never 'jump' to the selected one on a multiple...
0
 
LVL 10

Expert Comment

by:jnhorst
ID: 11958579
mantarae:

First, right after you call DataBind() on the dropdown, do this:

// ddl is my name for the dropdown list.
ddl.ClearSelecttion();

Then do this:
string clientID = Session["CurrClientId"].ToString();
if (clientID != "0")
{
     ListItem li = ddl.Items.FindByValue(clientID);
     if (li != null) li.Selected = true;
}

You have to call ClearSelection so that ASP.NET does not try to select multiple items.  After that, get your id and use FindByValue() to see if you can resolve to it.

John
0
 
LVL 10

Expert Comment

by:jnhorst
ID: 11958591
Oops:

Mispelled Selection in this line:

// ddl is my name for the dropdown list.
ddl.ClearSelecttion();

Should be:

// ddl is my name for the dropdown list.
ddl.ClearSelection();

John
0
 
LVL 7

Author Comment

by:mantarae
ID: 11962603
Thanks for all the input.   Rodnex is right.   If I take the multiple="multiple" off, it "jumps" to the item, when I add that back in, it doesn't "jump" any more.    Having the multiple is more important than having the box jump, so I guess I'm stuck.  :-P    Thanks again.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

610 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