Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Confused out of my minde

Confused out of my minde

Scheduled Pinned Locked Moved ASP.NET
csharpasp-netdatabasedesignsecurity
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tcombs07
    wrote on last edited by
    #1

    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
    
    T A 2 Replies Last reply
    0
    • T tcombs07

      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
      
      T Offline
      T Offline
      tcombs07
      wrote on last edited by
      #2

      Just wanted to add, Thanks in advance to anyone who may have suggestions Travis Combs

      N 1 Reply Last reply
      0
      • T tcombs07

        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
        
        A Offline
        A Offline
        AlexeiXX3
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • T tcombs07

          Just wanted to add, Thanks in advance to anyone who may have suggestions Travis Combs

          N Offline
          N Offline
          NeverHeardOfMe
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups