Loading ...

ArrayList in C Sharp.

Who is online?  0 guests and 0 members
home  »  forums   »  asp.net topics   »  getting started / general asp.net   » ArrayList in C Sharp.

ArrayList in C Sharp.

Posts under the topic: ArrayList in C Sharp.

Posted: 7/29/2010

Contributor 2237  points  Contributor
  • Joined on: 9/24/2009
  • Posts: 172

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.


Posted: 7/29/2010

Guru 16813  points  Guru
  • Joined on: 4/19/2009
  • Posts: 490
  Answered

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.


Posted: 7/29/2010

Professional 8338  points  Professional
  • Joined on: 4/15/2009
  • Posts: 424
  Answered

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();



Posted: 7/29/2010

Contributor 2237  points  Contributor
  • Joined on: 9/24/2009
  • Posts: 172

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)?


Posted: 7/29/2010

Professional 8505  points  Professional
  • Joined on: 5/3/2010
  • Posts: 391
  Answered

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" />

then, your code-behind will be:

        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


Posted: 7/29/2010

Contributor 2237  points  Contributor
  • Joined on: 9/24/2009
  • Posts: 172

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 HajanSmile 


Posted: 7/29/2010

Guru 16813  points  Guru
  • Joined on: 4/19/2009
  • Posts: 490

mohit said:

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. 


Posted: 7/29/2010

Contributor 2237  points  Contributor
  • Joined on: 9/24/2009
  • Posts: 172

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.


Page 1 of 1 (8 items)