|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Please Help: ListBox.SelectedItem returns nullHello 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: ohada***@netvision.net.il Can you get the value like this?
str myString = listbox.SelectedValue.ToString(); Show quoteHide quote "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: > ohada***@netvision.net.il > > > Thanks for responsing.
Still not working. Show quoteHide quote "tperri" <tpe***@discussions.microsoft.com> wrote in message news:AFF3BD92-EE90-4EE4-8802-4D621B3214D4@microsoft.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: >> ohada***@netvision.net.il >> >> >> Ohad,
Are you sure you are not re-populating the listbox on postback? In other words, are you checking IsPostBack property? Eliyahu Show quoteHide quote "Ohad Asor" <ohada***@netvision.net.il> wrote in message news:dat5tl$31b$1@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: > ohada***@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(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> <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> Show quoteHide quote "Eliyahu Goldin" <removemeegol***@monarchmed.com> wrote in message news:%23IaSwHfhFHA.576@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" <ohada***@netvision.net.il> wrote in message > news:dat5tl$31b$1@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: >> ohada***@netvision.net.il >> >> > > 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 Show quoteHide quote "Ohad Asor" <ohada***@netvision.net.il> wrote in message lstProjects.SelectedItem.Text,news:datt22$ac6$1@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(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}", Show quoteHide quote > > 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> > > > > <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" <removemeegol***@monarchmed.com> wrote in message > news:%23IaSwHfhFHA.576@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" <ohada***@netvision.net.il> wrote in message > > news:dat5tl$31b$1@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: > >> ohada***@netvision.net.il > >> > >> > > > > > > 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 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!
Custom control: How to render an embedded image at design-time?
Render and Image on a Button Can aspnet simulate windows pane separator? Web User Controls: Exposing Items property of contained controls Load User Control from different directory Collection with the correct object type on the Collection dialog Tool to monitor/trace all method calls? WinForm as ASP.NET custom control Webcontrol Designer Awareness... DropDownList <optgroup> |
|||||||||||||||||||||||