Click here to Skip to main content
16,011,428 members
Home / Discussions / ASP.NET
   

ASP.NET

 
QuestionAjax Animation Extender Pin
jithusuji1-Dec-08 17:36
jithusuji1-Dec-08 17:36 
QuestionPls Help Me in displaying webforms continuously page wise Pin
Sameer-The Performer1-Dec-08 17:26
Sameer-The Performer1-Dec-08 17:26 
AnswerRe: Pls Help Me in displaying webforms continuously page wise Pin
Christian Graus1-Dec-08 20:39
protectorChristian Graus1-Dec-08 20:39 
QuestionMessage Removed Pin
1-Dec-08 14:35
Uralya1-Dec-08 14:35 
Questiondifference between asp file upload and html file upload Pin
Aslesh1-Dec-08 13:30
Aslesh1-Dec-08 13:30 
AnswerRe: difference between asp file upload and html file upload Pin
Christian Graus1-Dec-08 13:38
protectorChristian Graus1-Dec-08 13:38 
GeneralRe: difference between asp file upload and html file upload Pin
Aslesh1-Dec-08 13:45
Aslesh1-Dec-08 13:45 
QuestionExtending javascript types generated by ASP.NET Pin
bnieland1-Dec-08 11:26
bnieland1-Dec-08 11:26 
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.w3.org/1999/xhtml" >
<head runat="server">
    <title>Add Javascript Method Test</title>
	<script language="javascript" type="text/javascript">
		function getADogFromTheServer()
		{
			AddJavascriptMethodTest.wsAnimals.GetDog(GetDog_Callback_Success, GetDog_Callback_Failure);
		}
		function GetDog_Callback_Success(oDog)
		{
			alert(oDog.Color);
		}
		function GetDog_Callback_Failure(oDog)
		{
			alert("GetDog_Callback_Failure");
		}
		
	</script>    
</head>
<body>
    <form id="form1" runat="server">
	<asp:ScriptManager ID="ScriptManager1" runat="server">
		<Services>
			<asp:ServiceReference Path="~/wsAnimals.asmx" />
		</Services>
	</asp:ScriptManager> 
    <div>
		<input type="button" value="Get a Dog" onclick="getADogFromTheServer();" />
    </div>
    </form>
</body>
</html>
<!--/////////////////////////////-->
<!--   END FILE: Default.aspx  //-->
<!--/////////////////////////////-->

So, okay, you click the button, and the word "Red" is displayed in an alert.

Now, what I want to do instead is add a prototype function called
RenderForDisplay() to the Javascript type AddJavascriptMethodTest.CDog.
CDog.RenderForDisplay() would look somethig like this. It would probably be in a
seperate js file.
AddJavascriptMethodTest.CDog.prototype = { 
	RenderForDisplay: function()
	{
		return "I was given a " + this.Color + " Dog.";
	} 
}

So I can replace GetDog_Callback_Failure(oDog) in the Default.aspx with
function GetDog_Callback_Success(oDog)
{
    alert(oDog.RenderForDisplay());
}

The AddJavascriptMethodTest.CDog type is generated by the wsAnimals.asmx file at
runtime (maybe at compile time, but I don't think so).

Perhaps I could add the RenderForDisplay() function in pageLoad, but that does
not seem the right way to go.

Any Ideas?

Brett Nieland

Questionusing a dropdownlist, textbox, and a button to search sql 05 database Pin
brownk12971-Dec-08 9:22
brownk12971-Dec-08 9:22 
AnswerRe: using a dropdownlist, textbox, and a button to search sql 05 database Pin
Christian Graus1-Dec-08 9:43
protectorChristian Graus1-Dec-08 9:43 
GeneralRe: using a dropdownlist, textbox, and a button to search sql 05 database Pin
brownk12971-Dec-08 9:51
brownk12971-Dec-08 9:51 
GeneralRe: using a dropdownlist, textbox, and a button to search sql 05 database Pin
Christian Graus1-Dec-08 10:18
protectorChristian Graus1-Dec-08 10:18 
GeneralRe: using a dropdownlist, textbox, and a button to search sql 05 database Pin
Paul Conrad1-Dec-08 11:25
professionalPaul Conrad1-Dec-08 11:25 
QuestionHow to make DataGrid TemplateColumn ItemTemplate and EditItemTemplate the same without duplicating code? Pin
Cyrilix1-Dec-08 8:39
Cyrilix1-Dec-08 8:39 
AnswerRe: How to make DataGrid TemplateColumn ItemTemplate and EditItemTemplate the same without duplicating code? Pin
Samer Aburabie1-Dec-08 10:39
Samer Aburabie1-Dec-08 10:39 
GeneralRe: How to make DataGrid TemplateColumn ItemTemplate and EditItemTemplate the same without duplicating code? Pin
Cyrilix1-Dec-08 11:59
Cyrilix1-Dec-08 11:59 
Questionfile upload to FTP ? Pin
Aslesh1-Dec-08 7:37
Aslesh1-Dec-08 7:37 
AnswerRe: file upload to FTP ? Pin
Muhammad Gouda1-Dec-08 8:04
Muhammad Gouda1-Dec-08 8:04 
GeneralRe: file upload to FTP ? Pin
Aslesh1-Dec-08 8:14
Aslesh1-Dec-08 8:14 
GeneralRe: file upload to FTP ? Pin
Muhammad Gouda1-Dec-08 8:29
Muhammad Gouda1-Dec-08 8:29 
QuestionCustom Control property problem Pin
Dirso1-Dec-08 6:48
Dirso1-Dec-08 6:48 
AnswerRe: Custom Control property problem Pin
Dirso1-Dec-08 9:21
Dirso1-Dec-08 9:21 
AnswerRe: Custom Control property problem Pin
Christian Graus1-Dec-08 9:45
protectorChristian Graus1-Dec-08 9:45 
GeneralRe: Custom Control property problem Pin
Dirso1-Dec-08 12:53
Dirso1-Dec-08 12:53 
GeneralRe: Custom Control property problem Pin
Christian Graus1-Dec-08 14:18
protectorChristian Graus1-Dec-08 14:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.