我现在使用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>
答案 0 :(得分: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
}
}