Extending javascript types generated by ASP.NET
-
Folks,
How can I add functions to a JavaScript type generated by using the GenerateScriptType in a web service.
Basicly, the C# class has data members that are serialized and passed to the client to be deserialized javascript objects whoose type bears the same name as the c# type.
Of course, methods can not be shared.
So let's say I have a c# type/////////////////////////
// BEGIN FILE: CDog.cs //
/////////////////////////
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;namespace AddJavascriptMethodTest
{
[Serializable]
public class CDog
{
public CDog()
{
this._sColor = "Brown";
}
public CDog(string sColor)
{
this._sColor = sColor;
}private string \_sColor = null; public string Color { get { return \_sColor; } } }
}
/////////////////////////
// END FILE: CDog.cs //
/////////////////////////Then I create a web service to return an object of CDog. I also add a GenerateScriptType attribute to generate a javascript type called AddJavascriptMethodTest.CDog
////////////////////////////////
// BEGIN FILE: wsAnimals.asmx //
////////////////////////////////
using System.Web.Services;
using System.Web.Script.Services;namespace AddJavascriptMethodTest
{
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
public class wsAnimals : System.Web.Services.WebService
{
[WebMethod]
[GenerateScriptType(typeof(CDog))]
public CDog GetDog()
{
return new CDog("Red");
}
}
}
////////////////////////////////
// END FILE: wsAnimals.asmx //
////////////////////////////////Now, the default.aspx looks like this (and this version works fine)...
<!--/////////////////////////////-->
<!-- BEGIN FILE: Default.aspx //-->
<!--/////////////////////////////-->
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="AddJavascriptMethodTest._Default" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www