Code Simplified – Viral Sarvaiya

Code Simplified – Viral Sarvaiya, Web Developer Friends, dot net Developer, Sql Server Developer

Archive for March, 2010

Reading XMl file from Javascript in asp.net C#

Posted by Viral Sarvaiya on March 23, 2010


hi here i demonstrate another application which read the XML file from the javascript,

in my previous post, i demostrate the webservice calling from javascript, this post i continue from that,

web service  return the string which is in XML format and javascript read this XML string and give the alert according to the TAG of the XML.

follow the steps as per the my previous post url : https://viralsarvaiya.wordpress.com/2010/03/23/calling-web-service-from-java-script-in-asp-net-c/

we repeat the Steps,

step 1 : file -> new -> web site

step 2 : select ASP.NET Web Service

step 3 : Add new web form name “default.aspx”

Step 4 : following code shows the “service.cs” file which is the web service file


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Xml ;
using System.IO ;

[WebService(Namespace = "http://Localhost...xys/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService()]
public class MyService : System.Web.Services.WebService
{
public MyService()
{

//Uncomment the following line if using designed components
//InitializeComponent();
}

[WebMethod]
public string HelloWorld(string strNoOfData)
{
// Create the xml document containe
XmlDocument doc = new XmlDocument();// Create the XML Declaration, and append it to XML document
XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", null, null);
doc.AppendChild(dec);// Create the root element
XmlElement root = doc.CreateElement("PHI_Data");
doc.AppendChild(root);

int iNoOfData = 0 ;
if ( int.TryParse( strNoOfData, out iNoOfData ) == false )
iNoOfData = 600 ;

DateTime dtTemp = new DateTime(2009, 1, 1, 12, 0, 0);
Random rndTemp = new Random();

float fValue = ((float)rndTemp.Next(1000, 2000)) / 100;

for (int iCounter = 0; iCounter < iNoOfData; iCounter++)
{
XmlElement Data = doc.CreateElement("PHI_Record");
Data.SetAttribute("Record_Number", iCounter.ToString());

XmlElement dataTimeStamp = doc.CreateElement("TimeStamp");
dataTimeStamp.InnerText = dtTemp.ToString("yyyy-MMM-dd hh:mm:ss");
XmlElement dataValue = doc.CreateElement("Value");
dataValue.InnerText = fValue.ToString();
Data.AppendChild(dataTimeStamp);
Data.AppendChild(dataValue);
root.AppendChild(Data);

fValue = ((float)rndTemp.Next(1000, 2000)) / 100;
dtTemp = dtTemp.AddHours(1);

}
string xmlOutput = doc.OuterXml;

return xmlOutput;
}
}

step 5 : following code shows the “default.aspx” file

</pre>
<head id="Head1" runat="server">
<title>Read XML File From Javascript with Web Serice in asp.net</title>

<script type="text/javascript">

function CallService() {
MyService.HelloWorld(document.getElementById('Textbox1').value, OnComplete, OnError, OnTimeOut);
}

function OnComplete(xmlText) {
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = "false";
xmlDoc.loadXML(xmlText);

var child = xmlDoc.documentElement.firstChild;
while (child) {
alert(child.firstChild.text);
alert(child.lastChild.text);
child = child.nextSibling;
}
}

function OnTimeOut(arg) {
alert("timeOut has occured");
}

function OnError(arg) {
alert("error has occured: " + arg._message);
}
</script>

</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="~/Service.asmx" />
</Services>
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">
<ContentTemplate>
<fieldset>
<asp:TextBox ID="Textbox1" runat="server"></asp:TextBox>
<br />
<asp:Button ID="Button1" runat="server" Text="Call Service" OnClientClick="CallService()" />
</fieldset>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>

Step 6 : Run The Application Enjoy Coding…

Advertisements

Posted in ASP.NET, Javascript | Tagged: , , , , , , | Leave a Comment »

Calling Web Service from Java script in asp.net c#

Posted by Viral Sarvaiya on March 23, 2010


Here i m demonstrate the simple application that calling webservie from javascript

step 1 : file -> new -> web site

step 2 : select ASP.NET Web Service

step 3 : Add new web form name “default.aspx”

Step 4 : following code shows the “service.cs” file


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

[WebService(Namespace = "http://Localhost...xys/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]

<strong>[System.Web.Script.Services.ScriptService()]</strong>

public class Service : System.Web.Services.WebService
{
public Service () {

//Uncomment the following line if using designed components
//InitializeComponent();
}

[WebMethod]
public string HelloWorld(string strNoOfData)
{
return strNoOfData;
}

}

step 5 : following code shows the “default.aspx” file


<%@ 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>
<script type="text/javascript" language="javascript">
function CallService() {
Service.HelloWorld(document.getElementById('Textbox1').value, OnComplete, OnError, OnTimeOut);
}

function OnComplete(Text) {
alert(Text);
}

function OnTimeOut(arg) {
alert("timeOut has occured");
}

function OnError(arg) {
alert("error has occured: " + arg._message);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="~/Service.asmx" />
</Services>
</asp:ScriptManager>

<asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">
<ContentTemplate>
<fieldset>
<asp:TextBox ID="Textbox1" runat="server"></asp:TextBox>
<br />
<asp:Button ID="Button1" runat="server" Text="Call Service" OnClientClick="CallService()" />
</fieldset>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>

Step 6 : Run The Application

Enjoy Coding…

Posted in ASP.NET, Javascript | Tagged: , , , | 4 Comments »

scrollable div

Posted by Viral Sarvaiya on March 11, 2010


to view scroll in div tag just copy and paste below code.


<div style="width:100px; height:100px; overflow:auto;"> PUT YOUR TEXT HERE  </div>

when you html height becomes bigger then 100 px then scroll will come.

enjoy coding…..

Posted in General | Tagged: , , | Leave a Comment »

 
%d bloggers like this: