1

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

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(假设在代码中),然后进入方法事件。请帮帮我,我被这个困住了。

4

1 回答 1

2

包裹:CreatePageList_if(!IsPostBack)

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

完整的生命周期在每次回发时运行。这是正常的。Page_Load但是,如果启用了 ViewState(默认),则不应在每次回发时将网格数据绑定到其 DataSource 。仅在某些情况发生变化时才这样做,例如您从删除事件中删除一行之后。

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

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

但是,由于您要设置NavigateUrlin RowDataBoundwhich 派生自ID底层证券,因此DataItem您必须使用适当Hyperlink的. 使用它来查找并设置它的属性。RowCreatedIDIDRowDataBoundHyperlinkNavigateUrl

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

于 2013-07-19T11:33:26.000 回答