tags:

views:

1609

answers:

3

Here is a piece of code that I've written. The problem that I'm having is: When i click a button in the gridview "rowcommand" adds the item to an arraylist which works fine. After the user clicks the button the page loads again and it goes to "rowcommand" AGAIN! As a result the same value is added to the arraylist.

Is this something regarding postback? if it's I dont think I've understood it clearly enough! what seems to be wrong here?

//edit 2: entire code block

public partial class Action_k : System.Web.UI.Page
{
    ArrayList array;
    ArrayList tmpArrayList = new ArrayList();
    string itemIDs = string.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack == false)
        {
            if (Session["username"] == null)
            {
                Session["anonuser"] = "anon";

                Label1.Text = "";
                userLabel.Text = "";
                ImageButton1.ImageUrl = "~/images/logink.gif";
                ImageButton1.PostBackUrl = "~/Login_k.aspx";
            }
            else
            {
                userLabel.Text = Session["username"].ToString();
                Label1.Text = "Your logged in as: ";
                ImageButton1.ImageUrl = "~/images/logoutk.gif";
            }

            if (Session["array"] == null)
            {
                array = new ArrayList();
                Session.Add("array", array);
            }

        }
        array = Session["array"] as ArrayList;
    }

    public void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
    {

        if (e.CommandName == "AddToCart")
        {
            int index = Convert.ToInt32(e.CommandArgument);
            string items = GridView2.DataKeys[index].Value.ToString();
            array.Add(items);
            Response.Redirect("ShoppingCart_k.aspx?itemID=" + items);
        }

    }
}

Thanks,

A: 
Joel Coehoorn
I, too, am unclear on exactly what the issue is.
Jon Freeland
@Joel, It skips going to page mentioned in "rowcommand" when the button is clicked for the 1st time and loads the page. After it loads for the 2nd time, it goes to the page mentioned.Therefore the value is added twice to the arraylist. Which is not I want.
pier
+1  A: 

It's doing what it's suppose to. First your page load event is handled, then your Row Command event is handled.

What you're seeing is that two events are being handled, one after the other.

Also checkout Callbacks versus Postbacks.

Like Joel said, a Postback is going to invalidate and rebuild your page. A Callback uses Javascript/AJAX __doPostBack() and does not invalidate the entire page, just your callback component or container. A page load event is still called though, but you can test for the IsCallback property.

This link may help... http://msdn.microsoft.com/en-us/library/ms178141.aspx

kervin
A: 

Well I found the answer for it, and it was simple. It seems to be a issue when ButtonType="Button".

The problem can be resolved by changing the ButtonType to "Link".

Just incase if your interested here's the link that helped me out.

http://forums.asp.net/p/1002747/1324414.aspx#1324414
pier