using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Xml;
public partial class _Default : System.Web.UI.Page
{
//Global varaiable declaration
string xmlfile = @"C:\Documents and Settings\Employee.xml";
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
LoadData();
}
}
//Loading the data from xml file
protected void LoadData()
{
DataSet ds = new DataSet();
DataTable table = new DataTable();
//reading xml file and loading listview.
ds.ReadXml(xmlfile);
lvEmployee.DataSource = ds;
//binding dataset to listview.
lvEmployee.DataBind();
}
// onclick of insert button in listview
protected void lvEmployee_ItemInserting(object sender, ListViewInsertEventArgs e)
{
TextBox txtidTextBox = (TextBox)lvEmployee.InsertItem.FindControl("txtID");
TextBox txtnameTextBox = (TextBox)lvEmployee.InsertItem.FindControl("txtName");
// loading xml file
XmlDocument xdoc = new XmlDocument();
xdoc.Load(xmlfile);
//Creating childnode to root node using Xmlelement
XmlElement xelement = xdoc.CreateElement("Employee");
//creating subnode to childnode using XmlElement
XmlElement xID = xdoc.CreateElement("ID");
xID.InnerText = txtidTextBox.Text;
xelement.AppendChild(xID);
//creating subnode to childnode using XmlElement
XmlElement xName = xdoc.CreateElement("Name");
xName.InnerText = txtnameTextBox.Text;
xelement.AppendChild(xName);
//Adding childnode to the root node.(in my case "NewElement is root node")
xdoc.DocumentElement.AppendChild(xelement);
xdoc.Save(xmlfile);
LoadData();
}
// onclick of edit button in listview
static Int16 i = 0;// based on the requirement we can change the declaration of variable i.
protected void lvEmployee_ItemEditing(object sender, ListViewEditEventArgs e)
{
//getting the index value of particular row on click of edit button
lvEmployee.EditIndex = e.NewEditIndex;
i =Convert.ToInt16(lvEmployee.EditIndex);
// to get the values from label which is inside listview, we need FindControl
Label lblid = (Label)lvEmployee.EditItem.FindControl("lblID");
Label lblname = (Label)lvEmployee.EditItem.FindControl("lblName");
// to get the values from textbox which is inside listview, we need FindControl
TextBox txtidTextBox = (TextBox)lvEmployee.InsertItem.FindControl("txtID");
TextBox txtnameTextBox = (TextBox)lvEmployee.InsertItem.FindControl("txtName");
txtidTextBox.Text = lblid.Text;
txtidTextBox.Visible = false;
txtnameTextBox.Text = lblname.Text;
}
protected void lvEmployee_ItemDeleting(object sender, ListViewDeleteEventArgs e)
{
// to get the values from label which is inside listview, we need FindControl
Label lbl = (lvEmployee.Items[e.ItemIndex].FindControl("lblID")) as Label;
Label lblName = (lvEmployee.Items[e.ItemIndex].FindControl("lblName")) as Label;
//loading xml file using Xmldocument.
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(xmlfile);
XmlNodeList newXMLNodes = xmlDoc.SelectNodes("/NewElement/Employee");
// here i am searching based on id and name.So i am concatenating data into one string variable.
string value = lbl.Text + lblName.Text;
// code to remove child node from xml based on selection(matches all the data in xml file)
//when a match is found removes that childnode from root node.
foreach (XmlNode newXMLNode in newXMLNodes)
{
if (newXMLNode.InnerText == value)
{
newXMLNode.ParentNode.RemoveChild(newXMLNode);
}
}
//saving back xml file and reloading listview.
xmlDoc.Save(xmlfile);
xmlDoc = null;
LoadData();
}
protected void lvEmployee_ItemCanceling(object sender, ListViewCancelEventArgs e)
{
// to clear the textbox data.. (reset)
TextBox txtidTextBox = (TextBox)lvEmployee.InsertItem.FindControl("txtID");
TextBox txtnameTextBox = (TextBox)lvEmployee.InsertItem.FindControl("txtName");
txtidTextBox.Text = string.Empty;
txtnameTextBox.Text = string.Empty;
}
protected void btnUpdate_Click(object sender, EventArgs e)
{
// to get the values from label which is inside listview, we need FindControl
Label lblid = (Label)lvEmployee.EditItem.FindControl("lblID");
Label lblname = (Label)lvEmployee.EditItem.FindControl("lblName");
// to get the values from textbox which is inside listview, we need FindControl
TextBox txtidTextBox = (TextBox)lvEmployee.InsertItem.FindControl("txtID");
TextBox txtnameTextBox = (TextBox)lvEmployee.InsertItem.FindControl("txtName");
//loading xml file
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(xmlfile);
// Index value will be obtained at lvEmployee_ItemEditing ,that index value
//is used here for update.
XmlNode xmlnode = xmldoc.DocumentElement.ChildNodes.Item(i);
xmlnode["ID"].InnerText = lblid.Text;
xmlnode["Name"].InnerText = txtnameTextBox.Text;
// save xml file and reload listview.
xmldoc.Save(xmlfile);
LoadData();
}
}
Try.. its working for me...
sivatulasi
0 Points
5 Posts
Insert,update and delete in listview using xmlfile as datasource
Oct 11, 2010 08:54 PM|LINK
Hi,
here is the source code for insert,update and delete in listview using xmlfile as datasource.
Xmlfile:
<?xml version="1.0" encoding="utf-8"?> <NewElement> <Employee> <ID>1</ID> <Name>siva</Name> </Employee> <Employee> <ID>2</ID> <Name>abc</Name> </Employee> <Employee> <ID>3</ID> <Name>xyz</Name> </Employee> <Employee> <ID>4</ID> <Name>efg</Name> </Employee> </NewElement><%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!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="lvEmployee" runat="server" InsertItemPosition="LastItem" OnItemInserting="lvEmployee_ItemInserting" OnItemDeleting="lvEmployee_ItemDeleting" OnItemEditing="lvEmployee_ItemEditing" OnItemCanceling="lvEmployee_ItemCanceling" > <LayoutTemplate> <table id="Table1" runat="server"> <tr id="Tr1" runat="server"> <td id="Td1" runat="server"> <table id="itemPlaceholderContainer" runat="server" border="0" style=""> <tr id="Tr2" runat="server" style=""> <th id="Th1" runat="server"> </th> <th id="Th2" runat="server"> ID </th> <th id="Th3" runat="server"> Name </th> </tr> <tr id="itemPlaceholder" runat="server"> </tr> </table> </td> </tr> <tr id="Tr3" runat="server"> <td id="Td2" runat="server" style=""> </td> </tr> </table> </LayoutTemplate> <ItemTemplate> <tr style=""> <td> <asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="Delete" /> <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" /> </td> <td> <asp:Label ID="lblID" runat="server" Text='<%# Bind("ID") %>' /> </td> <td> <asp:Label ID="lblName" runat="server" Text='<%# Bind("Name") %>' /> </td> </tr> </ItemTemplate> <InsertItemTemplate> <tr style=""> <td> <asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" /> <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Clear" /> </td> <td> </td> <td> <asp:TextBox ID="txtID" runat="server" Text='<%# Bind("ID") %>' /> </td> <td> <asp:TextBox ID="txtName" runat="server" Text='<%# Bind("Name") %>' /> </td> <td> <asp:Button ID="btnUpdate" runat="server" Text="Update" OnClick="btnUpdate_Click" /> </td> </tr> </InsertItemTemplate> </asp:ListView> </div> </form> </body> </html>.aspx.cs
using System; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; using System.Xml; public partial class _Default : System.Web.UI.Page { //Global varaiable declaration string xmlfile = @"C:\Documents and Settings\Employee.xml"; protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { LoadData(); } } //Loading the data from xml file protected void LoadData() { DataSet ds = new DataSet(); DataTable table = new DataTable(); //reading xml file and loading listview. ds.ReadXml(xmlfile); lvEmployee.DataSource = ds; //binding dataset to listview. lvEmployee.DataBind(); } // onclick of insert button in listview protected void lvEmployee_ItemInserting(object sender, ListViewInsertEventArgs e) { TextBox txtidTextBox = (TextBox)lvEmployee.InsertItem.FindControl("txtID"); TextBox txtnameTextBox = (TextBox)lvEmployee.InsertItem.FindControl("txtName"); // loading xml file XmlDocument xdoc = new XmlDocument(); xdoc.Load(xmlfile); //Creating childnode to root node using Xmlelement XmlElement xelement = xdoc.CreateElement("Employee"); //creating subnode to childnode using XmlElement XmlElement xID = xdoc.CreateElement("ID"); xID.InnerText = txtidTextBox.Text; xelement.AppendChild(xID); //creating subnode to childnode using XmlElement XmlElement xName = xdoc.CreateElement("Name"); xName.InnerText = txtnameTextBox.Text; xelement.AppendChild(xName); //Adding childnode to the root node.(in my case "NewElement is root node") xdoc.DocumentElement.AppendChild(xelement); xdoc.Save(xmlfile); LoadData(); } // onclick of edit button in listview static Int16 i = 0;// based on the requirement we can change the declaration of variable i. protected void lvEmployee_ItemEditing(object sender, ListViewEditEventArgs e) { //getting the index value of particular row on click of edit button lvEmployee.EditIndex = e.NewEditIndex; i =Convert.ToInt16(lvEmployee.EditIndex); // to get the values from label which is inside listview, we need FindControl Label lblid = (Label)lvEmployee.EditItem.FindControl("lblID"); Label lblname = (Label)lvEmployee.EditItem.FindControl("lblName"); // to get the values from textbox which is inside listview, we need FindControl TextBox txtidTextBox = (TextBox)lvEmployee.InsertItem.FindControl("txtID"); TextBox txtnameTextBox = (TextBox)lvEmployee.InsertItem.FindControl("txtName"); txtidTextBox.Text = lblid.Text; txtidTextBox.Visible = false; txtnameTextBox.Text = lblname.Text; } protected void lvEmployee_ItemDeleting(object sender, ListViewDeleteEventArgs e) { // to get the values from label which is inside listview, we need FindControl Label lbl = (lvEmployee.Items[e.ItemIndex].FindControl("lblID")) as Label; Label lblName = (lvEmployee.Items[e.ItemIndex].FindControl("lblName")) as Label; //loading xml file using Xmldocument. XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(xmlfile); XmlNodeList newXMLNodes = xmlDoc.SelectNodes("/NewElement/Employee"); // here i am searching based on id and name.So i am concatenating data into one string variable. string value = lbl.Text + lblName.Text; // code to remove child node from xml based on selection(matches all the data in xml file) //when a match is found removes that childnode from root node. foreach (XmlNode newXMLNode in newXMLNodes) { if (newXMLNode.InnerText == value) { newXMLNode.ParentNode.RemoveChild(newXMLNode); } } //saving back xml file and reloading listview. xmlDoc.Save(xmlfile); xmlDoc = null; LoadData(); } protected void lvEmployee_ItemCanceling(object sender, ListViewCancelEventArgs e) { // to clear the textbox data.. (reset) TextBox txtidTextBox = (TextBox)lvEmployee.InsertItem.FindControl("txtID"); TextBox txtnameTextBox = (TextBox)lvEmployee.InsertItem.FindControl("txtName"); txtidTextBox.Text = string.Empty; txtnameTextBox.Text = string.Empty; } protected void btnUpdate_Click(object sender, EventArgs e) { // to get the values from label which is inside listview, we need FindControl Label lblid = (Label)lvEmployee.EditItem.FindControl("lblID"); Label lblname = (Label)lvEmployee.EditItem.FindControl("lblName"); // to get the values from textbox which is inside listview, we need FindControl TextBox txtidTextBox = (TextBox)lvEmployee.InsertItem.FindControl("txtID"); TextBox txtnameTextBox = (TextBox)lvEmployee.InsertItem.FindControl("txtName"); //loading xml file XmlDocument xmldoc = new XmlDocument(); xmldoc.Load(xmlfile); // Index value will be obtained at lvEmployee_ItemEditing ,that index value //is used here for update. XmlNode xmlnode = xmldoc.DocumentElement.ChildNodes.Item(i); xmlnode["ID"].InnerText = lblid.Text; xmlnode["Name"].InnerText = txtnameTextBox.Text; // save xml file and reload listview. xmldoc.Save(xmlfile); LoadData(); } } Try.. its working for me...Umesh Daiya
Member
2 Points
1 Post
Re: Insert,update and delete in listview using xmlfile as datasource
Nov 23, 2011 04:33 AM|LINK
Thnaks its good.
but i would like to knwo why we use @
like example
string xmlfile = @HttpContext.Current.Request.MapPath("employee.xml");
while i use this its will display error and above string work properly.
string xmlfile = HttpContext.Current.Request.MapPath("employee.xml");
please tell me why we use @
thanks
regards
umesh daiya
guruprasad.b...
Member
8 Points
5 Posts
Re: Insert,update and delete in listview using xmlfile as datasource
Nov 15, 2012 02:46 PM|LINK
i tried above code bt getting an error(s) is .aspx.cs as
Error : The name 'lvEmployee' does not exist in the current context
please provide solution for this...
Anil Srivast...
Member
442 Points
292 Posts
Re: Insert,update and delete in listview using xmlfile as datasource
Nov 15, 2012 03:08 PM|LINK
@ is used to remove additional tag
only to give physical path
guruprasad.b...
Member
8 Points
5 Posts
Re: Insert,update and delete in listview using xmlfile as datasource
Nov 15, 2012 03:50 PM|LINK
@ is used to remove additional tags.....used to provide only physical path