Solved

Set SelectedValue on ListBox

Posted on 2004-09-01
11
1,582 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
  • 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

914 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

15 Experts available now in Live!

Get 1:1 Help Now