----------------XML file XMLFile.xml--------------------------
<?xml version="1.0" encoding="utf-8"?>
<DocumentElement>
<Person>
<id>100</id>
<Name>Sudipta</Name>
<City>Kol</City>
<Age>1212</Age>
</Person>
<Person>
<id>101</id>
<Name>Sudipta123</Name>
<City>kol</City>
<Age>26</Age>
</Person>
<Person>
<id>10</id>
<Name>Kol</Name>
<City>Kola</City>
<Age>10</Age>
</Person>
</DocumentElement>
----------------Design Page : Default.aspx----------------------
<div style="font-family: Verdana; font-size: 12px">
<table style="width: 80%" cellpadding="2" cellspacing="2" align="center">
<tr>
<td>
Id
</td>
<td>
<asp:TextBox ID="txtId" runat="server"></asp:TextBox>
</td>
<td>
Name
</td>
<td>
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
</td>
<td>
City
</td>
<td>
<asp:TextBox ID="txtCity" runat="server"></asp:TextBox>
</td>
<td>
Age
</td>
<td>
<asp:TextBox ID="txtAge" runat="server"></asp:TextBox>
</td>
<td>
<asp:Button ID="btnInsertIntoXml" runat="server" Text="Insert Into Xml"
onclick="btnInsertIntoXml_Click" />
</td>
</tr>
</table>
<table style="width: 80%" cellpadding="2" cellspacing="2" align="center">
<tr>
<td>
Name
</td>
<td>
<asp:TextBox ID="txtUpdateName" runat="server"></asp:TextBox>
</td>
<td>
City
</td>
<td>
<asp:TextBox ID="txtUpdateCity" runat="server"></asp:TextBox>
</td>
<td>
Age
</td>
<td>
<asp:TextBox ID="txtUpdateAge" runat="server"></asp:TextBox>
</td>
<td>
<asp:HiddenField ID="hidId" runat="server" />
<asp:Button ID="btnUpdateXml" runat="server" Text="Update Xml " OnClick="btnUpdateXml_Click" />
</td>
</tr>
<tr>
<td style="width: 100%;" colspan="7">
<asp:Repeater ID="Repeater1" runat="server"
onitemdatabound="Repeater1_ItemDataBound"
onitemcommand="Repeater1_ItemCommand">
<HeaderTemplate>
<table style="width: 99%" cellpadding="4" cellspacing="1">
<tr style="background-color: #E0E1E1">
<td style="width: 50%; font-weight: bold">
Name
</td>
<td style="width: 35%; font-weight: bold">
City
</td>
<td style="width: 9%; font-weight: bold">
Age
</td>
<td style="width: 5%; font-weight: bold">
Edit
</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td style="width: 50%">
<asp:Label ID="lblName" runat="server" Text="Label"></asp:Label>
</td>
<td style="width: 35%">
<asp:Label ID="lblCity" runat="server" Text="Label"></asp:Label>
</td>
<td style="width: 9%">
<asp:Label ID="lblAge" runat="server" Text="Label"></asp:Label>
</td>
<td style="width: 5%">
<asp:LinkButton ID="lnkEdit" runat="server" style="text-decoration:none;">Edit</asp:LinkButton>
</td>
</tr>
</ItemTemplate>
<AlternatingItemTemplate>
<tr style="background-color: #E0E1E1">
<td style="width: 50%">
<asp:Label ID="lblName" runat="server" Text="Label"></asp:Label>
</td>
<td style="width: 35%">
<asp:Label ID="lblCity" runat="server" Text="Label"></asp:Label>
</td>
<td style="width: 9%">
<asp:Label ID="lblAge" runat="server" Text="Label"></asp:Label>
</td>
<td style="width: 5%; " >
<asp:LinkButton ID="lnkEdit" runat="server" style="text-decoration:none;" >Edit</asp:LinkButton>
</td>
</tr>
</AlternatingItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</td>
</tr>
</table>
</div>
-------------Cs Page :Default.aspx.cs
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Xml;
using System.Xml.XPath;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
DataSet ds = new DataSet("XmlData");
DataTable dt = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
PopulateRepeter();
}
}
private void PopulateRepeter()
{
ds.ReadXml(Server.MapPath("XMLFile.xml"));
dt = ds.Tables[0];
Repeater1.DataSource = dt;
Repeater1.DataBind();
}
protected void btnUpdateXml_Click(object sender, EventArgs e)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("XMLFile.xml"));
XmlNodeList nodeList = xmlDoc.SelectNodes("DocumentElement/Person[id='" + hidId.Value.ToString() + "']");
nodeList[0].ChildNodes[1].InnerText = txtUpdateName.Text;
nodeList[0].ChildNodes[2].InnerText = txtUpdateCity.Text;
nodeList[0].ChildNodes[3].InnerText = txtUpdateAge.Text;
xmlDoc.Save(Server.MapPath("XMLFile.xml"));
PopulateRepeter();
}
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Label lblName = (Label)e.Item.FindControl("lblName");
if (lblName != null)
{
lblName.Text = dt.Rows[e.Item.ItemIndex]["Name"].ToString();
}
Label lblCity = (Label)e.Item.FindControl("lblCity");
if (lblCity != null)
{
lblCity.Text = dt.Rows[e.Item.ItemIndex]["City"].ToString();
}
Label lblAge = (Label)e.Item.FindControl("lblAge");
if (lblAge != null)
{
lblAge.Text = dt.Rows[e.Item.ItemIndex]["Age"].ToString();
}
LinkButton lnkEdit = (LinkButton)e.Item.FindControl("lnkEdit");
if (lnkEdit != null)
{
lnkEdit.CommandName = "Edit";
lnkEdit.CommandArgument = dt.Rows[e.Item.ItemIndex]["Id"].ToString() + "," + dt.Rows[e.Item.ItemIndex]["Name"].ToString() + "," + dt.Rows[e.Item.ItemIndex]["City"].ToString() + "," + dt.Rows[e.Item.ItemIndex]["Age"].ToString();
}
}
}
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
string[] field = e.CommandArgument.ToString().Split((",").ToCharArray());
if (e.CommandName == "Edit")
{
hidId.Value = field[0].ToString();
txtUpdateName.Text = field[1].ToString();
txtUpdateCity.Text = field[2].ToString();
txtUpdateAge.Text = field[3].ToString();
}
}
protected void btnInsertIntoXml_Click(object sender, EventArgs e)
{
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("XMLFile.xml"));
XmlNode node = doc.CreateNode(XmlNodeType.Element, "Person", null);
XmlNode Id = doc.CreateElement("id");
Id.InnerText = txtId.Text.Trim();
XmlNode Name = doc.CreateElement("Name");
Name.InnerText = txtName.Text.Trim();
XmlNode City = doc.CreateElement("City");
City.InnerText = txtCity.Text.Trim();
XmlNode Age = doc.CreateElement("Age");
Age.InnerText = txtAge.Text.Trim();
node.AppendChild(Id);
node.AppendChild(Name);
node.AppendChild(City);
node.AppendChild(Age);
doc.DocumentElement.AppendChild(node);
doc.PreserveWhitespace = true;
doc.Save(Server.MapPath("XMLFile.xml"));
PopulateRepeter();
}
}
<?xml version="1.0" encoding="utf-8"?>
<DocumentElement>
<Person>
<id>100</id>
<Name>Sudipta</Name>
<City>Kol</City>
<Age>1212</Age>
</Person>
<Person>
<id>101</id>
<Name>Sudipta123</Name>
<City>kol</City>
<Age>26</Age>
</Person>
<Person>
<id>10</id>
<Name>Kol</Name>
<City>Kola</City>
<Age>10</Age>
</Person>
</DocumentElement>
----------------Design Page : Default.aspx----------------------
<div style="font-family: Verdana; font-size: 12px">
<table style="width: 80%" cellpadding="2" cellspacing="2" align="center">
<tr>
<td>
Id
</td>
<td>
<asp:TextBox ID="txtId" runat="server"></asp:TextBox>
</td>
<td>
Name
</td>
<td>
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
</td>
<td>
City
</td>
<td>
<asp:TextBox ID="txtCity" runat="server"></asp:TextBox>
</td>
<td>
Age
</td>
<td>
<asp:TextBox ID="txtAge" runat="server"></asp:TextBox>
</td>
<td>
<asp:Button ID="btnInsertIntoXml" runat="server" Text="Insert Into Xml"
onclick="btnInsertIntoXml_Click" />
</td>
</tr>
</table>
<table style="width: 80%" cellpadding="2" cellspacing="2" align="center">
<tr>
<td>
Name
</td>
<td>
<asp:TextBox ID="txtUpdateName" runat="server"></asp:TextBox>
</td>
<td>
City
</td>
<td>
<asp:TextBox ID="txtUpdateCity" runat="server"></asp:TextBox>
</td>
<td>
Age
</td>
<td>
<asp:TextBox ID="txtUpdateAge" runat="server"></asp:TextBox>
</td>
<td>
<asp:HiddenField ID="hidId" runat="server" />
<asp:Button ID="btnUpdateXml" runat="server" Text="Update Xml " OnClick="btnUpdateXml_Click" />
</td>
</tr>
<tr>
<td style="width: 100%;" colspan="7">
<asp:Repeater ID="Repeater1" runat="server"
onitemdatabound="Repeater1_ItemDataBound"
onitemcommand="Repeater1_ItemCommand">
<HeaderTemplate>
<table style="width: 99%" cellpadding="4" cellspacing="1">
<tr style="background-color: #E0E1E1">
<td style="width: 50%; font-weight: bold">
Name
</td>
<td style="width: 35%; font-weight: bold">
City
</td>
<td style="width: 9%; font-weight: bold">
Age
</td>
<td style="width: 5%; font-weight: bold">
Edit
</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td style="width: 50%">
<asp:Label ID="lblName" runat="server" Text="Label"></asp:Label>
</td>
<td style="width: 35%">
<asp:Label ID="lblCity" runat="server" Text="Label"></asp:Label>
</td>
<td style="width: 9%">
<asp:Label ID="lblAge" runat="server" Text="Label"></asp:Label>
</td>
<td style="width: 5%">
<asp:LinkButton ID="lnkEdit" runat="server" style="text-decoration:none;">Edit</asp:LinkButton>
</td>
</tr>
</ItemTemplate>
<AlternatingItemTemplate>
<tr style="background-color: #E0E1E1">
<td style="width: 50%">
<asp:Label ID="lblName" runat="server" Text="Label"></asp:Label>
</td>
<td style="width: 35%">
<asp:Label ID="lblCity" runat="server" Text="Label"></asp:Label>
</td>
<td style="width: 9%">
<asp:Label ID="lblAge" runat="server" Text="Label"></asp:Label>
</td>
<td style="width: 5%; " >
<asp:LinkButton ID="lnkEdit" runat="server" style="text-decoration:none;" >Edit</asp:LinkButton>
</td>
</tr>
</AlternatingItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</td>
</tr>
</table>
</div>
-------------Cs Page :Default.aspx.cs
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Xml;
using System.Xml.XPath;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
DataSet ds = new DataSet("XmlData");
DataTable dt = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
PopulateRepeter();
}
}
private void PopulateRepeter()
{
ds.ReadXml(Server.MapPath("XMLFile.xml"));
dt = ds.Tables[0];
Repeater1.DataSource = dt;
Repeater1.DataBind();
}
protected void btnUpdateXml_Click(object sender, EventArgs e)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("XMLFile.xml"));
XmlNodeList nodeList = xmlDoc.SelectNodes("DocumentElement/Person[id='" + hidId.Value.ToString() + "']");
nodeList[0].ChildNodes[1].InnerText = txtUpdateName.Text;
nodeList[0].ChildNodes[2].InnerText = txtUpdateCity.Text;
nodeList[0].ChildNodes[3].InnerText = txtUpdateAge.Text;
xmlDoc.Save(Server.MapPath("XMLFile.xml"));
PopulateRepeter();
}
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Label lblName = (Label)e.Item.FindControl("lblName");
if (lblName != null)
{
lblName.Text = dt.Rows[e.Item.ItemIndex]["Name"].ToString();
}
Label lblCity = (Label)e.Item.FindControl("lblCity");
if (lblCity != null)
{
lblCity.Text = dt.Rows[e.Item.ItemIndex]["City"].ToString();
}
Label lblAge = (Label)e.Item.FindControl("lblAge");
if (lblAge != null)
{
lblAge.Text = dt.Rows[e.Item.ItemIndex]["Age"].ToString();
}
LinkButton lnkEdit = (LinkButton)e.Item.FindControl("lnkEdit");
if (lnkEdit != null)
{
lnkEdit.CommandName = "Edit";
lnkEdit.CommandArgument = dt.Rows[e.Item.ItemIndex]["Id"].ToString() + "," + dt.Rows[e.Item.ItemIndex]["Name"].ToString() + "," + dt.Rows[e.Item.ItemIndex]["City"].ToString() + "," + dt.Rows[e.Item.ItemIndex]["Age"].ToString();
}
}
}
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
string[] field = e.CommandArgument.ToString().Split((",").ToCharArray());
if (e.CommandName == "Edit")
{
hidId.Value = field[0].ToString();
txtUpdateName.Text = field[1].ToString();
txtUpdateCity.Text = field[2].ToString();
txtUpdateAge.Text = field[3].ToString();
}
}
protected void btnInsertIntoXml_Click(object sender, EventArgs e)
{
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("XMLFile.xml"));
XmlNode node = doc.CreateNode(XmlNodeType.Element, "Person", null);
XmlNode Id = doc.CreateElement("id");
Id.InnerText = txtId.Text.Trim();
XmlNode Name = doc.CreateElement("Name");
Name.InnerText = txtName.Text.Trim();
XmlNode City = doc.CreateElement("City");
City.InnerText = txtCity.Text.Trim();
XmlNode Age = doc.CreateElement("Age");
Age.InnerText = txtAge.Text.Trim();
node.AppendChild(Id);
node.AppendChild(Name);
node.AppendChild(City);
node.AppendChild(Age);
doc.DocumentElement.AppendChild(node);
doc.PreserveWhitespace = true;
doc.Save(Server.MapPath("XMLFile.xml"));
PopulateRepeter();
}
}
No comments:
Post a Comment