Hi everbody i am trying to save data coming from gridview footer row.I dont get any errors but when i check the xml file just nodes are added not the content coming from texboxes
A better way to deal with this kind of problem is to transfer from an xml file into something like DataTable,and then handle events of GridView do adding,deleting or updating to DataTable,and then use WriteXml to output a whole xml file。
Sample:
【xml】
<?xml version="1.0" standalone="yes"?>
<DocumentElement>
<book id="bk105">
<author>Corets, Eva</author>
<title>The Sundered Grail</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2001-09-10</publish_date>
<description>
The two daughters of Maeve, half-sisters,
battle one another for control of England. Sequel to
Oberon's Legacy.
</description>
</book>
<book id="bk107">
<author>Thurman, Paula</author>
<title>Splish Splash</title>
<genre>Romance</genre>
<price>4.95</price>
<publish_date>2000-11-02</publish_date>
<description>
A deep sea diver finds true love twenty
thousand leagues beneath the sea.
</description>
</book>
<book id="bk108">
<author>Knorr, Stefan</author>
<title>Creepy Crawlies</title>
<genre>Horror</genre>
<price>4.95</price>
<publish_date>2000-12-06</publish_date>
<description>
An anthology of horror stories about roaches,
centipedes, scorpions and other insects.
</description>
</book>
<book id="bk109">
<author>Kress, Peter</author>
<title>Paradox Lost</title>
<genre>Science Fiction</genre>
<price>6.95</price>
<publish_date>2000-11-02</publish_date>
<description>
After an inadvertant trip through a Heisenberg
Uncertainty Device, James Salway discovers the problems
of being quantum.
</description>
</book>
<book id="bk110">
<author>O'Brien, Tim</author>
<title>Microsoft .NET: The Programming Bible</title>
<genre>Computer</genre>
<price>36.95</price>
<publish_date>2000-12-09</publish_date>
<description>
Microsoft's .NET initiative is explored in
detail in this deep programmer's reference.
</description>
</book>
<book id="bk111">
<author>O'Brien, Tim</author>
<title>MSXML3: A Comprehensive Guide</title>
<genre>Computer</genre>
<price>36.95</price>
<publish_date>2000-12-01</publish_date>
<description>
The Microsoft MSXML3 parser is covered in
detail, with attention to XML DOM interfaces, XSLT processing,
SAX and more.
</description>
</book>
<book id="bk112">
<author>Galos, Mike</author>
<title>Visual Studio 7: A Comprehensive Guide</title>
<genre>Computer</genre>
<price>49.95</price>
<publish_date>2001-04-16</publish_date>
<description>
Microsoft Visual Studio 7 is explored in depth,
looking at how Visual Basic, Visual C++, C#, and ASP+ are
integrated into a comprehensive development
environment.
</description>
</book>
</DocumentElement>
【aspx】
/****************************** Module Header ******************************\
* Module Name: Default.aspx.cs
* Project: CSASPNETCRUDXmlInGridView
* Copyright (c) Microsoft Corporation
*
* The project shows up how to insert/delete/update a record into the xml file
* by the GridView.
*
* This source is subject to the Microsoft Public License.
* See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
* All other rights reserved.
\***************************************************************************/
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
namespace XmlEditInsertDeleteInGridView
{
public partial class Default : System.Web.UI.Page
{
/// <summary>
/// For the first time when the page loads, load data into DataTable
/// with DataSet, and save the DataTable into ViewState for further
/// usage.
/// </summary>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataSet ds = new DataSet();
ds.ReadXml(Request.MapPath("try.xml"));
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
ViewState["dt"] = ds.Tables[0];
}
}
/// <summary>
/// Handle the Edit event of GridView for assigning the specific row to be
/// in the edit mode.
/// </summary>
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
GridView1.DataSource = (DataTable)ViewState["dt"];
GridView1.DataBind();
}
/// <summary>
/// Update the specific row in the DataTable with the data from GridView,
/// re-write the data into the xml file and re-databind again.
/// </summary>
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
DataTable dt = (DataTable)ViewState["dt"];
for (int i = 1; i < GridView1.Rows[e.RowIndex].Cells.Count; i++)
{
dt.Rows[e.RowIndex][i-1] = (GridView1.Rows[e.RowIndex].Cells[i].Controls[0] as TextBox).Text;
}
dt.AcceptChanges();
GridView1.EditIndex = -1;
GridView1.DataSource = dt;
GridView1.DataBind();
dt.WriteXml(Request.MapPath("try.xml"));
}
/// <summary>
/// Cancel edit and set the mode of the GridView to normal viewing mode.
/// </summary>
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
DataTable dt = (DataTable)ViewState["dt"];
GridView1.DataSource = dt;
GridView1.DataBind();
}
/// <summary>
/// Insert the data into the DataTable, re-write into the xml file and
/// re-databind to the GridView.
/// </summary>
protected void btnInsert_Click(object sender, EventArgs e)
{
DataTable dt = (DataTable)ViewState["dt"];
dt.Rows.Add(tbAuthor.Text, tbTitle.Text, tbGenre.Text, tbPrice.Text, tbPublishDate.Text, tbDescription.Text, tbId.Text);
dt.AcceptChanges();
dt.WriteXml(Request.MapPath("try.xml"));
GridView1.DataSource = dt;
GridView1.DataBind();
}
/// <summary>
/// Delete the row from DataTable and write data into xml file,
/// re-databind to the GridView.
/// </summary>
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
DataTable dt = (DataTable)ViewState["dt"];
dt.Rows.RemoveAt(e.RowIndex);
dt.WriteXml(Request.MapPath("try.xml"));
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
// Find each row of the GridView
$("tr").each(function () {
// Find each table cell of the row and find the second linkbutton,
// Add the confirm dialog before deleting.
$(this).find("td:eq(0)>a:eq(1)").click(function () {
aleyna_
Member
189 Points
112 Posts
saving as xml
Mar 11, 2012 07:40 PM|LINK
Hi everbody i am trying to save data coming from gridview footer row.I dont get any errors but when i check the xml file just nodes are added not the content coming from texboxes
if (e.CommandName == "SaveFrom")
{
TextBox ln = (TextBox)GridView1.FooterRow.FindControl("Txtfootlast");
TextBox fn = (TextBox)GridView1.FooterRow.FindControl("TxtFotname");
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("Sample.xml"));
XmlElement par = doc.CreateElement("Infos");
XmlElement hfn = doc.CreateElement("NameInfo");
hfn.InnerText = fn.Text;
XmlElement hln = doc.CreateElement("LastNameInfo");
hln.InnerText = ln.Text;
par.AppendChild(hfn);
par.AppendChild(hln);
doc.DocumentElement.AppendChild(par);
doc.Save(Server.MapPath("Sample.xml"));
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
onrowcancelingedit="GridView1_RowCancelingEdit"
onrowediting="GridView1_RowEditing" Width="225px" ShowFooter="True"
onrowcommand="GridView1_RowCommand" AllowPaging="True">
<Columns>
<asp:TemplateField HeaderText="Name">
<EditItemTemplate>
<asp:TextBox ID="txtname" runat="server"></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="TxtFotname" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="lblname" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"CompanyName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="LastName">
<EditItemTemplate>
<asp:TextBox ID="txtlast" runat="server"></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="Txtfootlast" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="lblast" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"ContactName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit">
<EditItemTemplate>
<asp:LinkButton ID="LinkButton3" runat="server" Text="Edit"></asp:LinkButton>
<asp:LinkButton ID="LinkButton22" runat="server" CausesValidation="False"
CommandName="Cancel" Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
<FooterTemplate>
</FooterTemplate>
<EditItemTemplate>
<asp:LinkButton ID="LinkButton33" runat="server" CausesValidation="True" CommandName="Update" Text="Update"></asp:LinkButton>
<asp:LinkButton ID="LinkButton22" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" Text="Edit" CommandName="Edit"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Save AS XML">
<FooterTemplate>
<asp:LinkButton ID="LnkFooterAdd" runat="server" CommandName="SaveFrom">SAVE</asp:LinkButton>
</FooterTemplate>
<ItemTemplate>
<asp:LinkButton ID="LinkButton2" runat="server" Text="Save" CommandName="SaveIt" CommandArgument='<%# ((GridViewRow) Container).RowIndex %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
arunabathan
Member
730 Points
236 Posts
Re: saving as xml
Mar 12, 2012 08:52 AM|LINK
hi,
Please refer below url,
http://www.sergiopereira.com/articles/xmlbuilder.html
http://www.codeproject.com/Articles/7718/Using-XML-in-C-in-the-simplest-way
Thanks&Regards,
Arunabathan.G
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: saving as xml
Mar 13, 2012 01:27 AM|LINK
Hello aleyna:)
A better way to deal with this kind of problem is to transfer from an xml file into something like DataTable,and then handle events of GridView do adding,deleting or updating to DataTable,and then use WriteXml to output a whole xml file。
Sample:
【xml】
<?xml version="1.0" standalone="yes"?>
<DocumentElement>
<book id="bk105">
<author>Corets, Eva</author>
<title>The Sundered Grail</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2001-09-10</publish_date>
<description>
The two daughters of Maeve, half-sisters,
battle one another for control of England. Sequel to
Oberon's Legacy.
</description>
</book>
<book id="bk107">
<author>Thurman, Paula</author>
<title>Splish Splash</title>
<genre>Romance</genre>
<price>4.95</price>
<publish_date>2000-11-02</publish_date>
<description>
A deep sea diver finds true love twenty
thousand leagues beneath the sea.
</description>
</book>
<book id="bk108">
<author>Knorr, Stefan</author>
<title>Creepy Crawlies</title>
<genre>Horror</genre>
<price>4.95</price>
<publish_date>2000-12-06</publish_date>
<description>
An anthology of horror stories about roaches,
centipedes, scorpions and other insects.
</description>
</book>
<book id="bk109">
<author>Kress, Peter</author>
<title>Paradox Lost</title>
<genre>Science Fiction</genre>
<price>6.95</price>
<publish_date>2000-11-02</publish_date>
<description>
After an inadvertant trip through a Heisenberg
Uncertainty Device, James Salway discovers the problems
of being quantum.
</description>
</book>
<book id="bk110">
<author>O'Brien, Tim</author>
<title>Microsoft .NET: The Programming Bible</title>
<genre>Computer</genre>
<price>36.95</price>
<publish_date>2000-12-09</publish_date>
<description>
Microsoft's .NET initiative is explored in
detail in this deep programmer's reference.
</description>
</book>
<book id="bk111">
<author>O'Brien, Tim</author>
<title>MSXML3: A Comprehensive Guide</title>
<genre>Computer</genre>
<price>36.95</price>
<publish_date>2000-12-01</publish_date>
<description>
The Microsoft MSXML3 parser is covered in
detail, with attention to XML DOM interfaces, XSLT processing,
SAX and more.
</description>
</book>
<book id="bk112">
<author>Galos, Mike</author>
<title>Visual Studio 7: A Comprehensive Guide</title>
<genre>Computer</genre>
<price>49.95</price>
<publish_date>2001-04-16</publish_date>
<description>
Microsoft Visual Studio 7 is explored in depth,
looking at how Visual Basic, Visual C++, C#, and ASP+ are
integrated into a comprehensive development
environment.
</description>
</book>
</DocumentElement>
【aspx】
/****************************** Module Header ******************************\
* Module Name: Default.aspx.cs
* Project: CSASPNETCRUDXmlInGridView
* Copyright (c) Microsoft Corporation
*
* The project shows up how to insert/delete/update a record into the xml file
* by the GridView.
*
* This source is subject to the Microsoft Public License.
* See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
* All other rights reserved.
\***************************************************************************/
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
namespace XmlEditInsertDeleteInGridView
{
public partial class Default : System.Web.UI.Page
{
/// <summary>
/// For the first time when the page loads, load data into DataTable
/// with DataSet, and save the DataTable into ViewState for further
/// usage.
/// </summary>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataSet ds = new DataSet();
ds.ReadXml(Request.MapPath("try.xml"));
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
ViewState["dt"] = ds.Tables[0];
}
}
/// <summary>
/// Handle the Edit event of GridView for assigning the specific row to be
/// in the edit mode.
/// </summary>
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
GridView1.DataSource = (DataTable)ViewState["dt"];
GridView1.DataBind();
}
/// <summary>
/// Update the specific row in the DataTable with the data from GridView,
/// re-write the data into the xml file and re-databind again.
/// </summary>
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
DataTable dt = (DataTable)ViewState["dt"];
for (int i = 1; i < GridView1.Rows[e.RowIndex].Cells.Count; i++)
{
dt.Rows[e.RowIndex][i-1] = (GridView1.Rows[e.RowIndex].Cells[i].Controls[0] as TextBox).Text;
}
dt.AcceptChanges();
GridView1.EditIndex = -1;
GridView1.DataSource = dt;
GridView1.DataBind();
dt.WriteXml(Request.MapPath("try.xml"));
}
/// <summary>
/// Cancel edit and set the mode of the GridView to normal viewing mode.
/// </summary>
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
DataTable dt = (DataTable)ViewState["dt"];
GridView1.DataSource = dt;
GridView1.DataBind();
}
/// <summary>
/// Insert the data into the DataTable, re-write into the xml file and
/// re-databind to the GridView.
/// </summary>
protected void btnInsert_Click(object sender, EventArgs e)
{
DataTable dt = (DataTable)ViewState["dt"];
dt.Rows.Add(tbAuthor.Text, tbTitle.Text, tbGenre.Text, tbPrice.Text, tbPublishDate.Text, tbDescription.Text, tbId.Text);
dt.AcceptChanges();
dt.WriteXml(Request.MapPath("try.xml"));
GridView1.DataSource = dt;
GridView1.DataBind();
}
/// <summary>
/// Delete the row from DataTable and write data into xml file,
/// re-databind to the GridView.
/// </summary>
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
DataTable dt = (DataTable)ViewState["dt"];
dt.Rows.RemoveAt(e.RowIndex);
dt.WriteXml(Request.MapPath("try.xml"));
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="XmlEditInsertDeleteInGridView.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>Xml-based CRUD Code Sample</title>
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.5.1.min.js"></script>
<script type="text/javascript">
$(function () {
// Find each row of the GridView
$("tr").each(function () {
// Find each table cell of the row and find the second linkbutton,
// Add the confirm dialog before deleting.
$(this).find("td:eq(0)>a:eq(1)").click(function () {
return confirm("Are you sure to delete?");
});
});
})
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2>
Xml-based CRUD Code Sample——</h2>
<asp:GridView ID="GridView1" runat="server" Width="70%" AutoGenerateDeleteButton="True"
AutoGenerateEditButton="True" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None"
BorderWidth="1px" CellPadding="3" CellSpacing="2" OnRowCancelingEdit="GridView1_RowCancelingEdit"
OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" OnRowDeleting="GridView1_RowDeleting">
<FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
<HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
<PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
<RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#FFF1D4" />
<SortedAscendingHeaderStyle BackColor="#B95C30" />
<SortedDescendingCellStyle BackColor="#F1E5CE" />
<SortedDescendingHeaderStyle BackColor="#93451F" />
</asp:GridView>
</div>
<p>
Id:<asp:TextBox ID="tbId" runat="server"></asp:TextBox>
</p>
<p>
author:<asp:TextBox ID="tbAuthor" runat="server"></asp:TextBox>
</p>
<p>
title:<asp:TextBox ID="tbTitle" runat="server"></asp:TextBox>
</p>
<p>
genre:<asp:TextBox ID="tbGenre" runat="server"></asp:TextBox>
</p>
<p>
price:<asp:TextBox ID="tbPrice" runat="server"></asp:TextBox>
</p>
<p>
publishdate:<asp:TextBox ID="tbPublishDate" runat="server"></asp:TextBox>
</p>
<p>
description:<asp:TextBox ID="tbDescription" runat="server"></asp:TextBox>
</p>
<asp:Button ID="btnInsert" runat="server" Height="26px" OnClick="btnInsert_Click"
Text="Insert" Width="141px" />
</form>
</body>
</html>
PS:If possible,leave your email and I'll send you a copy of that as a sample:)
aleyna_
Member
189 Points
112 Posts
Re: saving as xml
Mar 16, 2012 04:34 PM|LINK
thank you friend