Posted: 7/29/2010
Hello Experts,
There are two buttons and one label.I want to know that how many click on a button by ArrayList.But due to postback i can't store the click in ArrayList because everytime ArrayList creates a new instance.I don't want to use ViewState.
Is there any way to do this?
here is my code -
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Untitled Page</title> </head> <body> <form id="form1" runat="server"> <div> <asp:Button ID="btn" runat="server" onclick="btn_Click" Text="click" /> <asp:Button ID="btnShow" runat="server" onclick="btnShow_Click" Text="show click count" /> <asp:Label ID="lbl" runat="server"></asp:Label> </div> </form> </body> </html>
using System; using System.Collections; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; public partial class ArrayListTest : System.Web.UI.Page { private ArrayList arrList = new ArrayList(); protected void Page_Load(object sender, EventArgs e) { } protected void btn_Click(object sender, EventArgs e) { arrList.Add(1); } protected void btnShow_Click(object sender, EventArgs e) { lbl.Text = arrList.Count.ToString(); } }
Thanx in advance.
Mohit,
You are living in stateless protocol world so each request in web is independent of previous one . You don't wan't to use Viewstate fine then go for input hidden control and increase the counter there in it's value property.
Hi Mohit,
Raghav is correct.. Since a web page is *stateless* then you'll need to store the values on some storage so that you can reference the values there on each and every postbacks. If you are trying to count the number of clicks in the buttonand display it on a Label control then you can do something like this:
lbl.Text = (Convert.ToInt32(lbl.Text) + 1).ToString();
Thank you Raghav sir and Vinz.
But is there no way where I can use ArrayList to store click(every time when I click on button)?
mohit said: Thank you Raghav sir and Vinz.But is there no way where I can use ArrayList to store click(every time when I click on button)?
I completely agree with both, Raghav and Vinz. The web page life lasts all until you refresh it. But, of course you have methods to maintain the state like the well-known ViewState, as well as Sessions, and so on.
Sincerely, I don't like the way you want to achieve this. Yes, it's possible to be achieved even with ArrayList, but it will be slower and unnecessary when you have much better methods.
So, here is how you can do this using ArrayList (as you are demanding):
protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { Session.Clear(); Session["array"] = new ArrayList(); } } protected void btn_Click(object sender, EventArgs e) { ArrayList arrList = (ArrayList)Session["array"]; arrList.Add(1); Session["array"] = arrList; } protected void btnShow_Click(object sender, EventArgs e) { ArrayList currentArrayList = (ArrayList)Session["array"]; lbl.Text = currentArrayList.Count.ToString(); }
So, I'm using Session to maintain the current state of your ArrayList
the same method using ViewState
protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { ViewState["array"] = new ArrayList(); } } protected void btn_Click(object sender, EventArgs e) { ArrayList arrList = (ArrayList)ViewState["array"]; arrList.Add(1); ViewState["array"] = arrList; } protected void btnShow_Click(object sender, EventArgs e) { ArrayList currentArrayList = (ArrayList)ViewState["array"]; lbl.Text = currentArrayList.Count.ToString(); }
And the best way that Raghav & Vinz has already told you about - using HiddenField (less code, easier, more efficient - indeed!)
Add hidden field in your ASPX
<asp:HiddenField ID="totalClicks" runat="server" Value="0" />
protected void Page_Load(object sender, EventArgs e) { // } protected void btn_Click(object sender, EventArgs e) { totalClicks.Value = (Convert.ToInt32(totalClicks.Value)+1).ToString(); } protected void btnShow_Click(object sender, EventArgs e) { lbl.Text = totalClicks.Value; }
I hope this has helped you understand the point why I agree with Raghav & Vinz :)
Regards,Hajan
Thank you so much Hajan.
Actually Yesterday I was reading about ArrayList. So I wanted to use ArrayList in this thread without any use of state management.Now I got that I can't do this without help of state management.
Thank you again Hajan
mohit said: I wanted to use ArrayList
I wanted to use ArrayList
Why at this time you are using arraylist ? It is harmful since it will perform boxing and unboxing. Go with generics in this world.
raghav_khunger said: Why at this time you are using arraylist ? It is harmful since it will perform boxing and unboxing. Go with generics in this world.
Thank you sir.Today I will enter in generics world.