Solved

Set SelectedValue on ListBox

Posted on 2004-09-01
11
1,580 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
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 …
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

746 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

12 Experts available now in Live!

Get 1:1 Help Now