2009-05-18 68 views
2

這是我寫的一段代碼。我遇到的問題是: 當我點擊gridview中的一個按鈕「rowcommand」將項目添加到工作正常的數組列表。 用戶點擊按鈕後,頁面再次加載,它會繼續「rowcommand」再次!結果將相同的值添加到數組列表。pageload in c#

這是關於回發的東西嗎?如果這是我不認爲我已經清楚足夠清楚!這裏似乎有什麼錯誤?

//編輯2:整個代碼塊

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

    } 
} 

感謝,

+0

在代碼中有很多失敗,經典的糟糕的webforms代碼。我真的建議你看看通過會話傳遞參數和替代webforms的替代方案 – 2009-05-21 18:34:48

回答

1

嗯,我找到了答案了它,它很簡單。這似乎是一個問題,當ButtonType =「按鈕」。

該問題可以通過將ButtonType更改爲「Link」來解決。

只要你有興趣,這裏是幫助我的鏈接。

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

用戶後再次加載頁面點擊按鈕,並將其變爲 「rowcommand」 了!

我沒有看到問題。用戶點擊了按鈕,因此它處理了事件。你期望它做什麼?

請記住,每一個崗位回—和所有事件強制執行完整的回發—指運行整個頁面生命週期,包括你的網頁加載事件,除了可能引發的回發的任何事件。此外,您正在爲每個帖子回覆一個新類的實例。


因此,在第一個按鈕按一下您所期待的網頁被重定向並最終別的地方?

+0

我也不清楚問題是什麼。 – 2009-05-18 21:56:50

2

它正在做它假設的。首先處理頁面加載事件,然後處理Row Command事件。

你看到的是兩個事件正在一個接一個地處理。

此外結帳回調與回傳。

像Joel說的那樣,Postback將會使你的頁面無效並重建。回調使用Javascript/AJAX __doPostBack()並且不會使整個頁面失效,只是您的回調組件或容器。雖然頁面加載事件仍然被調用,但您可以測試IsCallback屬性。

此鏈接可以幫助... http://msdn.microsoft.com/en-us/library/ms178141.aspx