Please Help: ListBox.SelectedItem returns null

O

Ohad Asor

Hello all,

I have an ASP.NET page I've written using VS.NET2003, which have a ListBox
in it. When I press a button in the form, I try to get the selected item in
the list by calling ListBox.SelectedItem. And it returns null, even when I
select an item.

Even when I try to read the value when I get the SelectedIndexChanged event,
it still doesn't give me the correct answer.

What can I do?

Thanks a lot!
Ohad Asor
Please cc to my mailbox:
(e-mail address removed)
 
E

Eliyahu Goldin

Ohad,

Are you sure you are not re-populating the listbox on postback? In other
words, are you checking IsPostBack property?

Eliyahu
 
O

Ohad Asor

Thanks for your reply.

Here is the source code:

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<script runat="server">


protected void Page_Load(object sender, EventArgs e)

{

System.Xml.Serialization.XmlSerializer xs = new
System.Xml.Serialization.XmlSerializer(typeof(ArrayList));

ArrayList users = new ArrayList();

ArrayList projs = new ArrayList();

System.IO.FileStream fs = System.IO.File.Open("/projects.xml",
System.IO.FileMode.Open);

projs = xs.Deserialize(fs) as ArrayList;

fs.Close();

xs = new System.Xml.Serialization.XmlSerializer(typeof(ArrayList));

fs = System.IO.File.Open("/users.xml", System.IO.FileMode.Open);

users = xs.Deserialize(fs) as ArrayList;

fs.Close();

while (lstUsers.Items.Count > 0)

lstUsers.Items.Remove(lstUsers.Items[0]);

while (lstProjects.Items.Count > 0)

lstProjects.Items.Remove(lstProjects.Items[0]);

foreach (object o in users)

lstUsers.Items.Add(o as string);

foreach (object o in projs)

lstProjects.Items.Add(o as string);

fs.Close();

}

protected void btnGenerate_Click(object sender, EventArgs e)

{

// here is the problem: selecteditem.text is null

string s = string.Format("c:\\{0}\\{1}.{2}", lstProjects.SelectedItem.Text,

lstUsers.SelectedItem.Text, txtHours.Text);


System.IO.FileStream fs = System.IO.File.Open(s,
System.IO.FileMode.OpenOrCreate,

System.IO.FileAccess.Write);

fs.Close();

}

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

<title>Untitled Page</title>

</head>

<body>

<form id="form1" runat="server">

<div>

&nbsp;

<asp:ListBox ID="lstProjects" runat="server"></asp:ListBox>

<asp:ListBox ID="lstUsers" runat="server"></asp:ListBox>

<asp:Label ID="txtHours" runat="server" Text="Hours:"></asp:Label>

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><asp:Button
ID="btnGenerate"

runat="server" Text="Generate" OnClick="btnGenerate_Click" />

<asp:Label ID="Label1" runat="server" Height="15px"
Width="475px"></asp:Label>


</div>

</form>

</body>

</html>
 
J

John.Net

Remember that when you fire a postback event, the page reloads. So,
PageLoad will process, and then your triggered event will fire. If you
do not have ViewState enabled for the dropdown list, it will be reset
to the original (unselected) state when the form reloads.

Good luck!
 
E

Eliyahu Goldin

Ohad,

That's exactly what I've told you. In Page_Load you are not checking
IsPostBack property. As a result, code

while (lstUsers.Items.Count > 0)
lstUsers.Items.Remove(lstUsers.Items[0]);

runs every postback. btnGenerate_Click gets called after Page_Load and
Page_Load removes all old items from the lstUsers and re-populates it from
users. You should make sure taht ViewState is enabled for lstUsers and
re-populate it only on the first load. Property IsPostBack tells you if you
are in the first load or in a postback.

Eliyahu

Ohad Asor said:
Thanks for your reply.

Here is the source code:

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<script runat="server">


protected void Page_Load(object sender, EventArgs e)

{

System.Xml.Serialization.XmlSerializer xs = new
System.Xml.Serialization.XmlSerializer(typeof(ArrayList));

ArrayList users = new ArrayList();

ArrayList projs = new ArrayList();

System.IO.FileStream fs = System.IO.File.Open("/projects.xml",
System.IO.FileMode.Open);

projs = xs.Deserialize(fs) as ArrayList;

fs.Close();

xs = new System.Xml.Serialization.XmlSerializer(typeof(ArrayList));

fs = System.IO.File.Open("/users.xml", System.IO.FileMode.Open);

users = xs.Deserialize(fs) as ArrayList;

fs.Close();

while (lstUsers.Items.Count > 0)

lstUsers.Items.Remove(lstUsers.Items[0]);

while (lstProjects.Items.Count > 0)

lstProjects.Items.Remove(lstProjects.Items[0]);

foreach (object o in users)

lstUsers.Items.Add(o as string);

foreach (object o in projs)

lstProjects.Items.Add(o as string);

fs.Close();

}

protected void btnGenerate_Click(object sender, EventArgs e)

{

// here is the problem: selecteditem.text is null

string s = string.Format("c:\\{0}\\{1}.{2}", lstProjects.SelectedItem.Text,

lstUsers.SelectedItem.Text, txtHours.Text);


System.IO.FileStream fs = System.IO.File.Open(s,
System.IO.FileMode.OpenOrCreate,

System.IO.FileAccess.Write);

fs.Close();

}

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

<title>Untitled Page</title>

</head>

<body>

<form id="form1" runat="server">

<div>

&nbsp;

<asp:ListBox ID="lstProjects" runat="server"></asp:ListBox>

<asp:ListBox ID="lstUsers" runat="server"></asp:ListBox>

<asp:Label ID="txtHours" runat="server" Text="Hours:"></asp:Label>

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><asp:Button
ID="btnGenerate"

runat="server" Text="Generate" OnClick="btnGenerate_Click" />

<asp:Label ID="Label1" runat="server" Height="15px"
Width="475px"></asp:Label>


</div>

</form>

</body>

</html>



Eliyahu Goldin said:
Ohad,

Are you sure you are not re-populating the listbox on postback? In other
words, are you checking IsPostBack property?

Eliyahu

item
in
 
J

John.Net

In your Page_Load you could add

if not page.ispostback then
-- load initial values for dropdown here --
-- that way it only 'loads' the values if it is a brand new page -
not during a postback
end if

good luck
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top