#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2004
    Posts
    81
    Rep Power
    10

    Not totalling the items


    guys need help i am new to asp.net programming and i cant figure out what could possibly the solution.

    ihave two listboxes. the first one contains the items to be selected and added to the other listbox. When i click the add button, the program should count the items that was added on the second list box. I want to put the count in a textbox

    here is the code: c#

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public partial class _Default : System.Web.UI.Page

    {
    int count = 0;
    int x;
    protected void Page_Load(object sender, EventArgs e)
    {
    if (!(IsPostBack)){
    lst1.Items.Add("Red");
    lst1.Items.Add("Blue");
    lst1.Items.Add("Yellow");
    lst1.Items.Add("Black");
    }
    }


    protected void add(object sender, EventArgs e)
    {
    lst2.Items.Add(lst1.SelectedItem.Text);

    count = count + 1;


    }
    protected void btnRemove_Click(object sender, EventArgs e)
    {
    lst2.Items.Remove(lst2.SelectedItem);

    }

    }

    need help please
    Last edited by excaliburdotcom; June 19th, 2012 at 10:12 PM.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2004
    Posts
    81
    Rep Power
    10
    help please
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2011
    Posts
    18
    Rep Power
    0

    the count wont update


    your add method adds the selected item to second list box and increments the count variable.. But page level variables loose their value after postback roundtrip cause they are reinitialised every time your page loads.. so your count variable will always hold the value of 0 and when incremented, 1. This wont work...
    Why not just use
    Code:
    listbox2.items.count
    to get the count of items in second listbox?
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2004
    Posts
    81
    Rep Power
    10
    Originally Posted by cchy1988
    your add method adds the selected item to second list box and increments the count variable.. But page level variables loose their value after postback roundtrip cause they are reinitialised every time your page loads.. so your count variable will always hold the value of 0 and when incremented, 1. This wont work...
    Why not just use
    Code:
    listbox2.items.count
    to get the count of items in second listbox?
    is there any remedy if i would choose to use a counter variable? thanks
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2004
    Posts
    81
    Rep Power
    10
    is there any remedy if i would choose to use a counter variable? just in javascript? thanks

IMN logo majestic logo threadwatch logo seochat tools logo