Archive for the ‘C# / VB/ ASP .net’ Category

Call a Web Service with ASP.Net Ajax without writing client-side scripts

Wednesday, May 9th, 2007

In my previous post I wrote about how we can simply call a Web Service with ASP.Net Ajax. In this post I will do the same but with ASP.Net Ajax script definition. With ASP.Net Ajax script definition markup we can use markup to hook up to controls events, invoke methods etc without writing any client-side script code.
Note: This post will use the same Web Service from the previous post.

When we want to use ASP.Net Ajax script we add the script to our code with a <script> element. The ASP.Net Ajax script is a markup language that uses simple XML. The root element for writing ASP.Net Ajax script is the <page>. Into the <page> element we add our components:

<script type="text/xml-script">
<page xmlns:script="http://schemas.microsoft.com/xml-script/2005">
<components>
<textBox id="myTextBox" />
</components>
</page>
</script>
The code above declare TextBox with the id “myTextBox”. The id is the value of the id for a <input type=”text”> element that exists on our page. The code above do nothing at the moment, more than only initialize a TextBox object. The Web Service that we are going to call in this post has a method that takes on argument of type string:
[WebMethod]
public string MyMethod(string value)
{
return value;
}

Instead of writing client-side script to pass a value from a textbox into the Web Service method, we can use Atlas script to bind a control to the method’s argument. So when the Web Service method will be executed, the value of a textbox will be automatically past as an argument to our Web Service method. We can also make sure a control on the page will be updated with the return value of the Web Service method without writing client-side script. The following ASP.Net Ajax script will specify a Web Service method and bind a TextBox’s Text property to the Web Service method’s argument, value, and bind the return value of the Web Service to another binding, in this case to the “resultsBinding” that is the id of a binding for a label control:

<serviceMethod
id="myService"
url="MyWebService.asmx"
methodName="MyMethod">
<bindings>
<binding
dataContext="myTextBox"
dataPath="text"
property="parameters"
propertyKey="value" />
</bindings>
<completed>
<invokeMethod
target="resultsBinding"
method="evaluateIn" />
</completed>
</serviceMethod>

To specify a Web Service method, we use the <serviceMethod> element, we also give the method an id, the URL to the Web Service and the Web Service method we want to call. By using the element <bindings> and <binding>, we can bind a “component” another controls. In the code above we specify that we want to bind the “myTextBox” control’s Text property to the Web Service method’s parameter with the name “value”. So when we call our specified Service method, the value of the myTextBox’s Text property will be passed to the ‘value’ argument of the Web Service method. The <binding> element has different set of attributes to specify the binding. In this case we use “dataContext” that specify the id of the control that is the source, “dataPath” that is the name of the property of the specified “dataContext” that should be called to get a value from the “dataContext”. The “property” attributes species the member of the main component that should have the value from the “dataContext”. In this case the <serviceMethod> object’s Parameters. The last attribute we use is the “propertyKey”, we use this attribute to specify the name of the argument of the Web Service method, where the “dataContext” value should be passed to.

Note: The <serviceMethos>’s Parameters property will be called and add the value from the “dataContext” with the “propertyKey” as the key to help the object to know which argument of the Service method the value should be passed to.

To specify a method that should be executed when the Web Service is done, we use the <completed> element. By using the <invokeMethod> element, we specify a method that should be called when the Web Service is completed with the execution of its method. The <invokeMethod> element points to another binding, “resultsBinding”, which should be executed. The “resultsBinding” is added to a label control on the page, whoch task is to display the return value of the Web Service method:

<label id="results">
<bindings>
<binding
id="resultsBinding"
dataContext="myService"
dataPath="result"
property="text"
automatic="false" />
</bindings>
</label>

As you can see in the code above, we bind the Label control “results” to the Service Method. As you can see the “dataContext” for the Label control is the <serviceMethod> we specified earlier, “myService”. The “dataPath” is set to the result of the specified Service method. The result value is mapped to the “test” property of the label control. We also set the “automatic” attribute of the binding to false to make sure it will not automatically set the “text” property. Instead we make sure the binding is executed when the Web Service is completed with its execution. As you probably remember, it was done with the <invokeMethod> element added to the <comepleted> element of the <serviceMethod>.

No when we have bind the “myTextBox” value to the Web Service method’s argument and the result value from the Web service to a label, we also need to specify the our specified Service method should be execute from somewhere. We can do that by adding the <invokeMethod> element to a button control’s “click” event:

<button id="myButton">
<click>
<invokeMethod
target="myService"
method="invoke" />
</click>
</button>

The code above will invoke the ”myService” Service method when the click event of the button is pressed.

Here is the whole example:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!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></title>

<atlas:scriptmanager runat="server" id="scriptManager">
<services>
<atlas:servicereference path="~/MyWebService.asmx" />
</services>
</atlas:scriptmanager>
</head>

<body>
<form id="Form1" runat="server">
<div>
Enter value: <input id="myTextBox" type="text" /><br />
<input id="myButton" type="button" value="Execute" />
</div>
</form>

<label id="results"></label>

<script type="text/xml-script">
<page xmlns:script="http://schemas.microsoft.com/xml-script/2005">
<components>

<textBox id="myTextBox" />

<serviceMethod
id="myService"
url="MyWebService.asmx"
methodName="MyMethod">
<bindings>
<binding
dataContext="myTextBox"
dataPath="text"
property="parameters"
propertyKey="value" />
</bindings>
<completed>
<invokeMethod
target="resultsBinding"
method="evaluateIn" />
</completed>
</serviceMethod>

<button id="myButton">
<click>
<invokeMethod
target="myService"
method="invoke" />
</click>
</button>

<label id="results">
<bindings>
<binding
id="resultsBinding"
dataContext="myService"
dataPath="result"
property="text"
automatic="false" />
</bindings>
</label>

</components>
</page>
</script>
</body>
</html>

By using the ASP.Net Ajax script markup we don’t need to learn how to code client-client-side script, such as Javascript, instead we learn how to write ASP.Net Ajax script in a markup language. Some client-side script can be difficult to write, we need for example learn how to call and use the DOM (document object model) and also how to write script that should work for different browsers etc. But by using the ASP.Net Ajax script markup we can now easy bound controls to each other and call methods etc without worry about writing client-side script that should be compatible with other browser etc.

Regards,
Sachin D.

Call a WebService with ASP.Net Ajax

Wednesday, May 2nd, 2007

This will be my first post about ASP.Net Ajax.In this post I’m going to show you a very simple example where I will from a Web Form call a Web Service without reloading the page.
When we will use ASP.Net Ajax on our pages we need to add the <atlas:ScriptManager> element. The ScriptManager will automatically add the references to the required JavaScript files that provide Atlas functionality. So it’s required on every page where we will use the ASP.Net Ajax features.
When we want to add a reference to our WebService we use the Service child element of the ScriptManager:
<atlas:ScriptManager runat="server" ID="scriptManager">
<services>
<atlas:servicereference path="~/MyWebService.asmx" />
</services>
</atlas:ScriptManager>
The code above will add a reference to “MyWebService.asmx”.
Let’s take a look at the MyWebService:

using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class MyWebService : System.Web.Services.WebService
{
public MyWebService()
{
}

[WebMethod]
public string MyMethod(string value)
{
return value;
}
}

As you can see in the code above, it’s a simple Web Service with the method MyMethod. MyMethod will return the value passed as an argument.

When we have added a reference to our WebService we can simply call it from client-side script by using the name of the WebService as the object:

function CallMyWebService()
{
MyWebService.MyMethod(“My Value”, OnRequestComplete);
}

When we call a WebService we need to specify a client-side method that will be called when our Web Service is done with the execution. The OnRequestComplete (you can change the name if you like) is required because the call to the web Service is asynchronous, and we need to specify a method that should be called on the client-side to notify the client when the execution is done.

function OnRequestComplete(result)
{
alert(result);
}

More than this is not needed to call a WebService with ASP.Net Ajax. Here is the code of the WebForm that will call our WebService:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!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>Untitled Page</title>

<script language="javascript" type="text/javascript">
function CallMyWebService()
{
MyWebService.MyMethod("Value by Sachin", OnRequestComplete);
}

function OnRequestComplete(result)
{
alert(result);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<atlas:ScriptManager ID="scriptManger1" runat="server">
<Services>
<atlas:ServiceReference Path="~/MyWebService.asmx" />
</Services>
</atlas:ScriptManager>
<input type="button" onclick="CallMyWebService();" id="myButton" value="Call MyWebService"/>
</div>
</form>
</body>
</html>

Regards,
Sachin D

How To Change Color Of Datagrid Text …Depending On the value In Database….!!!!

Thursday, April 26th, 2007

//Following Ex. shows that if employee is absent then how to change the color of text in perticular cell…..

DataSet ds=new DataSet();
OdbcDataAdapter da=new OdbcDataAdapter("Select * from emp");
OdbcCommandBuilder cmd=new OdbcCommandBuilder(da);
da.fill(ds,"emp");

DataGrid1.DataSource =ds.Tables["emp"];
DataGrid1.DataBind();

//Code To change the for color if datagrid cell contains "A"
private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{

DateTime dat=new DateTime();
dat=System.DateTime.Now;
int cur_day=dat.Day;

for(int i=0;i <=cur_day;i++)
{
string st = e.Item.Cells[i].Text;
if(st == "A")
e.Item.Cells[i].ForeColor= Color.Red;
}
}

By,
Aniruddha

Hello world!

Thursday, April 12th, 2007

Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!