2012-08-10 48 views
0

我现在正在使用xml工作可更新消息,但每次刷新第一页即插入/更新页时,即使我清空了文本框,它仍然会张贴。插入/可更新的XML

1 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

你按下按钮或使用F5键刷新页面? – 2012-08-10 19:15:32

+0

使用F5刷新页面 – FishBowlGuy 2012-08-10 19:16:06

回答

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 
    } 
} 
+0

我如何防止它? – FishBowlGuy 2012-08-10 19:22:00

+0

等待........... – 2012-08-10 19:24:40

+0

感谢kind sir – FishBowlGuy 2012-08-10 19:32:30