![]() |
Web Development »
ASP.NET »
General
Advanced
Remote scripting XML. Fill Table HTML with DataTable from WebServices using Javascript. C#By Marcelo Lujan [El Bebe.Net ]Calling a WebService and load a XML dataset from it with Javascript. Manipulate XML and fill a HTML Table. Security included by using a local asmx with the function of proxy, and dont reveal the webservice URI in the javascript |
C#, Javascript, XML, SQL, .NET, WinXP, Win2003, ASP.NET, Visual Studio, DBA, Dev
|
||||||||
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||

I was using webservices for two years, an will use web services, if we want to do a order form, and just ask for the productId, and use just HTML controls, to make your page weightless, but you want to show the products list and dont want to post back all the page just to show it, send the call Using remote scripting and call your webservices from the client, and mantain the page content, remember, you are using HTML controls with no state, the webservice sends you a XML Dataset in the result an you just have to manipulate it with ActiveX and show it with Javascript.
The page no need to do a postback, the operation is processed behind. Blind to the user, and fast as a "line go-XML comes".
We need to create an interface using just HTML controls, because we don't want send the page to the server. Just a text, and a button with the function.
onclick="doData();"
And a section where we want to display the result, like a Div.
id="service" style="BEHAVIOR: url(webservice.htc)"
we need to put an init() in the load of the html, and the function doData(...) it will be called by clicking the button.
function init() {
service.useService("proxy.asmx?WSDL","proxy");
}
function doData(){ iCallID = service.proxy.callService(myResultsData, "ObtenDatos", "x");
}
the iCallID is a var unused later, but the result comes whit the call from it. We need to fill parameters to:
We need to use ActiveX, the XML omes as XMLDocument, the activex can open it and read it, the xml with the dataset is in the RAW property of the result.
var xmlDoc = new ActiveXObject("MSXML2.DomDocument");
xmlDoc.loadXML(result.raw.xml);
var items = xmlDoc.getElementsByTagName('ProductName');
function myResultsData(result){
if(result.error){
var xfaultcode = result.errorDetail.code;
var xfaultstring = result.errorDetail.string;
var xfaultsoap = result.errorDetail.raw;
document.getElementById("areadetextoid").innerHTML= xfaultcode + " " + xfaultstring + " " + xfaultsoap;
}
else{
var xmlDoc = new ActiveXObject("MSXML2.DomDocument");
xmlDoc.loadXML(result.raw.xml);
var items = xmlDoc.getElementsByTagName('ProductName');
window.alert('Total de items: '+items.length);
for (var i=0;i < items.length;i++)
{
window.document.getElementById("areadetextoid").innerHTML += xmlDoc.getElementsByTagName('ProductID')(i).text;
window.document.getElementById("areadetextoid").innerHTML += '<hr>';
window.document.getElementById("areadetextoname").innerHTML += xmlDoc.getElementsByTagName('ProductName')(i).text;
window.document.getElementById("areadetextoname").innerHTML += '<hr>';
}
}
}

The local proxy has the goal of give security to the aplication, we seen in the javascript a URI pointing to localhost, if we dont use the proxy in localhost we need to give the full URI of the webservice, and we give other users the door wide open. in this case, we can apply seciruty by forms level protection in asp.net
The proxy just have an instance of the real web service and a single call of a function.
using System;
using System.Web;
using System.Web.Services;
namespace RemoteScriptingDemo1
{
public class proxy : System.Web.Services.WebService
{
public proxy()
{
InitializeComponent();
}
private IContainer components = null;
private void InitializeComponent()
{
}
protected override void Dispose( bool disposing )
{
if(disposing && components != null)
{
components.Dispose();
}
base.Dispose(disposing);
}
[WebMethod]
public decimal Adicion(decimal x, decimal y)
{
WSDemo.Service1 ws = new RemoteScriptingDemo1.WSDemo.Service1();
return ws.suma(x,y);
}
}
}
| You must Sign In to use this message board. | |||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 21 Feb 2005 Editor: |
Copyright 2005 by Marcelo Lujan [El Bebe.Net ] Everything else Copyright © CodeProject, 1999-2009 Web22 | Advertise on the Code Project |