.Net Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming Languages - More.Net Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 19th, 2012, 07:25 PM
excaliburdotcom excaliburdotcom is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 81 excaliburdotcom User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 2 h 6 m 19 sec
Reputation Power: 9
Send a message via Yahoo to excaliburdotcom
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.

Reply With Quote
  #2  
Old June 19th, 2012, 10:13 PM
excaliburdotcom excaliburdotcom is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 81 excaliburdotcom User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 2 h 6 m 19 sec
Reputation Power: 9
Send a message via Yahoo to excaliburdotcom
help please

Reply With Quote
  #3  
Old June 20th, 2012, 06:24 AM
cchy1988 cchy1988 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2011
Posts: 18 cchy1988 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 49 m 19 sec
Reputation 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?

Reply With Quote
  #4  
Old June 24th, 2012, 10:07 PM
excaliburdotcom excaliburdotcom is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 81 excaliburdotcom User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 2 h 6 m 19 sec
Reputation Power: 9
Send a message via Yahoo to excaliburdotcom
Quote:
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

Reply With Quote
  #5  
Old June 24th, 2012, 10:12 PM
excaliburdotcom excaliburdotcom is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 81 excaliburdotcom User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 2 h 6 m 19 sec
Reputation Power: 9
Send a message via Yahoo to excaliburdotcom
is there any remedy if i would choose to use a counter variable? just in javascript? thanks

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - More.Net Development > Not totalling the items

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap