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,
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.