Using Xml file With the Gridview and Textboxes

A4u_6178
Posted by in ASP.NET category on for Beginner level | Points: 250 | Views : 27191 red flag
Rating: 5 out of 5  
 1 vote(s)

The output demonstrates the use of xml file in binding to gridview. This is one of sample webapplication which i created while learning the xml, Xpath and gridview.

happy coding...!

Introduction


In this article, we will know how an xml file can be used as data source. I am going to show how to enter data into xml file using textboxes and display the content of xml file into gridview.


Lets get started with Implementation and coding

Design view:


The textboxes and labels are placed into table to make proper alignment on page so that the page look good, below is the code for entering data into the xml file as u can see in the below code the i have used proper validation on each textboxes to avoid empty values into xml file.


<table>

<tr>

<td>

<asp:Label ID="lblName" runat="server" Text="First Name:"></asp:Label>

</td>

<td>

<asp:TextBox ID="txtName" runat="server" ViewStateMode="Disabled"></asp:TextBox>

<asp:RequiredFieldValidator ID="rfvName" runat="server" ControlToValidate="txtName"

Display="Dynamic" ErrorMessage="! Please Enter Your Name Here" ForeColor="#FF3300"

SetFocusOnError="True"></asp:RequiredFieldValidator>

</td>

</tr>

<tr>

<td>

<asp:Label ID="lblEmail" runat="server" Text="Email ID:"></asp:Label>

</td>

<td>

<asp:TextBox ID="txtEmailID" runat="server" ViewStateMode="Disabled"></asp:TextBox>

<asp:RequiredFieldValidator ID="rfvEmailID" runat="server" ControlToValidate="txtEmailID"

Display="Dynamic" ErrorMessage="! Enter a Valid Email ID" ForeColor="Red" SetFocusOnError="True"></asp:RequiredFieldValidator>

<asp:RegularExpressionValidator ID="revEmailID" runat="server" ControlToValidate="txtEmailID"

Display="Dynamic" ErrorMessage="! Enter a Valid Email ID(eg: eg@domain.com)"

ForeColor="Red" SetFocusOnError="True" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>

</td>

</tr>

<tr>

<td>

<asp:Label ID="lblCntNum" runat="server" Text="Contact Number:"></asp:Label>

</td>

<td>

<asp:TextBox ID="txtCntNum" runat="server" ViewStateMode="Disabled"></asp:TextBox>

<asp:RequiredFieldValidator ID="rfvCntNum" runat="server" ControlToValidate="txtCntNum"

Display="Dynamic" ErrorMessage="! Please Enter Contact Number Here" ForeColor="Red"

SetFocusOnError="True"></asp:RequiredFieldValidator>

<asp:RegularExpressionValidator ID="revEmailID0" runat="server" ControlToValidate="txtCntNum"

Display="Dynamic" ErrorMessage="! Enter 5 Digit Contact Number." ForeColor="Red"

SetFocusOnError="True" ValidationExpression="\d{5}"></asp:RegularExpressionValidator>

</td>

</tr>

<tr>

<td>

<asp:Label ID="lblMessage" runat="server" Text="Leave Message:"></asp:Label>

</td>

<td>

<asp:TextBox ID="txtMessage" runat="server" TextMode="MultiLine" Height="100px" ForeColor="Red"

ViewStateMode="Disabled"></asp:TextBox>

<asp:RequiredFieldValidator ID="rfvMessage" runat="server" ControlToValidate="txtMessage"

Display="Dynamic" ErrorMessage="! Please Enter Your Message Here." ForeColor="Red"

SetFocusOnError="True"></asp:RequiredFieldValidator>

</td>

</tr>

<tr>

<td>

<asp:Button ID="btnSend" runat="server" Text="Save to xmlfile" OnClick="btnSend_Click" />

</td>

<td>

&nbsp;

</td>

</tr>

</table>

Code behind file

public void Bindgrid()

{

DataSet ds1 = new DataSet();

ds1.ReadXml(Request.PhysicalApplicationPath + "XMLFile2.xml");

GridView1.DataSource = ds1;

GridView1.DataBind();

}

On click of "Save to xmlfile" button a method called btnSend_Click fires. I am using ReadXml method to read the xmlfile into dataset object called ds1, now dr an object of DataRow class used to enter the textbox values into default table on btnSend_Click method fires. When the row is being inserted into the default table i am using the dataset object with WriteXml method  to save data into xmlfile and calling the Bindgrid() method.


protected void btnSend_Click(object sender, EventArgs e)

{

DataSet ds1 = new DataSet();

ds1.ReadXml(Request.PhysicalApplicationPath + "XMLFile2.xml");

DataRow dr = ds1.Tables[0].NewRow();

dr["FirstName"] = txtName.Text;

dr["EmailID"] = txtEmailID.Text;

dr["ContactNumber"] = txtCntNum.Text;

dr["Description"] = txtMessage.Text;

ds1.Tables[0].Rows.Add(dr);

ds1.WriteXml(Request.PhysicalApplicationPath + "XMLFile2.xml");

Bindgrid();

}

GridView and xmldatasource code in aspx page is as follows:


<asp:GridView ID="GridView1" AllowPaging="true" PageSize="3" runat="server" OnPageIndexChanging="page_IndexChange"

OnRowDeleting="row_Delete">

<HeaderStyle Font-Bold="true" BorderColor="Azure" BackColor="Black" ForeColor="White"

VerticalAlign="Middle" Font-Italic="true" Font-Underline="true" />

<FooterStyle BackColor="LightBlue" BorderColor="LightCoral" BorderWidth="1px" ForeColor="White" />

<PagerStyle BackColor="ActiveCaption" Font-Size="Large" HorizontalAlign="Right" />

<Columns>

<asp:CommandField ShowDeleteButton="true" HeaderText="Delete" DeleteImageUrl="~/images/imagesCAMEJRZQ.jpg"

DeleteText="Click Here To Delete" ButtonType="Image" />

</Columns>

</asp:GridView>

<asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="~/XMLFile2.xml" XPath="/gridview/message">

</asp:XmlDataSource>

The code shows a gridview and xmldatasource as u can see in xmldatasource an attribute Xpath="/gridview/mesage" here i am pointing from where should the content of xml be displayed into my gridview. Have a look at xml file below.


<?xml version="1.0" standalone="yes"?>

<gridview>

<message>

<FirstName>Iqbal</FirstName>

<EmailID>iqbal@air.com</EmailID>

<ContactNumber>998664354</ContactNumber>

<Description>kay karra???</Description>

</message>

<message>

<FirstName>nafkhansab</FirstName>

<EmailID>nffu@gmail.com</EmailID>

<ContactNumber>12345</ContactNumber>

<Description>hi,Wat's up buddy..</Description>

</message>

Here is the code for the delete opertion of records from gridview and xmlfile on click of the link in delete column.Again same thing reading xmlfile using dataset object and delete the selected row in default table of gridview then write the ds1 object content into the xml file and call Bindgrid method to populate the gridview.


protected void row_Delete(object sender, GridViewDeleteEventArgs e)

{

DataSet ds1 = new DataSet();

ds1.ReadXml(Request.PhysicalApplicationPath + "XMLFile2.xml");

ds1.Tables[0].Rows[GridView1.Rows[e.RowIndex].DataItemIndex].Delete();

ds1.WriteXml(Request.PhysicalApplicationPath + "XMLFile2.xml");

Bindgrid();

}

For page index changing :

protected void page_IndexChange(object sender, GridViewPageEventArgs e)

{

GridView1.PageIndex = e.NewPageIndex;

Bindgrid();

}

Hope this article was useful. Do let me know your comment or feedback. Thanks!


Page copy protected against web site content infringement by Copyscape

About the Author

A4u_6178
Full Name: a4u. t
Member Level: Starter
Member Status: Member
Member Since: 6/27/2011 2:33:56 AM
Country: India
Thanks & Regards,
Http://www.dotnetfunda.com

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)