r.a.d.controls version |
Q4 2006 SP2 |
.NET version |
2.0 |
Visual Studio version |
2005 |
programming language |
C# |
browser support |
all browsers supported by r.a.d.controls |
PROJECT DESCRIPTION I'm working first time on RadControls for ASP.NET to build my first test application. Sometime I need to send and receive in Javascript function object instead of values, but I can't found a way to create JSON object automatically.
With JsonSerializer class e JsonAttribute class you can define C# classes and pass them to client javascript functions on fly with injection. With Serialize method and string type support you can manage simple object in server to client and client to client communications. But adding Deserialize functionality and support for IList property you can manage complex objects from client to client, client to server, server to client.
Download sample and view step by step implementation.
Step 1
Define object to manage as C# class.
Step 2
Add custom attribute JsonAttribute defining property name in JSON object for each property.
Example:
private string _userAction; |
[Json("UserAction")] |
public string UserAction |
{ |
get { return _userAction; } |
set { _userAction = value; } |
} |
|
Step 3
Insert this code in your C# class:
public override string ToString() |
{ |
return this.ToString('\''); |
} |
public string ToString(char stringDelimiter) |
{ |
JsonSerializer s = new JsonSerializer(this.GetType(), stringDelimiter); |
return s.Serialize(this); |
} |
Step 4
Before injecting script in Dialog create C# class and then compose script.
Example (server-side):
RefreshArgs args = new RefreshArgs(); |
args.Source = "MyDialog"; |
args.SourceType = "RadWindows"; |
args.UserAction = "Process1"; |
|
InjectScript.Text = "<script type='text/javascript'>CloseAndRebind(" + args + ");</script>"; |
Step 5
Retrieve values from function from caller.
Example:
function RefreshControls(refreshArgs) |
{ |
alert(refreshArgs.Source + " (" + refreshArgs.SourceType + ") => " + refreshArgs.UserAction + " requested."); |
} |
I hope this code is usefull.
Emanuele
Oppidum s.r.l.