c#中继器文本框的集合
本文关键字:集合 文本 中继器 | 更新日期: 2023-09-27 18:15:40
假设我有以下列表:
First Name Last Name
Bob Wonder
Helen Wood
..., ....,
我可以在asp.net中使用一个中继器并将源绑定到此,但我这样做:
<asp:TextBox ID="txtName" runat="server" EnableVIewState="true" Text='<%# Eval("Name") %>'></asp:TextBox>
<asp:TextBox ID="txtLstNumber" runat="server" EnableVIewState="true" Text='<%# Eval("LastName") %>'></asp:TextBox>
然后我从联系人列表中绑定这些。我遇到的问题是,我需要更新XML中的记录,如下所示:
<Contact name="Bob" lastName="wonder">
我遇到的问题是,我不明白我将如何关联两个文本框,使它们相互关联,然后我可以只是更新XML?例如,如果"Bob"更改了他的姓氏,那么我将更新XML中的"Bob"?
您需要一些字段/列的数据作为主键或ID,如数字1,2,…这些都不会改变。
但是,对于仅仅更新联系人,将使用联系人索引作为标识。
标记:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html>
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:Repeater ID="repeater1" runat="server" OnItemDataBound="repeater1_ItemDataBound">
<HeaderTemplate>
<table>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
Name
</td>
<td>
<asp:TextBox ID="txtName" runat="server" />
</td>
<td>
Last name
</td>
<td>
<asp:TextBox ID="txtLstNumber" runat="server" />
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table></FooterTemplate>
</asp:Repeater>
<asp:Button ID="SaveButton" runat="server" Text="Save"
onclick="SaveButton_Click" />
</form>
</body>
</html>
CS using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) readxml();
}
private void readxml()
{
var xmldoc = new XmlDocument();
xmldoc.Load(Server.MapPath("contacts.xml"));
var xmlnodes = xmldoc.GetElementsByTagName("Contact");
repeater1.DataSource = xmlnodes;
repeater1.DataBind();
xmldoc = null;
}
protected void repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem) return;
var node = (XmlNode)e.Item.DataItem;
var txtName = (TextBox)e.Item.FindControl("txtName");
txtName.Text = node.Attributes["name"].Value;
var txtLstNumber = (TextBox)e.Item.FindControl("txtLstNumber");
txtLstNumber.Text = node.Attributes["lastName"].Value;
}
protected void SaveButton_Click(object sender, EventArgs e)
{
var xmldoc = new XmlDocument();
xmldoc.Load(Server.MapPath("contacts.xml"));
var xmlnodes = xmldoc.GetElementsByTagName("Contact");
for (var i = 0; i < repeater1.Items.Count; i++)
{
var item = repeater1.Items[i];
var txtName = (TextBox)item.FindControl("txtName");
var name = txtName.Text ;
var txtLstNumber = (TextBox)item.FindControl("txtLstNumber");
var lastName = txtLstNumber.Text;
var node = xmlnodes[i];
node.Attributes["name"].Value = name;
node.Attributes["lastName"].Value = lastName;
}
xmldoc.Save(Server.MapPath("contacts.xml"));
xmldoc = null;
}
}
XML文件<?xml version="1.0" encoding="utf-8"?>
<root>
<Contact name="bob" lastName="jan" />
<Contact name="john " lastName="doe" />
</root>