ListBox will not populate

G

google_me

Hi,
I was doing some C#/ASP.NET examples from an O'Reilly book.

I have this sample of code :
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs"
AutoEventWireup="false" Inherits="CH5_ListBoxes.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>ASP List Box Control</title>
<script runat="server">
void Page_Load(Object sender, EventArgs e)
{
// Put user code to initialize the page here
if (!IsPostBack)
{
// Build 2 dim array for the lists
// First dimension contains book name
// 2nd dimension contains ISBN number

string[,] books = {
{"Programming C#", "0596001177"},
{"Programming ASP.NET", "0596001711"},
{"WebClasses From Scratch", "0789721260"},
{"Teach Yourself C++", "06723072X"},
{"Teach Yourself C++ in 10 Minutes", "067231603X"},
{"XML & Java from Scratch", "0789724766"},
{"Complete Idiots Guide to a Career in Computer Programming",
"0789719959"},
{"XML Web Documents from Scratch", "0789723166"},
{"Clouds to Code", "1861000952"},
{"C++: An Introduction to Programming", "1575760614"},
{"C++ Unleased", "06723122395"}
};
// Populate the Lists
int i;
for (i=0; i < books.GetLength(0); i++)
{
//add both text and value
lbSingle.Items.Add(new ListItem(books[i,0], books[i,1]));
lbMulti.Items.Add(new ListItem(books[i,0], books[i,1]));
}
}
}

void lbSingle_SelectedIndexChanged(Object Source, EventArgs E)
{
//Check to verify that something has been selected.
if(lbSingle.SelectedIndex != -1)
{
lblLbSingle.Text=lbSingle.SelectedItem.Text + "----> ISBN: " +
lbSingle.SelectedItem.Value;
}
}
void lbMulti_SelectedIndexChanged(Object Source, EventArgs E)
{
string str = "";
foreach(ListItem li in lbMulti.Items)
{
if(li.Selected ==true)
{
str += "<br/>" + li.Text + " ----> ISBN: " + li.Value;
}
}
if (str.Length == 0)
lblLbMulti.Text ="No books selected.";
else
lblLbMulti.Text = str;
}
</script>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<h1>ASP Controls</h1>
<h2>List Box</h2>

<h3>ListBox - single selection</h3>
<asp:ListBox
id="lbSingle"
autoPostBack="True"
rows="6"
selectionMode="Single"
onSelectedIndexChanged="lbSingle_SelectedIndexChanged"
runat="server" />

<br/>
<asp:Label ID="lblLbSingle" Runat="server" />
<br/>

<h3>ListBox - multiple selection</h3>
<asp:ListBox
id="lbMulti"
autoPostBack="True"
selectionMode="Multiple"
onSelectedIndexChanged="lbMulti_SelectedIndexChanged"
runat="server" />
<asp:Label ID="lblLbMulti" Runat="server" />
</form>
</body>
</HTML>
.... everything compiles but for some reason when the page is brought up
the list box is not populated with the data found in the "books" array.
Any help will be great.

Thanks,

Ben
 
A

Alex Feinman [MVP]

Wrong group.
As for you code, remove the following from the Rage directive:

Codebehind="WebForm1.aspx.cs" AutoEventWireup="false"
Inherits="CH5_ListBoxes.WebForm1"


--
Alex Feinman
---
Visit http://www.opennetcf.org
google_me said:
Hi,
I was doing some C#/ASP.NET examples from an O'Reilly book.

I have this sample of code :
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs"
AutoEventWireup="false" Inherits="CH5_ListBoxes.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>ASP List Box Control</title>
<script runat="server">
void Page_Load(Object sender, EventArgs e)
{
// Put user code to initialize the page here
if (!IsPostBack)
{
// Build 2 dim array for the lists
// First dimension contains book name
// 2nd dimension contains ISBN number

string[,] books = {
{"Programming C#", "0596001177"},
{"Programming ASP.NET", "0596001711"},
{"WebClasses From Scratch", "0789721260"},
{"Teach Yourself C++", "06723072X"},
{"Teach Yourself C++ in 10 Minutes", "067231603X"},
{"XML & Java from Scratch", "0789724766"},
{"Complete Idiots Guide to a Career in Computer Programming",
"0789719959"},
{"XML Web Documents from Scratch", "0789723166"},
{"Clouds to Code", "1861000952"},
{"C++: An Introduction to Programming", "1575760614"},
{"C++ Unleased", "06723122395"}
};
// Populate the Lists
int i;
for (i=0; i < books.GetLength(0); i++)
{
//add both text and value
lbSingle.Items.Add(new ListItem(books[i,0], books[i,1]));
lbMulti.Items.Add(new ListItem(books[i,0], books[i,1]));
}
}
}

void lbSingle_SelectedIndexChanged(Object Source, EventArgs E)
{
//Check to verify that something has been selected.
if(lbSingle.SelectedIndex != -1)
{
lblLbSingle.Text=lbSingle.SelectedItem.Text + "----> ISBN: " +
lbSingle.SelectedItem.Value;
}
}
void lbMulti_SelectedIndexChanged(Object Source, EventArgs E)
{
string str = "";
foreach(ListItem li in lbMulti.Items)
{
if(li.Selected ==true)
{
str += "<br/>" + li.Text + " ----> ISBN: " + li.Value;
}
}
if (str.Length == 0)
lblLbMulti.Text ="No books selected.";
else
lblLbMulti.Text = str;
}
</script>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<h1>ASP Controls</h1>
<h2>List Box</h2>

<h3>ListBox - single selection</h3>
<asp:ListBox
id="lbSingle"
autoPostBack="True"
rows="6"
selectionMode="Single"
onSelectedIndexChanged="lbSingle_SelectedIndexChanged"
runat="server" />

<br/>
<asp:Label ID="lblLbSingle" Runat="server" />
<br/>

<h3>ListBox - multiple selection</h3>
<asp:ListBox
id="lbMulti"
autoPostBack="True"
selectionMode="Multiple"
onSelectedIndexChanged="lbMulti_SelectedIndexChanged"
runat="server" />
<asp:Label ID="lblLbMulti" Runat="server" />
</form>
</body>
</HTML>
... everything compiles but for some reason when the page is brought up
the list box is not populated with the data found in the "books" array.
Any help will be great.

Thanks,

Ben
 
G

google_me

Thank you and sorry for posting in the wrong group. I'm new to this
news group. What group should i post it in?

Thanks,

Ben
 
G

google_me

Can someone tell me why removing those certain attributes allows my
code to run?

thanks,

Ben
 
A

Alex Feinman [MVP]

AutoEventWireup set to false prevents ASP.NET from automatically attaching
functions like Page_Load to the corresponding page object events. Because of
that, none of your event handlers was actually called and thus listboxes
were not populated. The Codebehind and Inherits were most likely inserted by
Studio when you opened the page for editing.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top