2013-03-27 43 views
0

我有一個數據綁定到gridview的對象列表。 gridview有一個連接到該行的刪除按鈕。在gridview中禁用buttonfields/commandfield的回傳

我需要的對象列表,通過點擊堅持(好像脫機工作) 我不希望使用會話或cookie或viewstates

當前的行爲: 列表中包含了4項=>刪除1項=>列表有3項=>刪除1個項目(回發)列表獲取與4項再生然後刪除1.

class Emails 
{ 
    public string Email { get; set; } 
    public Emails(string _Address) 
    { 
     Email = _Address; 
    } 
} 


     if (!IsPostBack) 
     { 
      ListOfEmails = new List<Emails>(); 
      ListOfEmails.Add(new Emails("[email protected]")); 
      ListOfEmails.Add(new Emails("[email protected]")); 
      ListOfEmails.Add(new Emails("[email protected]")); 
      ListOfEmails.Add(new Emails("[email protected]")); 

      GridView1.DataSource = ListOfEmails; 
      GridView1.DataBind(); 
     } 

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) 
    { 
     string EmailToBeDeleted = GridView1.Rows[e.RowIndex].Cells[0].Text; 
     ListOfEmails.Remove(ListOfEmails.Find(L => L.Email == EmailToBeDeleted)); 
     GridView1.DataSource = null; 
     GridView1.DataSource = ListOfEmails; 
     GridView1.DataBind(); 
    } 

ASPX:

 <asp:BoundField DataField="Email" HeaderText="Email Address" /> 
     <asp:ButtonField ButtonType="Button" CommandName="Delete" HeaderText="Action2" 
      ShowHeader="True" Text="Delete" />  
     <asp:CommandField ButtonType="Image" DeleteImageUrl="~/delete.gif" 
      ShowDeleteButton="True" /> 

「我現在有2個刪除按鈕」

+0

把它們放在updatepanel中? – 2013-03-27 11:00:36

+0

這隻會抑制帖子後退或讓他們不可見我希望禁用它們,直到我明確命令回發。 – AngelicCore 2013-03-27 11:04:04

回答

0

網頁是無狀態的。如果您重新加載頁面,則會重新創建所有控件和數據。這就是爲什麼我們使用諸如Viewstates和Session變量之類的東西來保持post/gets之間的數據。使用其他技術(如緩存和更新面板)只會隱藏您正在使用視圖狀態的事實。我能想到的唯一選擇是讀/寫某些數據源(數據庫或平面文件,如XML),但使用視圖狀態或會話時,這會花費更多。也許如果你解釋你想避免這些選擇的理由,我們可以提出一個解決方案?

+0

我不希望使用會話/視圖狀態,因爲我希望以另一種方式進行操作。 我要像你那樣做會在桌面上.NET 也就是說列表(在內存中)上工作,而頁面後背上做,只在需要 – AngelicCore 2013-03-27 11:48:50

+0

桌面應用程序和Web應用程序是完全不同的時候做他們。桌面應用程序允許在GUI和服務器/應用程序代碼之間進行無縫通信。正如我上面提到的,HTTP是無狀態的。這意味着服務器代碼和在用戶Web瀏覽器上呈現的代碼之間沒有通信。這種溝通是通過視圖狀態,會話等完成的。您可以使用這些對象來使應用程序看起來像桌面應用程序,但它不會是真正的應用程序。 – jason 2013-03-27 11:55:36

+0

我明白,我通過使用會話持久性來解決它。 我想要的行爲,雖然是模仿桌面應用程序,但不使用回發,直到你完成,即像一個保存按鈕 – AngelicCore 2013-03-27 13:20:05