Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Extending javascript types generated by ASP.NET

Extending javascript types generated by ASP.NET

Scheduled Pinned Locked Moved ASP.NET
csharpjavascripthtmlasp-netlinq
1 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    bnieland
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups