Showing posts with label function. Show all posts
Showing posts with label function. Show all posts

Monday, March 26, 2012

accessing arg.IsValid from callback function.

I have a custom validator on an aspx page which calls the function below: the args object passes in the value from a dropdownlistbox and has a property IsValid. The problem I'm facing is I don't have access to the results ofOnDoesExchangeRageExistComplete in the main fuction, because I want to set args.IsValid to the result. Any ideas on how I can do this?

This is the manin function called from the aspx page

functionDoesExchangeRateExist(sender, args)
{
//pass in the period date in args.value to the webservice.
UtilityService.DoesExchangeRateExist(args.Value, OnDoesExchangeRageExistComplete); //What I would like to do is set args.IsValid to the result of the webservice call. Because this determines if the page is submitted.
}

//----------------------------------

function

OnDoesExchangeRageExistComplete(result)
{
var errMsg = document.getElementById( "lblErrorMsg");
if(result ==true)
{
errMsg.innerHTML =""
returntrue;
}
else
{

errMsg.innerHTML =

"Exchange rates are not available for selected period date. Please select a different period date."\n

returnfalse;\n

}

\n

}

\n

\n

thanks,

\n

Garfield.

\n\n",0]);D(["ce"]);//-->returnfalse;

}

}

thanks,

Garfield.

I think you can returnargs.IsValidas a return value of the WebMethod?UtilityService.DoesExchangeRateExist?and access it in the callback of the client side through javascript.
Try to take a look at the following for details about calling web service in javascript from the client side.
If the web service class on the server includes a web method that does not return data, you can call the web service without having to handle a response. This is the simplest web method call that can be made from the client. For example, your application has the following web method:
?
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[Microsoft.Web.Script.Services.ScriptService]
public class Parts : System.Web.Services.WebService
{
[WebMethod]
public void NoReturn()
{
//do work here
System.Threading.Thread.Sleep(2000);
}
}
?
The following JavaScript can be used to invoke that web method
?
function RunWebService()
{
Parts.NoReturn();
}
?
If the webservice is in a custom namespace, you would need to fully
qualify the call to the webservice. For example, if the WebService
has a namespace of MyCustomNameSpace.WebServices, the above JavaScript
becomes:
?
function RunWebService()
{
MyCustomNameSpace.WebServices.Parts.NoReturn();
}
?
Getting a return value?
In a scenario where the web method has a return value, then the asynchronous invocation model requires providing a callback function that will be called when the web service call returns. The callback function has an input parameter that contains the results of the web service call.
?
The server-side AJAX stack will appropriately serialize the return value from the web method, and the client-side AJAX stack will deserialize the data to an appropriate JavaScript type to be passed to the callback function.
?
On the Server-side, there is a WebMethod that returns a string:
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[Microsoft.Web.Script.Services.ScriptService]
public class Parts : System.Web.Services.WebService
{
[WebMethod]
public string DynamicDropdown()
{
string tmpDropDown;
tmpDropDown = "<select id=test name=test><option value=Part1>Part1</option><option value=Part2>Part2</option></select>";
return tmpDropDown;
}
}
?
When you call the web service in client script, the call will need to have a callback function specified to handle the return value. The following script demonstrates calling the DynamicDropdown WebMethod of the Parts Web Service and specifies OnComplete as the callback function. The OnComplete function has an input paramter which is the return object from the web service call, in this case, this will be the string value. The OnComplete function then takes the return value and inserts it as the innerHTML property of a span with ID DynamicArea. The result is that a dropdown appears on the page.
function InsertDropdown()
{
Parts.DynamicDropdown(OnComplete);
return false;
}
function OnComplete(result)
{
DynamicArea.innerHTML += result;
}
Passing primitive type parameters to web method
If the web method takes input parameters, then the JavaScript invocation of the method will take corresponding JavaScript parameters. The parameter values will be serialized by the AJAX stack into JSON and packaged in the body of the request and then de-serialized as .Net types corresponding to the signature of the web method.
?
For example, given the following server method that accepts a string parameter and returns a string:
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[Microsoft.Web.Script.Services.ScriptService]
public class Parts : System.Web.Services.WebService
{
[WebMethod]
public string EchoString(String s)
{
return s;
}
}
?
You would use the following JavaScript to call the web service, pass the string parameter and include the callback function:
?
function CallEchoString()
{
Parts.EchoString(form1.GetString.value, OnCallBack);
}
function OnCallBack(results)
{
alert(results);
}
?
The call to the EchoString web method on the Parts.asmx web service is taking the value from a textbox named GetString in a form with ID of "form1" to pass as the input parameter and specifying OnCallBack as the callback function. The OnCallBack function has an input parameter of "results" which is the return type from the EchoString web method. The JavaScript then displays that in a popup box.
Specifying a callback for failure cases?
If the request to the web method is unsuccessful, whether because of error, timeout, or if server code aborts the request, then the callback specified for successful completion will not be called. A second callback function can be specified in the call to the web service for failure cases, receiving an error object as parameter, as in the following javascript:
?
function CallService()
{
PeopleServices.ThrowError(OnCallBackThrowError, OnError);
}
function OnCallBackThrowError(result)
{
alert("OnCallbackThrowError: " + result);
}
function OnError(result)
{
alert("OnError: " + result.get_message());
alert(result.get_stackTrace());
}
?
In the code sample, the ThrowError method of the PeopleServices web service does not take any input parameters. The method call passes the callback function "OnCallBackThrowError" and a method for handling any error condition called "OnError". The OnError method takes an input parameter which is a error object that contains the error message and stack information passed from the server if a .NET Exception was thrown. In this case, the information is displayed in popup windows, but once you have the errors in the browser, you can notify the user in any manner you feel is appropriate.
Using the same callback from multiple callers?
You can leverage the same callback function on the client for multiple web service calls. This allows you to avoid having to write a client function for each callback. In order to differentiate the calls, you pass a user context object that contains information that can be used to tell the requests apart. The user context object can be any JavaScript primitive type, array, or object.
?
For example, let's say you have a web service that contains a web method that you need to call multiple times from the client. You need to know which response corresponds to each call. For example, the first call's return value is placed in Span1 and the second call is in Span2. In order to do this, you can associate context information with the request and you can use the context information to distinguish which request is providing the response. This information is not passed to the server by default. If you want to pass this information to the web service, you would need to pass it as an input parameter to the remote method.
?
On the server, you have the following web service method that is taking in the a string which should be a stock symbol and returning a string:
?
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[Microsoft.Web.Script.Services.ScriptService]
public class Parts : System.Web.Services.WebService
{
[WebMethod]
public string UserContextSample(string stockSymbol)
{
string returnValue = String.Empty;
//do work here to look up stock symbol info
return returnValue;
}
}
?
On the client, you create a JavaScript object to store context specific information and pass that as the last parameter to the web service call. In the sample below, we're creating a dictionary object on the client with a key of contextKey and a value of a stock quote. The syntax for the web service call is:
?
WebService.Method(InputParams, CallBackMethod, ErrorMethod, contextInfo);
?
function CallServicesTest()
{
var userContext1 = {contextKey:"MSFT"};
Parts.UserContextSample("MSFT", OnCallBack, OnError, userContext1);
var userContext2 = {contextKey:"AAPL"};
Parts.UserContextSample("AAPL", OnCallBack, OnError, userContext2);
}
In the callback function, you add a second input parameter that contains the context passed in the web service call. This way you get the results of the web service call in the first input parameter and the context information in the second parameter. In this case, we're checking the context information and inserting text into specific spans on the page along with the response from the web service.
?
function OnCallBack(results, userContext)
{
switch(userContext.contextKey)
{
case "MSFT":
SPAN1.innerHTML = "Microsoft " + results;
break;
case "AAPL":
SPAN2.innerHTML = "Apple " + results;
break;
}
}
Using the same callback for different web methods?
This scenario is similar to using the same callback for multiple calls with the exception that you have two separate web service methods on the client that are using the same callback method. In this scenario, you include a third input parameter on the callback function to receive the web service call that the response is associated to.
?
For example, you have two Web Methods that your function needs to call, but you want to have a single callback method. You can tell which web method is returning by accessing the optional third input parameter to the callback function.
?
On the server you have:
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[Microsoft.Web.Script.Services.ScriptService]
public class Parts : System.Web.Services.WebService
{
[WebMethod]
public string EchoString(String s)
{
return s;
}
[WebMethod]
public string AnotherEchoString(String s)
{
return s;
}
}
?
On the client you have a button that has an onclick method calling GetEcho() function in JavaScript:
?
function GetEcho()
{
Parts.EchoString("This is echo number 1", OnCallBack);
Parts.AnotherEchoString("Another echo string!", OnCallBack);
}
function OnCallBack(results, userContext, sender)
{
alert(results + "\n" + sender);
}
?
When OnCallBack runs, you will get an alert dialog with the string you passed to the web service along with Parts.EchoString or Parts.AnotherEchoString.
Passing and receiving a server type?
As mentioned previously, the AJAX networking stack will generate proxy scripts for any server type that is referenced as an input or output parameter by the web methods contained in linked web services. This allows a developer to access these types on the client in a similar fashion as they would on the server. The types are serialized using JSON serialization which is covered in the next lesson.
?
For example, let's say you have the following web service called PeopleServices. This web service has a method called NewPerson that takes in 3 parameters; 2 Person objects and 1 object of type bool. The return value is also of type Person.
?
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[Microsoft.Web.Script.Services.ScriptService]
public class PeopleServices : System.Web.Services.WebService
{
?
[WebMethod]
public Person NewPerson(Person parent1, Person parent2, bool female)
{
Person _newPerson = new Person();
//Do other work
return _newPerson;
}
}
When you add this web service to the ScriptManager, the client will make a request for the proxy script as discussed in the Proxy Generation section. The result is that the Proxy script will have the web service method as well as the properties for the Person class. The person class is defined as the following on the server:
?
public class Person
{
public string Name
{
get { return _name; }
set { _name = value; }
}
public int Age
{
get { return _age; }
set { _age = value; }
}
public int Height
{
get { return _height; }
set { _height = value; }
}
}
?
On the client, you would need to generate two objects of type Person and pass them as parameters to the web service. In the callback script, the result parameter will be of type Person. Here's a sample JavaScript function that takes the values of textboxes on the page and creates two Person objects, then calls the NewPerson object on the above web service.
?
function GetNewPerson()
{
var person1 = new Person();
var _name = document.getElementById('p1FirstName');
var _age = document.getElementById('p1Age');
var _height = document.getElementById('p1Height');
person1.Name = _name.value;
person1.Age = _age.value;
person1.Height = _height.value;
?
var person2 = new Person();
_name = document.getElementById('p2FirstName');
_age = document.getElementById('p2Age');
_height = document.getElementById('p2Height');
?
person2.Name = _name.value;
person2.Age = _age.value;
person2.Height = _height.value;
PeopleServices.NewPerson(person1, person2, true, OnCallbackGetNewPerson, OnError);
}
?
Here is a sample callback function that handles the return type from the NewPerson method. The result parameter is a Person object and you can directly access the properties. In this case NewPerson is the ID on a Div in the page.
?
function OnCallbackGetNewPerson(result)
{
var newPerson = result;
NewPerson.innerHTML = "New Person <br/>";
NewPerson.innerHTML += "Name: " + newPerson.Name + "<br />";
NewPerson.innerHTML += "Age: " + newPerson.Age + "<br />";
NewPerson.innerHTML += "Height: " + newPerson.Height + "<br />";
}
When you run pages that make these calls and check out the network traffic, you'll see the following:
Web service call is a POST to …/PeopleService.asmx/js/NewPerson. Since the request ends in /js/NewPerson, the AJAX HttpHandler will handle the request to the web service. The Content-Type of the request is application/json.
Request Body contains all the data to create the Person object on the server. The main piece is the __type parameter which tells the server what type to create:
?
{"parent1":{"__type":"Person","Name":"Name1","Age":"12","Height":"60"},"parent2":{"__type":"Person","Name":"Name2","Age":"24","Height":"72"},"female":true}
The response also has a Content-Type of application/json and has a similar body containing the __type parameter and values necessary to create the Person object on the client:
?
{"__type":"Person","Name":"Name1name2ella","Age":23,"Height":71}
?Demo is FireForgetDemo.aspx
?Wish the above can help you.

Saturday, March 24, 2012

about itemView Databind

I'm sorry for my poor english :)

javascript:

function ShowEdit()
{
Sunlinks.WebOA.Web.Services.CommonCodeService.GetOne(onGetComplete);
}
function onGetComplete(result)
{
var templ= $('divcontent').control;
templ.set_data(result);

}

function onSaveComplete(result)
{
alert(result);
}
function savedatas()
{

var templ= $('divcontent').control;
var result=templ.get_data();
Sunlinks.WebOA.Web.Services.CommonCodeService.SaveCommonCode(result,onGetComplete);

}

html:

<input id="Button1" type="button" value="edit" onclick="ShowEdit();" />

<input id="Button2" type="button" onclick="savedatas();" value="button" />

<div id="divcontent" ></div>

<div id="detailsTemplate">
<input id="txtName" type="text" /><br />
<input id="txtFullName" type="text" /><br />
<input id="txtShortName" type="text" /><br />
<input id="txtCode" type="text" /><br />
<input id="txtOrder_num" type="text" /><br />
<input id="chkIsClose" type="checkbox" /><br />
</div>

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


<itemView id="divcontent">
<itemTemplate>
<template layoutElement="detailsTemplate">
<textBox id="txtName">
<bindings>
<binding dataPath="Name" property="text" direction="InOut"/>
</bindings>
</textBox>
<textBox id="txtFullName">
<bindings>
<binding dataPath="FullName" property="text" direction="InOut"/>
</bindings>
</textBox>
</template>
</itemTemplate>
</itemView >
</components>
</page>

now the question:

tmpl.set_data(); work fine

but when txtFullName or txtName lost fouce then atlas throw a error

but when I change direction="In" ,it's no error

hello.

well, it looks like waht you0re trying to accomplish can only be done through a datasource:

http://atlas.asp.net/docs/atlas/doc/data/default.aspx#itemview

Thank USmile [:)]

abortPostBack(); is not working..

Hi, I

I have written a javascript file as

Sys.Application.add_load(ApplicationLoadHandler)

function ApplicationLoadHandler(sender, args)

{

Sys.WebForms.PageRequestManager.getInstance().add_initializeRequest(CheckStatus);

}

function CheckStatus(sender, args)

{

var prm = Sys.WebForms.PageRequestManager.getInstance();if (prm.get_isInAsyncPostBack() & args.get_postBackElement().id =='CancelRefresh')

{

alert('cancelling');

prm.abortPostBack();

alert('cancelled');

}

}

if(typeof(Sys) !=="undefined") Sys.Application.notifyScriptLoaded();

----------------

I have written this to cancel my dropdown list postback But request is not cancelling.

My .aspx code.. is

<formid="form1"runat="server">

<asp:ScriptManagerID="ScriptManager1"runat="server">

<Scripts>

<asp:ScriptReferencePath="Cancel.js"/>

</Scripts>

</asp:ScriptManager>

<div>

<asp:UpdatePanelID="UpdatePanel1"runat="server">

<ContentTemplate>

<asp:DropDownListrunat="server"ID="ddl_testing"OnSelectedIndexChanged="ddl_testing_SelectedIndexChanged"AutoPostBack="True">

<asp:ListItemText="Manoj"></asp:ListItem>

<asp:ListItemText="Atul"></asp:ListItem>

<asp:ListItemText="Punit"></asp:ListItem>

</asp:DropDownList>

<asp:LabelID="Label1"runat="server"Text="Label"></asp:Label><br/>

<br/>

<asp:ButtonID="CancelRefresh"runat="server"Text="Cancel"/>

</ContentTemplate>

</asp:UpdatePanel>

</div>

</form>

While my .cs code is as follow..

protectedvoid Page_Load(object sender,EventArgs e)

{

Label1.Text = ddl_testing.SelectedItem.Text;

}

protectedvoid ddl_testing_SelectedIndexChanged(object sender,EventArgs e)

{

System.Threading.Thread.Sleep(2000);

Label1.Text = ddl_testing.SelectedItem.Text.ToString();

}

}

But my ajax request is not cancelling on clikcking cancel button ..Can anybody tell me why..

Thanks in advance..

Since the Cancel button resides in the UpdatePanel it appends the NamingContainer in the ID.
Try indexof instead of exatct match:

args.get_postBackElement().id.indexOf ('CancelRefresh') > -1

A simple AJAX problem

I have had a ASP.NET page. Now I want to experiemt with the AJAX function. So first I drop aScriptManager and then aUpdatePanel onto the .aspx. After that, I further drag a label(Label1) and a button inside it and in a additon, I also drag a label(Label2) outside the UpdatePanel.

Inside the page_load event handler is:

Label1.Text = DateTime.Now.ToString();
Label2.Text = DateTime.Now.ToString();

However, when I click the button, two labels are updated as if a normal postback. In other words, the partial-update does not work.

What details do I miss so that the partial-update function do not work properly?

Thanks in advance.

Ricky.

Try this way..

<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>

//Place the Label within the Update panel Content template
<asp:Label ID="Label1" runat="server" Style="position: relative" Text="Label"></asp:Label>
</ContentTemplate>

//Add the trigger attribute and add as shown

<Triggers>
<asp:AsyncPostBackTrigger ControlID ="Button1" EventName ="Click" />
</Triggers>
</asp:UpdatePanel>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Style="left: 128px;
position: relative; top: 48px" Text="Button" /></div>
</form>

Thats all.

Any questions still, let me know

Sri


Hi, sridhar. After copying the web.config from an "ASP.NET AJAX-Enabled Web site" to my website. Everything got all right.

Anyway thanks for your reply.

Regards,

Ricky.

Wednesday, March 21, 2012

A question of using dollar sign...

I do'nt very clear about the using of dollar sign.

For example:

this._button$delegates =
{
click : Function.createDelegate(this, this._button_onclick)
}
(From AJAX Control toolkit)

or

Namespace.$create_AnimatedUpdatePanelOptions = function nStuff_AnimatedUpdatePanelOptions() { return {}; }
(From Nikhil's sample)

or

$get(DomElementName)
(From AJAX document, I got the meaning of this one.)

Is there any kind person to tell me the difference between those different using of dollar sign?
Thank you very much!

This is done basically for debugging support. You can read the full details inhttp://weblogs.asp.net/bleroy/archive/2007/03/15/what-are-these-foo-bar-baz-functions-in-the-microsoft-ajax-library-files.aspx


The post will be very useful!
Thanks!

A Question about Atlas client control Inheritance from Sys.UI.Select

Type.registerNamespace('Sys.UI');
Sys.UI.spxSelect = function(associatedElement) {
Sys.UI.spxSelect.initializeBase(this, [associatedElement]);
var _OnSelChange;
this.onSelChanged=this.createEvent();
this.get_selectedText = function() {
return this.element.options(this.element.selectedIndex).text;
}
this.dispose = function() {
Sys.UI.spxSelect.callBaseMethod(this, 'dispose');
}

this.getDescriptor = function() {
var td = Sys.UI.spxSelect.callBaseMethod(this, 'getDescriptor');
td.addProperty('selectedText', String,true);
return td;
}
Sys.UI.spxSelect.registerBaseMethod(this, 'getDescriptor');

this.initialize = function() {
Sys.UI.spxSelect.callBaseMethod(this, 'initialize');
_OnSelChange = Function.createDelegate(this, this.onSelChanged);
this.selectionChanged.add(_OnSelChange);

}
this.onSelChanged = function() {
this.raisePropertyChanged('selectedText');
}

}
Sys.UI.spxSelect.registerClass('Sys.UI.spxSelect', Sys.UI.Select);
Sys.TypeDescriptor.addType('script', 'spxselect', Sys.UI.spxSelect);

Why

this.onSelChanged = function() {
this.raisePropertyChanged('selectedText');
}

can't run after raise selectionChanged

Hi,

you have declared onSelChanged to be both an event:

this.onSelChanged=this.createEvent();

and a function:

this.onSelChanged = function() {

I think that removing the event declaration should fix it.


I fix it ,but it's have the same question


I have also found that if you inherit from an Atlas control, for example, Sys.UI.Button, and implement the initialize() method, the initialize() method of Sys.UI.Button does NOT get invoked because the initialize() method of Sys.UI.Button isn't registered as a BaseMethod. My question is: why not? Can anyone from the Atlas team please explain.

tzkuei


in yuor this.getDescriptor = function() {}

add line

td.addEvent('onSelChanged', true);

to register the event


Xiyuan Shen:

in yuor this.getDescriptor = function() {}

add line

td.addEvent('onSelChanged', true);

to register the event

Sys.UI.Select:

this._onSelectionChanged = function() {
this.raisePropertyChanged('selectedValue');
this.selectionChanged.invoke(this, Sys.EventArgs.Empty);
}

My Select:

var _OnSelChange;

this.initialize = function() {
Sys.UI.spxSelect.callBaseMethod(this, 'initialize');
_OnSelChange = Function.createDelegate(this, this.onSelChanged);
this.selectionChanged.add(_OnSelChange);

}
this.onSelChanged = function() {
this.raisePropertyChanged('selectedText');
}

Sorry,My question is how I can captureSelect.selectionChanged event in my select controlonSelChanged


The initialize() method of Sys.UI.Select is NOT invoked by Sys.UI.spxSelect.callBaseMethod(this, 'initialize'); therefore the onchange event is not hooked up.

I have raised this point in my post here: http://forums.asp.net/thread/1250969.aspx


if inseted line in my previous post to you getDescriptor function, then the syntax for the markup sholud look like this

<spxselect id="XXX"onSelChanged="youreventhandlerhere" ...>

</spxselect>

please notice that the name of prpoerty (red highlighted) is defiend by the line i posted for you.