listbox selections not being set as true

Discussion in 'ASP.NET / ASP.NET Core' started by solarissf, Apr 12, 2012.

  1. Hi guys,

    I have a asp.net webpage,
    When user uploads csv file, I read it in xml and populate listbox.

    When I select multiple choices for listbox, and when I hit RUN BUTTON(this is BEGIN PROCESS method)... the items I selected are not being registered as selected. I think it has something to do with postback but I do not fully understands the concept.

    any idea why its not being selected as true?

    just to give more details... on page load nothing happens. User selects file to upload... when the user hits button to upload, listbox is populated. Then I want user to select multiple items from listbox, and "do something" with the selected choices. Problem is selections are not being marked as true.

    please help

    Code:
        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            if (FileUpload1.HasFile)
            {
                try
                {
                    string uploadfilename = Path.GetFileName(FileUpload1.FileName);
                    FileUpload1.SaveAs(Server.MapPath("UserUploads/") + uploadfilename);
                    StatusLabel.Text = "Upload status: File uploaded!";
                    PopulateManagerList();
                    File.Delete(Server.MapPath("UserUploads/Comma.csv"));
                    
                }
                catch (Exception ex)
                {
                    StatusLabel.Text = "Upload status: The file could not be uploaded.";
                }
            }
        }
    
        protected void PopulateManagerList()
        {
    
    
                char separator = ',';
                StreamReader sr = new StreamReader(Server.MapPath("UserUploads/comma.csv"));
                string line;
                List<string> rows = new List<string>();
    
                while ((line = sr.ReadLine()) != null)
                {
                    rows.Add(line);
                    line = sr.ReadLine();
                }
    
                List<string[]> rowObjects = new List<string[]>();
                int maxColsCount = 0;
                foreach (string s in rows)
                {
                    string[] convertedRow = s.Split(new char[] { separator });
                    if (convertedRow.Length > maxColsCount)
                        maxColsCount = convertedRow.Length;
                    rowObjects.Add(convertedRow);
                }
    
    
                table = new DataTable("myNewTable");
                for (int i = 0; i < maxColsCount; i++)
                {
                    table.Columns.Add(new DataColumn());
                }
    
                foreach (string[] rowArray in rowObjects)
                {
                    table.Rows.Add(rowArray);
                }
                table.AcceptChanges();
    
                GridView1.DataSource = table;
                GridView1.DataBind();
    
                //Load names into Listbox from 1st row in datatable
                foreach (string itemz in table.Rows[0].ItemArray)
                {
                    ListBox1.Items.Add(itemz);
                }
                ListBox1.Items.RemoveAt(0);
    
                table.WriteXml(Server.MapPath("UserUploads/comma.xml"));
                //table.WriteXml(memStm);
                //memStm.Seek(0, System.IO.SeekOrigin.Begin);
    
                sr.Close();
               // string results = Request.Form[ListBox1.UniqueID]; 
        }
    
        protected void BeginProcess()
        {
            DataSet dataSet = new DataSet();
            DataTable table2;
            //memStm.Seek(0, System.IO.SeekOrigin.Begin);
            //dataSet.ReadXml(memStm);
            dataSet.ReadXml(Server.MapPath("UserUploads/comma.xml"));
            GridView2.DataSource = dataSet;
            GridView2.DataBind();
    
            //File.Delete(Server.MapPath("UserUploads/Comma.xml"));
    
            table2 = dataSet.Tables[0];
            GridView3.DataSource = dataSet;
            GridView3.DataBind();
    
            //go through listbox1 to find selected managers = selectedManagersList
            List<string> selectedManagersList = new List<string>();
    
            foreach (ListItem strItem in ListBox1.Items)
            {
                if (strItem.Selected)
                {
                    selectedManagersList.Add(strItem.Value);
                }
    
            }
    
            // go through each column and find selected managers... return column number
            int test1;
            List<int> managerColumn = new List<int>();
            string managerName;
            foreach (string item1 in selectedManagersList)
            {
                managerName = item1;
                foreach (string item2 in table2.Rows[0].ItemArray)
                {
                    if (item2 == managerName)
                    {
                        string counter = table2.Rows[0].ItemArray.ToString();
                    }
                }
            }
    
            string manager = "Ssaris Div";
    
                foreach (DataColumn myCol in table2.Columns)
                {
                    string temp = table2.Rows[0][myCol].ToString();
                    if (temp == manager)
                    {
                        int tempcounter = myCol.Ordinal;
                    }
                }
        }
     
  2. SOLVED... there was a back form<tag> that was posting to a incorrect page
     

Share This Page