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");
}