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

Wednesday, March 28, 2012

How do I use JavaScriptSerializer on only certain properties of an object?

I have an object that inherits from UserControl. I want to seralize certain properties in this object using JavaScriptSerializer (or another method that does the same thing). Unfortunatly, if I try to seralize the control itself, I get a crash.

I am forced to make another object that contains all the properties that need to be seralized. The properites in my control then refrence the properties of that object. Though this works, it leaves my control with a bunch of properties which are effectively nothing more than pointers to another object that exists soley to make javascript seralization to work.

Is there way around this? I want to be able to use JSON on a usercontrol and be able to specify which properties I want seralized to JSON.

Try using the ScriptIgnore attribute in the properties you don't want to be serialized.

Hope this helps,

Elias.


I tried to override every virutal property on usercontrol and I still got "Cannot get inner content of because the contents are not literal."Something in my base class (usercontrol) seems to be causing problems

How do I recieve a JavaScript object as a webmethod parameter?

I create a custom JavaScript object on the client side attempted to send to a webmethod and I kinda got it - but do not know how to get the date. The test method looks like this:

[WebMethod]
public string TestMethod(Object obj)
{
return "test";
}

So how can cast this in to the right object to access the data? I can see the contents if I watch it but don't know how to cast it to an object I can use it

Assuming your javascript object is something like the following

{index:-1,fileId:-1,fileExt:\"xls\",issueDate:\"1 Jan 2007\"}

you can create a structure in your serverside code to handle it

Public Structure Doc
Public index As Integer
Public fileId As Long
Public fileExt As String
Public issueDate As Date
End Structure

<WebMethod()> _
Public Function SaveDocs(ByVal docs As Doc()) As Doc()


One more question - if you don't mind:

If my JSON contains an Array - Can you tell how to make that happen? :


My BO:

 [Serializable]public class Supplier : Base {public string Name {get;set; }public string Phone {get;set; }public string Fax {get;set; }public string Website {get;set; }public string Status {get;set; }public Site Site {get;set; }public DateTime Since {get;set; }public Array Contacts {get;set; }public Supplier() {this.Site=new Site();this.Contacts =new Member[0]; } }

Member is also another Object.

My JSON looks something like:

Supplier = {"ID":-1,"Name":"","Phone":"","Website":"","Site":{"Address1":"","Address2":"","City":"","State":{"Name":"","Abbreviation":"","ID":-1},"ZipCode":""},"Contacts":[]};

Its the Contacts that is tripping me up.


Asp.net ajax does the trick for us, I mean the serialization/deserialization.


But for some reason it not filling all the objects - I check the JS object prior to calling the webmethod and the fields are populated but when it casted in the server object - some properties are not filled. Also, my other question is how to pass the array object that is a property of the JS object.


My mistake - I was expecting to receive the parent object and passing the child.

Monday, March 26, 2012

How do I consume a JavaScript object on the main page in an ASCX control?

Hello,

I am using the ASP.Net AJAX client side libraries, and I want to be able to write Web User Controls (ASCX) that I dynamically load into my site. I want to be able to have a global object in my main page that is used by each ASCX control that is loaded into the page. As soon as the ASCX control is loaded, I want it run its script to call a method in the global object.

I am having a "timing problem" with my code. In order to use the ASP.Net AJAX libraries (like the "Type" object to register namespaces, etc.), I have placed the JavaScript on the main page in the "pageLoad" function.

Right now I am using the "setTimeout" function in my ASCX script to delay it a few seconds so I can be sure that the script in the main page has completely loaded before I make a call to the global object.

Is there any way that I can ensure that the script on the main page has loaded and stop using the "setTimeout" method in my ASCX script?

Here is the markup for my main page…

1<%@dotnet.itags.org. Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="StartupScriptTest._Default" %>23<%@dotnet.itags.org. Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>45<%@dotnet.itags.org. Register src="WebUserControl1.ascx" tagname="WebUserControl1" tagprefix="uc1" %>67<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">89<html xmlns="http://www.w3.org/1999/xhtml" >10<head runat="server">11 <title>Untitled Page</title>12 <script type="text/javascript">1314 var x = null; // <- I want to access this in the ascx control after pageLoad is finished1516 function pageLoad()17 {18 Type.registerNamespace('MyNamespace');1920 MyNamespace.MyClass = function()21 {22 MyNamespace.MyClass.initializeBase(this);23 }2425 MyNamespace.MyClass.prototype =26 {27 sayHello : function(message)28 {29 alert(message);30 }31 }3233 MyNamespace.MyClass.registerClass("MyNamespace.MyClass");3435 x = new MyNamespace.MyClass();36 }3738</head>39<body>40 <form id="form1" runat="server">41 <div42"ToolkitScriptManager1" runat="server">43 </cc1:ToolkitScriptManager>44 </div>45 <uc1:WebUserControl1 ID="WebUserControl11" runat="server" />46 </form>47</body>48</html>49

…and here is the markup for my ASCX control…

1<%@dotnet.itags.org. Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs" Inherits="StartupScriptTest.WebUserControl1" %>2<script type="text/javascript">34 setTimeout('x.sayHello("hello")', 1000); // <- Any way to not use 'setTimeout'?56<> 

Thank you for your help,

Dave

hello.

i'll just enumerate the things i think that are important:

1. do you need to define the class in the load event? I think that you can put it outside, in an external file and load it so that it's available at the begining of the page

2. are you wrapping any html control or behavior that exists on the page? if not, then i think that you can also insert your x = new class() at the begining of the class declaration.


Hi restating what Luis has said, you gotto implement something like this.,

Create a .js file consider sample.js.

Put this code inside

Type.registerNamespace('MyNamespace');

MyNamespace.MyClass = function() { MyNamespace.MyClass.initializeBase(this); } MyNamespace.MyClass.prototype = { sayHello : function(message) { alert(message); } } if ( typeof (Sys) !== 'undefined' ) Sys.Application.notifyScriptLoaded();

register this sample.js in the scriptmanager.


<asp:ScriptManager ID="ScriptManager1" runat="server" >
<Scripts>
<asp:ScriptReference Path="../../client_scripts/sample.js" />
</Scripts>
</asp:ScriptManager>

Then create the global variable inside your function. var x = new MyNamespace.MyClass();

Wednesday, March 21, 2012

How can I get more size back from my WS? - System.InvalidOperationException -- Maximum len

I'm working on this webservice. I can easily getting my databack (an array of my custom object) but i'm now trying to return an array of 1,000 results to 5,000 results but I keep getting the error:

The Server method 'getPlaybackInformation' failed with the following error:
System.InvalidOperationException -- Maximum length exceeded.

I tried putting this in my web.config:
<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization maxJsonLength="500000">
</jsonSerialization>
</webServices>
</scripting>
</system.web.extensions>

but that got me no where.

Any Thoughts?

I came across the same problem, my web service returned a string that exceeded the limit

I applied the maxJsonLength that you attempted, into my web.config, and that fixed my problem completely...


I don't believe that changing the max JSON serialization length will do anything for you. That is how much data can be serialized at one into JSON, but you aren't returning JSON, you are returning an array of objects. What you need to do is turn up the max allowable data that can be transferred in the application. In the web.config add:

<configuration>
<system.web>
<httpRuntime maxRequestLength="500000" />
</system.web>
</configuration>

Note that the size is in kilobytes.

http://msdn2.microsoft.com/en-us/library/e1f13641(vs.71).aspx