2013-07-19 36 views
0

我無法找到此問題的來源。我調試我的代碼,並結束精細和它做什麼,它的要求(刪除從數據庫中的網頁條目),但它的代碼後,返回此結束:Gridview,未將對象引用設置爲對象的實例

http://i.imgur.com/IO5zY5v.png

爲ListPages.aspx的代碼:

<%@ Page Title="" Language="C#" MasterPageFile="~/admin/Admin.Master" AutoEventWireup="true" CodeBehind="ListPages.aspx.cs" Inherits="TopStart.admin.ListPages" %> 

<asp:Content ContentPlaceHolderID="Main" runat="server"> 
       <div id="container" runat="server" class="full_w"> 
       <div class="h_title">Manage Your Pages</div> 
       <h2>Page Management</h2> 
       <p>This is a list of all your website pages. Use the icons on the last column to perform various operations on a page.</p> 

      <div class="entry"> 
       <div class="sep"></div> 
      </div> 
      <asp:GridView ID="PageList" runat="server" AllowPaging="True" OnRowDataBound="PageList_RowDataBound" OnPageIndexChanging="PageList_PageIndexChanging"> 
       <PagerStyle CssClass="pager" /> 
      </asp:GridView> 

      <div class="entry"> 
       <div class="sep"></div>  
       <a class="button add" href="AddPage.aspx">Add new page</a> <a class="button" href="">Categories</a> 
      </div> 
     </div> 

爲ListPages.apsx.cs的代碼:

using MySql.Data.MySqlClient; 
using System; 
using System.Collections.Generic; 
using System.Data; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.HtmlControls; 
using System.Web.UI.WebControls; 

namespace TopStart.admin 
{ 
    public partial class ListPages : System.Web.UI.Page 
    { 
     protected void Page_Load(object sender, EventArgs e) 
     { 
       CreatePageList(); 
     } 

    protected void CreatePageList() 
    { 
     Database db = new Database("mySqlCon"); 

     string query = "SELECT p.id, p.title, p.slug, u.username, c.categories FROM pages as p LEFT JOIN users AS u ON p.user_id = u.id LEFT JOIN categories AS c ON p.category_id = c.id"; 

     DataTable dt = db.Query(query); 
     dt.Columns.Add(new DataColumn("Modify")); 

     PageList.DataSource = dt; 
     PageList.DataBind(); 
    } 

    protected void PageList_RowDataBound(object sender, GridViewRowEventArgs e) 
    { 
     if (e.Row.RowType == DataControlRowType.Header) 
     { 
      e.Row.Cells[5].Width = new Unit("65px"); 
     } 
     else if (e.Row.RowType == DataControlRowType.DataRow) 
     { 
      HyperLink edit = new HyperLink(); 
      edit.NavigateUrl = "EditPage.aspx?id=" + e.Row.Cells[0].Text; 
      edit.CssClass = "table-icon edit"; 
      edit.Attributes.Add("title", "Edit"); 

      HyperLink archive = new HyperLink(); 
      archive.NavigateUrl = "Archive.aspx?id=" + e.Row.Cells[0].Text; 
      archive.CssClass = "table-icon archive"; 
      archive.Attributes.Add("title", "Archive"); 

      LinkButton delete = new LinkButton(); 
      delete.CssClass = "table-icon delete"; 
      delete.Attributes.Add("title", "Delete"); 
      delete.Attributes.Add("id", e.Row.Cells[0].Text); 
      delete.ID = e.Row.Cells[0].Text; 
      delete.Click += new EventHandler(delete_Click); 

      e.Row.Cells[5].Controls.Add(edit); 
      e.Row.Cells[5].Controls.Add(archive); 
      e.Row.Cells[5].Controls.Add(delete); 
     } 
    } 

    protected void PageList_PageIndexChanging(object sender, GridViewPageEventArgs e) 
    { 
     PageList.PageIndex = e.NewPageIndex; 
     PageList.DataBind(); 
    } 

    protected void delete_Click(object sender, EventArgs e) 
    { 
     LinkButton lb = (LinkButton)sender; 

     Database db = new Database("mySqlCon"); 
     string query = "DELETE FROM pages WHERE id = @id"; 

     MySqlParameter pId = new MySqlParameter("@id", MySqlDbType.Int32, 4); 
     pId.Value = lb.ID; 

     if (db.MQuery(query, pId)) 
     { 
      container.InnerHtml = "<div class=\"h_title\">Success</div>\n<div class=\"n_ok\"><p>Page was deleted. Redirecting...</p></div>"; 
     } 
     else 
     { 
      container.InnerHtml = "<div class=\"h_title\">Failure</div>\n<div class=\"n_error\"><p>Page couldn't be deleted. Redirecting...</p></div>"; 
     } 

     Response.AddHeader("REFRESH", "3;URL=ListPages.aspx"); 
    } 
} 

}

我認爲GridView出了問題。當點擊刪除按鈕時,我的代碼不會直接進入事件方法,而是再次進入PageLoad,再次呈現GridView(假設在代碼中),然後進入方法事件。請幫助我我堅持這一點。

回答

1

CreatePageListif(!IsPostBack)

protected void Page_Load(object sender, EventArgs e) 
{ 
    if(!IsPostBack) 
     CreatePageList(); 
} 

的完整的生命週期是通過在每個回發運行。這很正常。但是,如果啓用ViewState(默認),則不應在每次回發時將網格綁定到來自Page_Load的DataSource。只有在某些東西發生了變化時才這樣做,例如之後,您已從刪除事件中刪除了一行。

否則,所有更改都會丟失,因爲之前的值是從數據庫加載的,事件不會再被觸發。

更新您還在RowDataBound中動態創建控件。這不是一個好地方,請使用RowCreated代替。動態控件必須在每次回發時重新創建。但RowDataBound僅在GridView是數據綁定的情況下才會被調用,並不一定在每次回發中都有。與此相反,RowCreated在每次回傳時調用。

但因爲你正在設置在RowDataBoundNavigateUrl這是從底層DataItemID得出你在RowCreated創建Hyperlink與適當的ID提及。在RowDataBound中使用此ID來查找Hyperlink並將其設置爲NavigateUrl屬性。

重要的一點是,它會一直重新創建,但它的NavigateUrl屬性只設置一次。

+0

在他的代碼中,他將'div'(id = container)的內容替換爲「GridView',這樣網頁視圖也會從頁面生命週期中移除?我想這就是他爲什麼會例外的原因。 – gzaxx

+0

@gzaxx:他應該在'RowCreated'中創建動態控件,並根據來自'RowDataBound'的數據源設置它的屬性。編輯我的答案。 –

+0

我不能在RowCreated中做所有事情嗎?我只是嘗試過,並沒有渲染控件。那麼如何設置適當的ID到RowCreated,然後在RowDataBound中使用它,我在這裏很困惑。是的,當刪除事件方法被觸發時,我將容器HTML更改爲成功消息,然後再次重定向到同一頁面以再次顯示GridView。 –

相關問題