?
Solved

Set SelectedValue on ListBox

Posted on 2004-09-01
11
Medium Priority
?
1,596 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
Independent Software Vendors: 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses
Course of the Month11 days, 11 hours left to enroll

752 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