Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2005 > C# 2005
|
C# 2005 For discussion of Visual C# 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 16th, 2007, 01:42 PM
SV SV is offline
Authorized User
 
Join Date: Oct 2007
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default RegisterClientScriptResource to include javascript

I am trying to run an example from ASP.Net Professional 2.0 which includes a javascipt file using RegisterClientScriptResource. When I run this example, I get the following error message in the browser:

  'CallbackHandler' is undefined. This handler is defined in the javascript include file. Looks like my file is not being included in the assembly. Here is the code. Any suggestions:


Control Code:
namespace WebControlLibrary1
{

    [DefaultProperty("Text")]
    [ToolboxData("<{0}:WebCustomControl1 runat=server></{0}:WebCustomControl1>")]
    public class WebCustomControl1 : WebControl, ICallbackEventHandler
    {
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        [Themeable(false)]
        public string Text
        {
            get
            {
                String s = (String)ViewState["Text"];
                return ((s == null) ? String.Empty : s);
            }

            set
            {
                ViewState["Text"] = value;
            }
        }

        protected override void RenderContents(HtmlTextWriter output)
        {
            output.RenderBeginTag(HtmlTextWriterTag.Div);

            output.AddAttribute(HtmlTextWriterAttribute.Type, "text");
            output.AddAttribute(HtmlTextWriterAttribute.Id, this.ClientID);
            output.AddAttribute(HtmlTextWriterAttribute.Name, String.Concat(this.ClientID, "zz"));
            output.AddAttribute(HtmlTextWriterAttribute.Value, this.Text);
            output.AddAttribute("OnBlur", "ClientCallback()");
            output.RenderBeginTag(HtmlTextWriterTag.Input);
            AddAttributesToRender(output);

            output.RenderEndTag();
            output.RenderEndTag();
        }

        protected override void OnPreRender(EventArgs e)
        {
            Page.ClientScript.RegisterClientScriptResource(thi s.GetType(), "Utility.js");
            Page.ClientScript.RegisterClientScriptBlock(
                typeof(Page),
                "ClientCallback",
                "function ClientCallback() {" +
                "args = document.getElementById('" + this.ClientID + "').Value;" +
                Page.ClientScript.GetCallbackEventReference(this, "args", "CallbackHandler", null, "ErrorHandler", true) + "}",
                true);

        }

        public void RaiseCallbackEvent(string eventArguments)
        {
            int result;
            if (!Int32.TryParse(eventArguments, out result))
            {
                throw new Exception("The method or operation not implemented");
            }
        }

        public string GetCallbackResult()
        {
            return "Valid Data";
        }
    }
}

AssemblyInfo.cs file:

using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;


// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("WebControlLibrary1")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("WebControlLibrary1")]
[assembly: AssemblyCopyright("Copyright © 2007")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("3dbd1c3c-64f0-480c-9544-051f28a9540b")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Revision and Build Numbers
// by using the '*' as shown below:
[assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: System.Web.UI.WebResource("WebControlLibrary1.Util ity.js", "text/javascript")]


Utility.cs File:
This file is in the root folder of the project.

// JScript File
var args;
var ctx;

function ValidateText(ctl)
{
    if (ctl.value=='') {
        alert('Please enter a value.');
        ctl.focus();
    }
}

function CallbackHandler(args,ctx)
{
    alert("The data is valid");
}

function ErrorHandler(args,ctx)
{
    alert("Please enter a number");
}
 
Old November 16th, 2007, 02:18 PM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Are you compiling the Utility.js file as an embedded resource? Is it in the root directory of the project?

/- Sam Judson : Wrox Technical Editor -/
 
Old November 16th, 2007, 02:52 PM
SV SV is offline
Authorized User
 
Join Date: Oct 2007
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks, Sam.

Are you compiling the Utility.js file as an embedded resource?
    I am doing nothing special but do a regular build. How do I compile the file as an embedded resource. I have defined it as web resource in the Assembly file.

Is it in the root directory of the project?
 Yes. The include file is in the roo directory of the project.
 
Old November 16th, 2007, 03:10 PM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

If you select the file in the Solution Explorer and look at its properties on of them should read "Compile Action" or something. It will probably have a value of "Content". Change that to "Embed Resource" and try again.


/- Sam Judson : Wrox Technical Editor -/
 
Old November 16th, 2007, 03:17 PM
SV SV is offline
Authorized User
 
Join Date: Oct 2007
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Sam, I have changed the Utility.js to be an embedded resource. I still get the same error. Any more suggestions?
 
Old November 28th, 2007, 11:41 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

You've used 'WebControlLibrary1.Utility.js' in one place and 'utility.js' in another. I think they should both be the same...(and I'd recommend 'WebControlLibrary1.Utility.js').

/- Sam Judson : Wrox Technical Editor -/





Similar Threads
Thread Thread Starter Forum Replies Last Post
c #include vicoldan Need help with your homework? 0 December 3rd, 2007 06:15 AM
#include arielote C# 3 February 26th, 2006 07:54 PM
difference between include file & include virtual crmpicco Classic ASP Basics 2 January 23rd, 2006 11:50 AM
xsl:include within include chuck123ie XSLT 1 January 5th, 2006 11:07 AM
passing data btwn asp page and javascript include Justine Classic ASP Professional 8 August 20th, 2004 02:23 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.