0

我现在使用 xml 处理可更新的新闻,但是每当我刷新作为插入/更新页面的第一页时,即使我清空了文本框,它仍在发布。

第一个 HTML/ASPX 文件的代码:

(

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;

public partial class Main : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        XmlDocument xmlfile = new XmlDocument();
        xmlfile.Load(Server.MapPath("~/TestXML.xml"));
        //create element
        XmlElement theNewsTag = xmlfile.CreateElement("news");
        XmlElement theTitleTag = xmlfile.CreateElement("title");
        XmlElement theContentsTag = xmlfile.CreateElement("contents");
        //create text node
        XmlText theTitleText = xmlfile.CreateTextNode(xmlTitle.Text);
        XmlText theContentsText = xmlfile.CreateTextNode(xmlContent.Text);
        //append
        theTitleTag.AppendChild(theTitleText);
        theContentsTag.AppendChild(theContentsText);

        theNewsTag.AppendChild(theTitleTag);
        theNewsTag.AppendChild(theContentsTag);
        //save
        xmlfile.DocumentElement.AppendChild(theNewsTag);
        xmlfile.Save(Server.MapPath("~/TestXML.xml"));

        xmlTitle.Text = "";
        xmlContent.Text = "";

        Response.Redirect(Server.MapPath("~/Main.aspx"));
    }
}

我的第一个 HTML/ASPX 文件(用于插入/更新)

<h2>Title</h2>
    <asp:TextBox ID="xmlTitle" runat="server"/>
<h2>Content</h2>
    <asp:TextBox ID="xmlContent" runat="server"/>
<h2>Insert XML</h2>
    <asp:Button ID="Button1" runat="server" Text="Post News" 
onclick="Button1_Click" />

我的第二个 HTML/ASPX 文件(用于查看 XML 文件)

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="xmlpath.aspx.cs" Inherits="xmlpath" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ListView ID="ListView1" runat="server" DataSourceID="newDS">
            <LayoutTemplate>
                <div id="ItemPlaceHolderContainer"></div>
                <span id="ItemPlaceHolder" runat="server"></span>
            </LayoutTemplate>

            <ItemTemplate>
                <h2><%#XPath("title") %></h2>
                <p><%#XPath("contents") %></p>
            </ItemTemplate>
        </asp:ListView>
        <asp:XmlDataSource ID="newDS" runat="server" DataFile="~/TestXML.xml">
        </asp:XmlDataSource>
    </div>
    </form>
</body>
</html>
4

1 回答 1

1

当您刷新页面时,它会重新发布数据,它还会向您显示一个对话框, Confirm Form Resubmission因此实际上您正在再次按下按钮。

在您的页面加载中创建这个

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        Session["Key"] = "1";
    }

}

并在单击按钮之前检查会话

protected void button_Click(object sender, EventArgs e)
 {
    if (Session["Key"].ToString() == "1")
    {
           XmlDocument xmlfile = new XmlDocument();
    xmlfile.Load(Server.MapPath("~/TestXML.xml"));
    //create element
    XmlElement theNewsTag = xmlfile.CreateElement("news");
    XmlElement theTitleTag = xmlfile.CreateElement("title");
    XmlElement theContentsTag = xmlfile.CreateElement("contents");
    //create text node
    XmlText theTitleText = xmlfile.CreateTextNode(xmlTitle.Text);
    XmlText theContentsText = xmlfile.CreateTextNode(xmlContent.Text);
    //append
    theTitleTag.AppendChild(theTitleText);
    theContentsTag.AppendChild(theContentsText);

    theNewsTag.AppendChild(theTitleTag);
    theNewsTag.AppendChild(theContentsTag);
    //save
    xmlfile.DocumentElement.AppendChild(theNewsTag);
    xmlfile.Save(Server.MapPath("~/TestXML.xml"));

    xmlTitle.Text = "";
    xmlContent.Text = "";

    Response.Redirect(Server.MapPath("~/Main.aspx"));
    Session["Key"] = "0"; // set key = 0 
    }
 }
于 2012-08-10T19:21:17.007 回答