我找不到这个问题的根源。我调试了我的代码,它结束得很好,它完成了它的请求(从数据库中删除一个页面条目),但是它在代码结束后返回这个:
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(假设在代码中),然后进入方法事件。请帮帮我,我被这个困住了。