Forum Moderators: open
I am working up to being able to interrogate another server for xml data, and then respond. To simulate that I am working between two web sites. The idea is to call up an xml file located at [url]http://www.example.com/WebServices/Tester/test.xml[/url] with a button click to display the content,each column headed by the appropriate node's name, then be able to insert, delete, edit and update.
At the remote site, I have created access via a web service with a file called transxml.amx.
That has the code:
<%@ WebService Language="VB" CodeBehind="~/App_Code/transxml.vb" Class="transxml" Debug="true" %>
Its code behind is:
Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.Xml
<System.Web.Script.Services.ScriptService()> _
<WebService(Namespace:="http://example.com/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Public Class transxml
Inherits System.Web.Services.WebService <WebMethod()> Public Function GetXmlFile() As XmlNode
Dim doc As New XmlDocument()
doc.Load(Server.MapPath("Tester/test.xml"))
Return doc.DocumentElement
End Function
End Class
I have made a start by creating a file on the calling server - /Testxml/Books.aspx which, if you click the button, brings up the desired grid - except there is no editing facility.
Here's the code of Books.aspx:
<%@ Page Language="VB" Debug="true" AutoEventWireup="false" CodeFile="Books.aspx.vb" Inherits="Books" validateRequest="false" %><%@ Register assembly="System.Web.Entity, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" namespace="System.Web.UI.WebControls" tagprefix="asp" %>
<!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>Editable xml</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
<asp:Button ID="Button1" runat="server" Text="Get books" OnClick="Button1_Click" />
</div>
</form>
</body>
</html>
Here's the code behind:
Imports System.Xml
Imports System.Xml.XmlNode
Imports System.Xml.XmlLinkedNode
Imports System.Xml.XmlElement
Imports System.DataPartial Class Books
Inherits System.Web.UI.Page
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim ws As New BooksInfo.transxml()
Dim element As XmlElement = DirectCast(ws.GetXmlFile(), XmlElement)
Dim ds As New DataSet()
Dim xnr As New XmlNodeReader(element)
ds.ReadXml(xnr)
GridView1.DataSource = ds
GridView1.DataBind()
End Sub
Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.SelectedIndexChanged
End Sub
End Class
But I have got severely stuck trying to do the editing bit. With no data bound directly to the grid in Books.aspx, I
did not get the usual options for editing when I clicked the grid's smart tag in visual web developer. After much experimentation, I got something that, in design view, looked like it could deliver:
<%@ Page Language="VB" Debug="true" AutoEventWireup="false" CodeFile="EditBooks.aspx.vb" Inherits="EditBooks" validateRequest="false" %><%@ Register assembly="System.Web.Entity, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" namespace="System.Web.UI.WebControls" tagprefix="asp" %>
<!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 id="Head1" runat="server">
<title>Editable xml</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateDeleteButton="True"
AutoGenerateEditButton="True" DataSourceID="ObjectDataSource1"
CellPadding="4" ForeColor="#333333" GridLines="None" AllowSorting="True"
AutoGenerateColumns="False" >
<RowStyle BackColor="#E3EAEB" />
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="Name" HeaderText="Name" ReadOnly="True"
SortExpression="Name" />
<asp:BoundField DataField="InnerXml" HeaderText="InnerXml"
SortExpression="InnerXml" />
<asp:BoundField DataField="InnerText" HeaderText="InnerText"
SortExpression="InnerText" />
</Columns>
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#7C6F57" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
DataObjectTypeName="System.IAsyncResult" DeleteMethod="EndGetXmlFile"
InsertMethod="EndGetXmlFile" SelectMethod="GetXmlFile"
TypeName="BooksInfo.transxml" UpdateMethod="EndGetXmlFile">
</asp:ObjectDataSource>
<asp:Button ID="Button1" runat="server" Text="Get books" OnClick="Button1_Click" />
</div>
</form>
</body>
</html>
with this code behind;
Imports System.Xml
Imports System.Xml.XmlNode
Imports System.Xml.XmlLinkedNode
Imports System.Xml.XmlElement
Imports System.DataPartial Class EditBooks
Inherits System.Web.UI.Page
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim ws As New BooksInfo.transxml()
Dim element As XmlElement = DirectCast(ws.GetXmlFile(), XmlElement)
GridView1.DataBind()
End Sub
Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.SelectedIndexChanged
End Sub
End Class
But I wound up with a render that I did not want - not the headings I wanted, nor the content displayed as required (though I liked the grid coming up without pushing the button) - and crashes when I try updating.
Can you help me get a grid that's like my original - but editable?
As for the ObjectData Source. I would not use it in this case at all. I would just bind the Dataset on initial page view (aka not a postback). And have all the databinding pointing to the dataset. And when you are ready to Save you will just updated the dataset, and send it back to the original webservice.
For Reference
Dataset [msdn.microsoft.com]
ObjectDataSource Class [msdn.microsoft.com]
There has been some progress:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" AutoGenerateEditButton="True">
<Columns>
<asp:BoundField DataField="author" HeaderText="Author" />
<asp:BoundField DataField="title" HeaderText="Title" />
<asp:BoundField DataField="content" HeaderText="Content" />
</Columns>
</asp:GridView>
and
Protected Sub GridView1_RowCancelingEdit(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles GridView1.RowCancelingEdit
GridView1.EditIndex = -1
End Sub
Protected Sub GridView1_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles GridView1.RowEditing
GridView1.EditIndex = e.NewEditIndex
Dim ws As New BooksInfo.transxml()
Dim element As XmlElement = DirectCast(ws.GetXmlFile(), XmlElement)
Dim ds As New DataSet()
Dim xnr As New XmlNodeReader(element)
ds.ReadXml(xnr)
GridView1.DataSource = ds
GridView1.DataBind()
End Sub
End Class
shows the the data as I want, and allows me to edit the fields - but I have been unable to work out how to update - and I have yet to move on to cancelling and deleting.
If you could point me at a reference using VB and a dataset for a similar task - or show some sample code, I would be very grateful.