Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1616
  • Last Modified:

Set SelectedValue on ListBox

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
mantarae
Asked:
mantarae
  • 4
  • 3
  • 2
  • +1
1 Solution
 
Rodney HelsensCommented:
c# code here. basically you use the dropdownlist items collection, findByValue method..

ddlYourDropDownList.Items.FindByValue(SessionVariable).Selected = true;
0
 
shovavnikCommented:
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
 
mantaraeAuthor Commented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Rodney HelsensCommented:
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
 
shovavnikCommented:
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
 
mantaraeAuthor Commented:
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
 
mantaraeAuthor Commented:
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
 
Rodney HelsensCommented:
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
 
jnhorstCommented:
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
 
jnhorstCommented:
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
 
mantaraeAuthor Commented:
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

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

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