Confused out of my minde
-
Hey guys i am hoping you guys can help, i have been working on this project with i thought would be very simple, but anyways here is what i got. I have a listbox, text box, add and remove buttons. I type in the text box click add and then the item adds to the list box and then gets save to a text file, what i can't figure out is how to remove the items from the list box. i have done this several times in a windows application but i can't seem to get it to work in a asp.net web app. When i comment out the page load procedure i am then able to remove items from the list box. here is the code that i have:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;namespace WMS_WebConfig
{
public partial class _Default : System.Web.UI.Page
{
private StreamWriter writer;
private StreamReader reader;
int index;protected void Page\_Load(object sender, EventArgs e) { monitoredSitesLB.Items.Clear(); if (!Directory.Exists("C:\\\\Program Files\\\\Virasec, LLC\\\\WMS")) { // Create the directory it does not exist. Directory.CreateDirectory("C:\\\\Program Files\\\\Virasec, LLC\\\\WMS"); } if (!File.Exists("C:\\\\Program Files\\\\Virasec, LLC\\\\WMS\\\\monitoredsites.txt")) { writer = new StreamWriter("C:\\\\Program Files\\\\Virasec, LLC\\\\WMS\\\\monitoredsites.txt", true); writer.Close(); } reader = new StreamReader("C:\\\\Program Files\\\\Virasec, LLC\\\\WMS\\\\monitoredsites.txt"); string line; while ((line = reader.ReadLine()) != null) { monitoredSitesLB.Items.Add(line); } index = monitoredSitesLB.SelectedIndex; reader.Close(); } protected void addBtn\_Click(object sender, EventArgs e) { ListItem item = new ListItem(urlTxtBox.Text); if (monitoredSitesLB.Items.Contains(item)) { statusLbl.Text = "The website you entered is currently being monitored. " + "Please enter another site to monitor."; } else
-
Hey guys i am hoping you guys can help, i have been working on this project with i thought would be very simple, but anyways here is what i got. I have a listbox, text box, add and remove buttons. I type in the text box click add and then the item adds to the list box and then gets save to a text file, what i can't figure out is how to remove the items from the list box. i have done this several times in a windows application but i can't seem to get it to work in a asp.net web app. When i comment out the page load procedure i am then able to remove items from the list box. here is the code that i have:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;namespace WMS_WebConfig
{
public partial class _Default : System.Web.UI.Page
{
private StreamWriter writer;
private StreamReader reader;
int index;protected void Page\_Load(object sender, EventArgs e) { monitoredSitesLB.Items.Clear(); if (!Directory.Exists("C:\\\\Program Files\\\\Virasec, LLC\\\\WMS")) { // Create the directory it does not exist. Directory.CreateDirectory("C:\\\\Program Files\\\\Virasec, LLC\\\\WMS"); } if (!File.Exists("C:\\\\Program Files\\\\Virasec, LLC\\\\WMS\\\\monitoredsites.txt")) { writer = new StreamWriter("C:\\\\Program Files\\\\Virasec, LLC\\\\WMS\\\\monitoredsites.txt", true); writer.Close(); } reader = new StreamReader("C:\\\\Program Files\\\\Virasec, LLC\\\\WMS\\\\monitoredsites.txt"); string line; while ((line = reader.ReadLine()) != null) { monitoredSitesLB.Items.Add(line); } index = monitoredSitesLB.SelectedIndex; reader.Close(); } protected void addBtn\_Click(object sender, EventArgs e) { ListItem item = new ListItem(urlTxtBox.Text); if (monitoredSitesLB.Items.Contains(item)) { statusLbl.Text = "The website you entered is currently being monitored. " + "Please enter another site to monitor."; } else
-
Hey guys i am hoping you guys can help, i have been working on this project with i thought would be very simple, but anyways here is what i got. I have a listbox, text box, add and remove buttons. I type in the text box click add and then the item adds to the list box and then gets save to a text file, what i can't figure out is how to remove the items from the list box. i have done this several times in a windows application but i can't seem to get it to work in a asp.net web app. When i comment out the page load procedure i am then able to remove items from the list box. here is the code that i have:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;namespace WMS_WebConfig
{
public partial class _Default : System.Web.UI.Page
{
private StreamWriter writer;
private StreamReader reader;
int index;protected void Page\_Load(object sender, EventArgs e) { monitoredSitesLB.Items.Clear(); if (!Directory.Exists("C:\\\\Program Files\\\\Virasec, LLC\\\\WMS")) { // Create the directory it does not exist. Directory.CreateDirectory("C:\\\\Program Files\\\\Virasec, LLC\\\\WMS"); } if (!File.Exists("C:\\\\Program Files\\\\Virasec, LLC\\\\WMS\\\\monitoredsites.txt")) { writer = new StreamWriter("C:\\\\Program Files\\\\Virasec, LLC\\\\WMS\\\\monitoredsites.txt", true); writer.Close(); } reader = new StreamReader("C:\\\\Program Files\\\\Virasec, LLC\\\\WMS\\\\monitoredsites.txt"); string line; while ((line = reader.ReadLine()) != null) { monitoredSitesLB.Items.Add(line); } index = monitoredSitesLB.SelectedIndex; reader.Close(); } protected void addBtn\_Click(object sender, EventArgs e) { ListItem item = new ListItem(urlTxtBox.Text); if (monitoredSitesLB.Items.Contains(item)) { statusLbl.Text = "The website you entered is currently being monitored. " + "Please enter another site to monitor."; } else
Web apps are different from win apps I got confused too when moved from win to web In the page load, you need to include all that code inside a page.ispostback condition so it will only execute the first time and not when you are adding or removing items
if (!page.ispostback)
{
//Code that needs to be executed only the first time
}Alexei Rodriguez
-
You haven't understood the life-cycle of an asp.net page. Every time you click either button a postback occurs and the page is re-loaded, thus re-setting your "index" variable. You should include a if (!Page.IsPostBack) {...} block surrounding your Page_Load event code to prevent this happening. The listbox will be populated by the page's ViewState instead and the selected item's index preserved. Google it.