Gridview,对象引用未设置为对象的实例

时间:2013-07-19 11:30:50

标签: c# asp.net gridview webforms linkbutton

我无法找到此问题的来源。我调试了我的代码,结果很好,它完成了它的请求(从数据库中删除了一个页面条目),但它在代码结束后返回:

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 个答案:

答案 0 :(得分:1)

CreatePageList中包裹if(!IsPostBack)

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

每个回发都会贯穿整个生命周期。这很正常。但是,如果启用了ViewState,则不应在每次回发时将网格数据绑定到Page_Load的DataSource(默认)。只有在某些内容发生变化时才这样做,例如 之后您已从删除事件中删除了一行。

否则所有更改都会丢失,因为先前的值是从数据库加载的,也不再触发事件。

更新您还在RowDataBound动态创建控件。这不是一个好地方,请改用RowCreated。必须在每次回发时重新创建动态控件。但是RowDataBound仅在GridView是数据绑定时被调用,而不是每次回发。与此相反,RowCreated 在每次回发时都会调用

但是,由于您要在NavigateUrl中设置RowDataBound,该ID来自基础DataItem的{​​{1}},您必须在Hyperlink中创建RowCreated {1}}与适当的ID一起提到。在ID中使用此RowDataBound查找Hyperlink并设置NavigateUrl属性。

重要的是它将永远重新创建,但它的NavigateUrl属性只设置一次。