Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
|
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 9th, 2007, 04:53 AM
Authorized User
 
Join Date: Mar 2006
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Default Dynamic ListBox Population n Selection

Hi,
I m trying to populate a ListBox Dynamically with MultipleSelect option and then trying to print the selected values in a different page. The cs code that i'm using for it is:

Code:
public partial class ListBox : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        AvailableReportsTableAdapter allReports = new AvailableReportsTableAdapter();
        ListBox1.DataSource = allReports.GetAllReports();
        ListBox1.DataTextField = "ReportName";
        ListBox1.DataValueField = "ReportID";
        ListBox1.DataBind();
    }
    protected void Btn_Click(object sender, EventArgs e)
    {
        Label1.Text = "Hi";
        foreach (ListItem item in ListBox1.Items)
        {
            if (item.Selected)
            {
                Label1.Text += item.Text;
            }
        }
    }
}
Unfortunately item.selected is never sets to true and the selected field doesn't get printed. I must be overlooking something n m not sure what...


Adventure, Play, Alive, Fast, Race, Addict, React, Attack, Heat, Aggression, Blast, Ahead, Escape, Challenge, Anarchy, Game, Real, Damage, Insane, Acclerate,Passion, Fear, Dominate, Crazy, Awesome, Sweat, Habit, Avenge, Balls, Imagine, Viril, Chaos, Amuse, Heavy, Panic, @#!%, Fanatic
__________________
------------------------------------------------------------------------------------------------------------
<b><i>Adventure, Play, Alive, Fast, Race, Addict, React, Attack, Heat, Aggression, Blast, Ahead, Escape, Challenge, Anarchy, Game, Real, Damage, Insane, Acclerate,Passion, Fear, Dominate, Crazy, Awesome, Sweat, Habit, Avenge, Balls, Imagine, Viril, Chaos, Amuse, Heavy, Panic, @#!%, Fanatic </i></b>
 
Old January 9th, 2007, 05:05 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

You're rebinding the list on every post back, overwriting the selected items. Try this instead:

protected void Page_Load(object sender, EventArgs e)
{
  if (!Page.IsPostBack)
  {
    AvailableReportsTableAdapter allReports = new AvailableReportsTableAdapter();
    ListBox1.DataSource = allReports.GetAllReports();
    ListBox1.DataTextField = "ReportName";
    ListBox1.DataValueField = "ReportID";
    ListBox1.DataBind();
  }
}


Hope this helps,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.





Similar Threads
Thread Thread Starter Forum Replies Last Post
About listbox selection mode chandra_yvr ASP.NET 2.0 Basics 1 April 3rd, 2007 07:50 AM
Toolbar switching / dynamic population yamyam .NET Framework 1.x 0 February 14th, 2006 08:15 AM
Dynamic Subform Generation & Population kindler Access 4 December 13th, 2005 01:59 PM
listbox selection jon23d Access VBA 1 June 21st, 2005 01:35 PM
Listbox selection in a form jpcoutts Access 1 November 10th, 2004 05:02 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.