简体   繁体   English

插入新项目后中继器的重新加载

[英]Repeater's reload after inserting new item

I am creating very simple discussion forum in asp.net c#. 我正在ASP.NET C#中创建一个非常简单的讨论论坛。 It is repeater which loads and inserts data from xml file. 它是从xml文件加载和插入数据的转发器。 Loading of comments from xml file works normally fine, but after inserting new comment to the xml file it does not. 从xml文件加载注释通常可以正常工作,但是在将新注释插入xml文件后,则不能正常工作。 After click on insert button it inserts new comment into xml file, but then the repeater cannot load any data from the same xml file. 单击插入按钮后,它将新注释插入xml文件,但是转发器无法从同一xml文件加载任何数据。 I tryed to rebind repeater in click event, but it does not works. 我试图在单击事件中重新绑定转发器,但是它不起作用。

Here is my code for inserting data into XML(using xmldocument class). 这是我的代码,用于将数据插入XML(使用xmldocument类)。

XmlNode author = doc.SelectSingleNode(string.Format("//author[@id={0}]",Request.QueryString["id"]));
XmlNode comment = doc.CreateNode(XmlNodeType.Element, "comment", "");
XmlNode name = doc.CreateNode(XmlNodeType.Element, "name", "");
name.InnerText = nameTxb.Text.Trim();
XmlNode date = doc.CreateNode(XmlNodeType.Element, "date", "");
date.InnerText = string.Format("{0:D}", DateTime.Now);
XmlNode message = doc.CreateNode(XmlNodeType.Element, "message", "");
message.InnerText = messageTxb.Text.Trim();
comment.AppendChild(name);
comment.AppendChild(date);
comment.AppendChild(message);
author.AppendChild(comment);
doc.Save(Server.MapPath("~/App_Data/discussion.xml"));
nameTxb.Text = "";
messageTxb.Text = "";
Repeater1.DataSourceID = "XmlDataSource1";
Repeater1.DataBind();

The XML fragment looks like this: XML片段如下所示:

<comment>
  <name>...</name>
  <date>...</date>
  <message>...</message>
</comment>

You need to set XmlDataSource1.DataFile after saving the xml and may also need to set the XPath like below: 保存xml之后,您需要设置XmlDataSource1.DataFile ,还可能需要设置XPath,如下所示:

doc.Save(Server.MapPath("~/App_Data/discussion.xml"));
nameTxb.Text = "";
messageTxb.Text = "";
XmlDataSource1.DataFile = Server.MapPath("~/App_Data/discussion.xml");
XmlDataSource1.XPath = "root/author[@id='" + Request.QueryString["id"] + "']/comment";//root is the root in discussion.xml

Here's how I have tested your code: 这是我测试您的代码的方式:

I had the following xml file discussion.xml in App_Data : 我有下面的XML文件discussion.xmlApp_Data

<?xml version="1.0" encoding="utf-8"?>
<root>
  <author id="1">
    <comment>
      <name>John Doe</name>
      <date>Wednesday, December 4, 2013</date>
      <message>Test Message</message>
    </comment>
  </author>
</root>

Here's my markup: 这是我的标记:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Repeater ID="Repeater1" runat="server" DataSourceID="XmlDataSource1">
            <ItemTemplate>
              <h3><%# XPath ("name") %></h3>
              <b>Date:</b>
              <%# XPath ("date") %>
          </ItemTemplate>
        </asp:Repeater>
        <asp:XmlDataSource ID="XmlDataSource1" runat="server"
             DataFile="~/App_Data/discussion.xml"
             XPath="root/author[@id='1']/comment">
        </asp:XmlDataSource>
        <asp:TextBox ID="nameTxb" runat="server"></asp:TextBox>
        <asp:TextBox ID="messageTxb" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
    </div>
    </form>
</body>
</html>

And in codebehind I have this method: 在后台代码中,我有以下方法:

protected void Button1_Click(object sender, EventArgs e)
{

    string auid = String.IsNullOrEmpty(Request.QueryString["id"]) ? "1" : Request.QueryString["id"];

    XmlDocument doc = new XmlDocument();
    doc.Load(Server.MapPath("~/App_Data/discussion.xml"));
    XmlNode author = doc.SelectSingleNode(string.Format("//author[@id={0}]", auid));
    XmlNode comment = doc.CreateNode(XmlNodeType.Element, "comment", "");
    comment.InnerText = messageTxb.Text;
    XmlNode name = doc.CreateNode(XmlNodeType.Element, "name", "");
    name.InnerText = nameTxb.Text.Trim();
    XmlNode date = doc.CreateNode(XmlNodeType.Element, "date", "");
    date.InnerText = string.Format("{0:D}", DateTime.Now);
    XmlNode message = doc.CreateNode(XmlNodeType.Element, "message", "");
    message.InnerText = messageTxb.Text.Trim();
    comment.AppendChild(name);
    comment.AppendChild(date);
    comment.AppendChild(message);
    author.AppendChild(comment);
    doc.Save(Server.MapPath("~/App_Data/discussion.xml"));
    nameTxb.Text = "";
    messageTxb.Text = "";
    XmlDataSource1.DataFile = Server.MapPath("~/App_Data/discussion.xml");
    XmlDataSource1.XPath = "root/author[@id='" + auid + "']/comment";
    Repeater1.DataBind();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM