2013-05-07 91 views
1

我有一個gridview使用列表作爲它的數據源,這個列表填充使用實體框架,並傳遞給我的方法,這裏的數據綁定到我的網格視圖控件。我似乎在編輯一行時遇到問題。GridView的OnRowEditing事件處理程序沒有觸發

在設計師,我已經添加屬性網格視圖有OnRowEditing處理程序,我增加了一個按鈕,進行編輯,但我OnRowEditing事件處理程序不點火。斷點沒有命中。

我的GridView控件

<asp:GridView runat="server" 
      ID="grdNotes" 
      OnRowCommand="grdNotes_RowCommand" 
      AllowPaging="true" 
      AllowSorting="true" 
      EnableTheming="true" 
      AutoGenerateColumns="false" 
      OnPageIndexChanging="grdNotes_PageIndexChanging" 
      OnSorting="grdNotes_Sorting" 
      AlternatingRowStyle-BorderColor="Yellow" 
      PageSize="3" 
      AlternatingRowStyle-BackColor="Yellow" 
      OnRowEditing="grdNotes_RowEditing" 
      OnRowDeleting="grdNotes_RowDeleting" 
      DataKeyNames="NotesID" > 
      <Columns> 
       <asp:BoundField HeaderText="Title" DataField="NotesTitle" SortExpression="NotesTitle"> 
        <ItemStyle Height="20px" Width="150px" /> 
       </asp:BoundField> 

       <asp:BoundField HeaderText="Text" DataField="NotesText" SortExpression="NotesText"> 
        <ItemStyle Height="20px" Width="250px" /> 
       </asp:BoundField> 

      <%--  <asp:ButtonField CommandName="EditRow" DataTextField="Edit" HeaderText="Edit" /> 
       <asp:ButtonField CommandName="DeleteRow" DataTextField="Delete" HeaderText="Delete" />--%> 

       <asp:CommandField ShowEditButton="true" /> 
       <asp:CommandField ShowDeleteButton="true" /> 
       <asp:CommandField ShowCancelButton="true" /> 
      </Columns> 

     </asp:GridView> 

背後

代碼我取回從Page_Init實體框架的數據。我也有全局變量

private List<NotesModel> list = new List<NotesModel>(); 
NotesModel nm = new NotesModel(); 

protected void Page_Init(object sender, EventArgs e) 
    { 
     NoteSearch ns = new NoteSearch(Business.ContextHelper.CurrentContext); 
     string[] urlArray = Request.RawUrl.Split('/'); 
     string t = urlArray[4]; 
     string[] relatedID = t.Split('='); 
     if (!IsPostBack) 
     { 
      // urlArray[3] is profile type , relatedID[1] is ID 
      list = ns.GetBasicNoteResults(nm, urlArray[3], relatedID[1]); 
     } 
     else 
     { 
      urlArray = Request.UrlReferrer.AbsoluteUri.Split('/'); 
      t = urlArray[6]; 
      relatedID = t.Split('='); 

      list = ns.GetBasicNoteResults(nm, urlArray[5], relatedID[1]); 
     } 
     GenerateGrid(list); 

     btnNotes.Text = "Notes: " + list.Count.ToString(); 

    } 

我的綁定方法

private void GenerateGrid(List<NotesModel> list) 
    { 

     grdNotes.DataSource = list; 
     grdNotes.DataBind(); 

     int count = grdNotes.Rows.Count; 

     //// Hide headers we don't want to expose 
     //grdNotes.HeaderRow.Cells[0].Visible = false; 
     //grdNotes.HeaderRow.Cells[3].Visible = false; 
     //grdNotes.HeaderRow.Cells[4].Visible = false; 
     //grdNotes.HeaderRow.Cells[5].Visible = false; 

     //for (int i = 0; i < count; i++) 
     //{ 
     // // Loop through rows and hide cells 
     // grdNotes.Rows[i].Cells[0].Visible = false; 
     // grdNotes.Rows[i].Cells[3].Visible = false; 
     // grdNotes.Rows[i].Cells[4].Visible = false; 
     // grdNotes.Rows[i].Cells[5].Visible = false; 
     //} 

     // Finally add edit/delete buttons for these click event handlers 

    } 

我注意到最後一件事是,當我將鼠標懸停在編輯行LinkBut​​ton的,沒有查詢字符串,同我在分頁網格的底部和我的標題。點擊任何網格控件的用戶轉到:

http://localhost:8192/website/Company 

,而不是

http://localhost:8192/website/Company/Advertiser/?id=8879 

摘要

我的GridView控件的事件處理程序不火。我錯過了什麼來完成這項工作?

+0

只是curiouse,做我的回答幫助你出來呢?你有沒有弄清楚這個標記是什麼? – jadarnel27 2013-05-22 17:01:21

回答

1

您需要將這些代碼:

GenerateGrid(list); 

裏面的if(!Page.IsPostBack)塊。

每次你的頁面回發到服務器(例如,當您單擊編輯按鈕),該代碼重建您的GridView回到它的原始狀態。這不允許RowEditing事件發生,因爲你基本上已經銷燬了它並在Init(在它有機會發生之前)重新添加它。


再看看您的代碼,看起來您使用的是IsPostBack來確定網格的內容。您需要修改該邏輯才能使其工作。也許您可以檢查要傳遞的查詢字符串的內容(或查詢字符串中的/字符數),以決定傳遞給GetBasicNoteResults方法的參數。

您的代碼將基本上是這樣的:

if (!IsPostBack) 
{ 
    if (Some logic to decide what parameters to pass) 
    { 
     list = ns.GetBasicNoteResults(nm, urlArray[3], relatedID[1]); 
    } 
    else 
    { 
     list = ns.GetBasicNoteResults(nm, urlArray[5], relatedID[1]); 
    } 
    GenerateGrid(list); 
} 
相關問題