By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,034 Members | 1,777 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,034 IT Pros & Developers. It's quick & easy.

Please Help: ListBox.SelectedItem returns null

P: n/a
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:
oh******@netvision.net.il
Nov 19 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Can you get the value like this?

str myString = listbox.SelectedValue.ToString();

"Ohad Asor" wrote:
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:
oh******@netvision.net.il

Nov 19 '05 #2

P: n/a
Ohad,

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

Eliyahu

"Ohad Asor" <oh******@netvision.net.il> wrote in message
news:da**********@news2.netvision.net.il...
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:
oh******@netvision.net.il

Nov 19 '05 #3

P: n/a
Thanks for responsing.
Still not working.
"tperri" <tp****@discussions.microsoft.com> wrote in message
news:AF**********************************@microsof t.com...
Can you get the value like this?

str myString = listbox.SelectedValue.ToString();

"Ohad Asor" wrote:
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:
oh******@netvision.net.il

Nov 19 '05 #4

P: n/a
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!

Nov 19 '05 #5

P: n/a
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(Arra yList));

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(Arra yList));

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" <re*************@monarchmed.com> wrote in message
news:%2***************@TK2MSFTNGP15.phx.gbl...
Ohad,

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

Eliyahu

"Ohad Asor" <oh******@netvision.net.il> wrote in message
news:da**********@news2.netvision.net.il...
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:
oh******@netvision.net.il


Nov 19 '05 #6

P: n/a
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" <oh******@netvision.net.il> wrote in message
news:da**********@news2.netvision.net.il...
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(Arra yList));

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(Arra yList));

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" <re*************@monarchmed.com> wrote in message
news:%2***************@TK2MSFTNGP15.phx.gbl...
Ohad,

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

Eliyahu

"Ohad Asor" <oh******@netvision.net.il> wrote in message
news:da**********@news2.netvision.net.il...
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:
oh******@netvision.net.il



Nov 19 '05 #7

P: n/a
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

Nov 19 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.