Showing posts with label object. Show all posts
Showing posts with label object. Show all posts

Wednesday, March 28, 2012

Calling a Webservice from within a javascript object - closure issue?

Hi,

I'm making a client side call to a web services but I need to wrap up the functionality that makes the call within another object. The code is below. The code worked fine before I wrapped it in the "ImageViewer" object but now when I try to call the Web Service Method it doesn't recognise the onComplete call back function and says "onComplete" undefined. (See the getImageIDs method). I suspect this is something to do with scope and/or closure but I've been unable to come up with a solution. What is the correct way to do this? Any help will be appreciated.

Type.registerNamespace(

"Raydius");

Raydius.ImageViewer =

function(ImageDivID, ImageListDivID){this.ImageDiv = $get(ImageDivID);this.ImageListDiv = $get(ImageListDivID);this.ImageIDs = [];this.Images = [];

}

Raydius.ImageViewer.prototype = {

getImageIDs:function(id){

// Call the WebService

RaydiusWS(id, onComplete);

},

onComplete:

function(results){

alert(results);

},

onTimeOut:

function(results){

alert(

"Timeout");

},

onError:

function(results){

alert(

"Error");

}

}

Raydius.ImageViewer.registerClass(

'Raydius.ImageViewer');function initImageViewer(){

var viewer =new Raydius.ImageViewer("ImageDiv","ImageListDiv");

viewer.getImageIDs(1);

}

See if my example here helps you:

http://forums.asp.net/thread/1574034.aspx


Thanks for the response. There was actually a daft error in my code that was stoppinmg it working both the other post you directed me to was helpful. The userContext param is very useful.

Thanks

Mat

Calling a Web service method that returns an XmlDocument object

I'm calling a webservice method that returns an XmlDocument object. I'm getting the result back object back, but in my SuccededCallback function I'm having trouble navigating through the object with JavaScript. I'm trying to use methods on the object like .selectsinglenode and .selectnodes etc, but I keep getting a script error; "Object doesn't support this property or method. Any ideas? I'll post some of the code i'm trying to work with below.

Here is how i call the method from the client ...

Navegate.Services.GeneralTasks.GetSelectedPartyNumber(guid, id, SucceededCallback, FailedCallback);

Here is the web method ...

<WebMethod()> _

<ScriptMethod(ResponseFormat:=ResponseFormat.Xml)> _

PublicFunction GetSelectedPartyNumber(ByVal sessionguidAsString,ByVal idAsInteger)As XmlDocumentDim oXmlDocumentAs XmlDocument

oXmlDocument =

New XmlDocumentWith oXmlDocument

.LoadXml(

"<DocumentPacket><Company><Number>12345</Number></Company></DocumentPacket>")EndWithReturn oXmlDocument

EndFunction

And here is how i handle the response from the webservice method but get an error on .selectsinglenode ...

function

SucceededCallback(result)

{

alert(result.selectsinglenode(

"DocumentPacket/Company/Number").text);

}

Umm... Since it took five hours for this to post i was able to find the solution elsewhere.

I worked after I user .selectSingleNode instead of .selectsinglenode.

Monday, March 26, 2012

Call Web Service : can not return dataset

Hi,

I got an error : "A circular reference was detected while serializing an object of type 'System.Globalization.Cultureinfo' when returning a dataset variable. It didn't happen when returning string.

It also run smoothly when using PageMethods of Atlas.

What's wrong with the code :

<Microsoft.Web.Script.Services.ScriptService()> _Public Class EmpServiceInherits System.Web.Services.WebServiceDim cnHimalayaAs New SqlConnection( _ ConfigurationManager.ConnectionStrings("HimalayaConnectionString").ConnectionString) <WebMethod()> _Public Function GetData(ByVal SearchCategoryAs String,ByVal NIKAs String)As Data.DataSetDim strSQLAs String Select Case SearchCategoryCase"Experience" strSQL = _"SELECT JobTitle,Company,Period,JobDesc DeptName FROM Ht_HRD_Experience " & _"WHERE NIK ='" & NIK & "'" Case "Training" strSQL = _ "SELECT Training,Category,Year,Host,Location FROM Ht_HRD_Training" & _ "WHERE NIK ='" & NIK & "'"Case"Education" strSQL = _"SELECT Education,School,Subject,YearGraduate FROM Ht_HRD_EmpEducation " & _ "WHERE NIK ='" & NIK & "'"End Select Dim sqlDaAs New SqlDataAdapter(strSQL, cnHimalaya)Dim dsDataAs New Data.DataSetIf cnHimalaya.State = Data.ConnectionState.ClosedThen cnHimalaya.Open()End If Try sqlDa.Fill(dsData)Return dsDataCatch exAs SqlExceptionThrow (New ApplicationException(ex.Message))Finally cnHimalaya.Close() dsData.Dispose()End Try End Function

thanks.

The problem is the DataSet is not natively supported by the JavaScriptSerializer.
In order to return a DataSet, you need to implement and register a custom converter for the DataSet in the config file.
There is a DataTable converter that comes with the ASP.NET 2.0 AJAX Futures November CTP that you can try to use.

HTH,

Maíra


Thanks.

Do you have article that expalin more detail with example how to do that?

Rgds


Instead of the dataset-- why don't you just return Xml?

That's good idea. I'll use it for the next project because there is a lot of code i have to change for the current project.

thanks anyway.


Instead of the dataset-- why don't you just return Xml?

Eh because using XML is pretty much a pain to use in JavaScript? <s> Wno wants to parse XML on the client side when you could instead get an object back instead?

As to the DataTable converter it's broken in Beta 2 - it's still in Beta 2 but it's returning a bogus object which appears to be an internal object. It is possible to use the converter and grab the JSON it generates but this will likely change in the future so it's probably not a good idea to use.

Here's an example (Client Code):

 function GetCustomerTable_Callback(Result) { var List = ListControl.get_element();// retrieve raw DOM element // *** This is a HACK workaround AJAX Beta 1 until Microsoft brings back a real // *** DataTable Converter currently it contains a string property that has be eval'd // *** String has trailing ( that must be trimmed off var Text= Result.dataArray.substring(0,Result.dataArray.length -1); var Table = eval( Text);// *** Clear the list firstfor (x=List.options.length-1; x > -1; x--) { List.remove(0); }for (x=0; x < Table.length; x++ ) { var Row = Table[x];// Mozilla needs to assign var option = document.createElement("option"); option.text = Row.CompanyName; option.value = Row.CustomerId;if ( window.navigator.appName.toLowerCase().indexOf("microsoft") > -1) List.add(option);else List.add(option,null); } }
 

On the server (WebService or PageMethod):

 [WebMethod]public DataTable GetCustomerTable() { busCustomer Customer =new busCustomer();if (Customer.GetCustomerList() < 0)return null; DataTable dt = Customer.DataSet.Tables["TCustomerList"];return dt; }

You also need to register the DataTableConverter:

<microsoft.web><scripting><webServices><!-- Uncomment this line to customize maxJsonLength and add a custom converter --><jsonSerialization maxJsonLength="50000"><converters><add name="DataTableConverter" type="Microsoft.Web.Preview.Script.Serialization.Converters.DataTableConverter"/></converters></jsonSerialization></webServices></scripting></microsoft.web>
 
+++ Rick -- 


I just downloaded AJAX v1.0 and now I get the circular reference error. I had the following code but I don't know what I have to change it to for my code to work properly.

<jsonSerialization maxJsonLength="50000"><converters><add name="DataTableConverter" type="Microsoft.Web.Preview.Script.Serialization.Converters.DataTableConverter"/></converters></jsonSerialization>

Anyone have any ideas? Thanks.


Sorry, this does work. I forgot to download the January Futures CTP.

You usually get this error when you try to serialize a DataTable that has no custom converter registered for it.

The Ajax v1.0 does not contain the converters for the DataTable, they are part of the ASP.NET AJAX Futures January CTP. You need to install this and add a reference to in in your project.

Have you done this?

Maíra


Hi folks,

I'm getting the same problem and am "stuck", so I appreciate some help !

I installed the ASP.NET AJAX Futures January CTP msi file.

I added a reference to my \BIN

C:\Program Files\Microsoft ASP.NET\ASP.NET 2.0 AJAX Futures January CTP\v1.0.61025\Microsoft.Web.Preview.dll

I put this in the web.config:

<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization maxJsonLength="50000">
<converters>
<add name="DataTableConverter" type="Microsoft.Web.Preview.Script.Serialization.Converters.DataTableConverter"/>
</converters>
</jsonSerialization>
</webServices>

But still no go...

Do I have to MERGE the web_CTP.config with my standard AJAX 1.0 web.config or replace it entirely ?

It seems fairly complex to do so. Is their a pre-existing AJAX 1.0 / Jan CTP combined web.config available ?

Thanks, LA Guy


Hi again,

I missed these settings but still no go.

<jsonSerialization maxJsonLength="50000">
<converters>
<add name="DataSetConverter" type="Microsoft.Web.Preview.Script.Serialization.Converters.DataSetConverter, Microsoft.Web.Preview"/>
<add name="DataRowConverter" type="Microsoft.Web.Preview.Script.Serialization.Converters.DataRowConverter, Microsoft.Web.Preview"/>
<add name="DataTableConverter" type="Microsoft.Web.Preview.Script.Serialization.Converters.DataTableConverter, Microsoft.Web.Preview"/>
</converters>
</jsonSerialization>

Thanks, LA Guy


I'm getting the same problem and am "stuck", thk


Guys I'm getting it working fine with the January CTP with the

<system.web.extensions>
<scripting>
<webServices>
<!-- Uncomment this line to customize maxJsonLength and add a custom converter -->
<jsonSerialization>
<!--<jsonSerialization maxJsonLength="500">-->
<converters>
<add name="DataSetConverter" type="Microsoft.Web.Preview.Script.Serialization.Converters.DataSetConverter, Microsoft.Web.Preview, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
<add name="DataRowConverter" type="Microsoft.Web.Preview.Script.Serialization.Converters.DataRowConverter, Microsoft.Web.Preview, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
<add name="DataTableConverter" type="Microsoft.Web.Preview.Script.Serialization.Converters.DataTableConverter, Microsoft.Web.Preview, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
</converters>
</jsonSerialization>

Config setting and the reference added to the Preview dll.

Can we assume these implementations would be included in the future MS Ajax releases bcoz I'm going to use these JSON converters for a real product.


I am running the latest AJAX 1.0 product set. When I apply the changes as mentiond above, I no longer am able to access my webmethod. I get a javascript error that states that method is undefined? when I comment the section above, I get circular reference encountred (since I am trying to return a datatable)

What am I doing wrong?

Call RegisterStartupScript problem

Dear friends,

I'm having problems to execute a RegisterStartupScript when I click on a button.
For example:

protected void btnSave_Click(object sender, ImageClickEventArgs e)
{
Page.ClientScript.RegisterStartupScript(typeof(Page), "OnLoad", "<script language='javascript'>alert('OK');</script>" );
}

If I disable the Atlas tags, the RegisterStartupScript works OK, but if I enable the Atlas tags, It doens't work.

How I correct this?

Try setting addScript Tags = True instead

Page.ClientScript.RegisterStartupScript(typeof(Page), "OnLoad", "alert('OK');", True);

RG


I put the True in the last parameter but not works.
Remember thar I'm using Atlas ok.

Do you havy any other sugestion?


just putting the following line in a button even which is inside the update panel works fine for me

Page.ClientScript.RegisterStartupScript(typeof(Page),"OnLoad","alert('OK');",true);

there must be some other issues in the page, you have to show the code.

Thanx


Not work again.

All my controls are inside the same updatepanel, for example:
...
<body>
<form .....>
<atlas:updatepanel .....>
//Here all tags and all controls
</atlas>
</form>
</body>

Is It a problem?


well you have to paste the whole non working code, without the code its hard to tell...


use following code

protected void btnSave_Click(object sender, ImageClickEventArgs e)
{
Page.ClientScript.RegisterStartupScript(typeof(Page), "OnLoad", "alert('OK');",true );
}

I think u'r code remain having "<script language='javascript'>alert('OK');</script>" . This is the problem. U shouldn't use "<script" tag if last parametere is 'true'


I have found that I must have the semi-colon after the script. EG:


page.ClientScript.RegisterClientScriptBlock(typeof(Page), "ShowMsg", "alert('hi')", true);


doesn't work BUT


page.ClientScript.RegisterClientScriptBlock(typeof(Page), "ShowMsg", "alert('hi');", true);

does!


is there a solution? I have the same porblem like rasl


http://ajax.asp.net/docs/mref/T_System_Web_UI_ScriptManager.aspx

You can use the scriptmanager.

Dim msgAsString ="Group '" & u.GroupCode &"' has been deleted."
ScriptManager.RegisterStartupScript(Me.Page,GetType(String),"alertdelete","reportMessage('" & Utils.ForJavascript(msg) &"');",True)

Saturday, March 24, 2012

Call Animation versus hooking an event

I'm looking for a way to perform an animation on a dynamically defined object.

For instance...Let's take the Toolkit demo animation where a dialog flies out from a button and the button is grayedout.

Assume there is a page that has a list of items and each item has an associated More... button. What I would like is to be able to call a function when I click the More... button that would pass the button and div to the AE that I want to flyout . This would prevent me from having to code an AE block for each item and hardcode the name of the control I want to hook for the onClick event. In the end I'm trying to accomplish two primary things: 1) Avoid writing the AnimationExtender block for every item by hand and hard coding the id of the link/button and the div tag [This would also reduce page size]. 2) Avoid writing a function in the code behind that has to programatic create the AE blocks [Avoiding this would increase speed of rendering].

Is this possible with the current functionality of the AE or would it require a rewrite?

Hi there,

I am also trying to do something on the same lines as you just mentioned. I just wanted to know if you were able to do the above mentioned task. Kindly let me know if you have any information about how this can be done.

Thanks in advance,

Cheers

Vishy


I have not been able to accomplish anything to this date. I have currently put it on the back burner pending more time to work on it.

Wednesday, March 21, 2012

CalendarExtender seems to cause loss of Session

I have a web app which passes a Session object between pages. When I added a CalendarExtender to the page, that Session object is null when I navigate to the next page, even if I never actually click on the associated ImageButton. The version number of the AjaxControlToolkit.dll is 1.0.10920, which I believe is the latest.

Any thoughts?

Thanks.

Dan

I noticed another post today about the CalendarExtender causing memory leaks in IE. Based on one of the suggestions in that thread, I tried my app in both IE7 & Firefox outside of VS. It exhibited the same problems as with IE inside VS.

Any suggestions would be greatly appreciated.

Thanks.

Dan


Hi Dhurwitz,

As we know, CalendarExtender works on the client and session works on the server side so it seems your issue is not caused by the CalendarExtender.

I suggest that we should use a discharging method. First remove the CalendarExtender from your page and have a test. Here is thedocument which shows how to resolve the session issues.

Best regards,

Jonathan

CalendarExtender popup out of position in IE7

Here is the object with its CalendarExtender attached:

<td style="width:30%" align="left">
<asp:TextBox Width="164px" SkinID="sk_TextBox" ID="txt_Loan_Exit_Date" runat="server"></asp:TextBox>
<ajaxToolkit:CalendarExtender runat="server"ID="ce_Loan_Exit_Date" TargetControlID="txt_Loan_Exit_Date"></ajaxToolkit:CalendarExtender></td>

When i click on the textbox the calendar pops up high up on the page. This seems to only happen when the page has a scroll bar. I have tried setting the PopupPosition to no avail. I also tried the stylesheet approach:

.CalendarExtender

{

z-index :1001;

}

to no avail as well. When i check this in FireFox all is well.

Any help would be appreciated.

Thanks!

That's pretty odd. I have an idea that there's some broken tag in the html somewhere though. Check the page for invalid HTML in Visual Studio as well as in the browser. IE handles broken tables in an... interesting... manner.


No broken HTML that i can see. This is a page with 6 AJAX tabs on it. All the other tabs work fine, dont have to scroll to see all information, even the date field at the top of this "contacts" tabs works properly.

I am having this problem on a gridview as well. if the gridview requires scrolling and the control to be updated is a little more than a page down and is clicked the calendar pops up on the top of the screen. grrrr... this isnt really a deal breaker for the application, i'm just a stickler for detail.


I feel your pain... I'm just the same when it comes to details...

Try removing the styling from the td to see if that has any affect.

Also, I'd like to see a test like this without a table. Just text & paragraph tags that take up a few pages with textbox's & calendarextenders scattered throughout. I wonder if you'd see this problem without the table. Gridviews also render as tables, so this would be a good experiment.

Also try on a page with _nothing_ else. Stick a really long gridview on a simple ASPX page and put a calendarextender in it to see if you have the same problem.

What version of the toolkit are you using?


Taking out the formatting did nothing. Same thing without table and only <p> tags. *sigh

i am thinking this is just something weird with IE7.

Toolkit version 10920.


Well on the upside this isn't your fault.

Yea that's a real pain. It's obviously an IE7/AJAX Toolkit quirk. I suppose you could a) put up with it b) use a third party control c) submit a bug & wait d) see if you can fix the bug yourself and submit a patch ;)