Jose Saiz

Jose Saiz

  • 1.4k
  • 243
  • 98.7k

Custom WebControl

Oct 3 2015 9:55 AM

Could any one tell me why this is not working, I think I have the correct coding here.

I have a C# Custom TextBox Control  that only allows numeric value which is validated with a javascript function, when using the control no error is thrown
except when the any alphanumeric key is pressed it pops this  "JavaScript runtime error: 'IsNumbericValue' is undefined" but the IsNumericValue javascript function is defined it just that the webResource.axd it does not see it. , here is my code

Located in the App_Code folder 

using System;
using System.Collections.Generic;

using System.ComponentModel;

using System.Drawing;

using System.Linq;

using System.Security.Permissions;

using System.Text;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

namespace CustomControls

{

[AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal)]

[AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]

[DefaultProperty("Text")]

[ToolboxData("<{0}:NumbericTextBox runat=server></{0}:NumbericTextBox>")]

//--public class NumbericTextBox : TextBox //- When This used it does not call the javascript

public class NumbericTextBox : CompositeControl

{

[Bindable(true)]

[Category("Appearance")]

[DefaultValue("")]

[Localizable(true)]

public override string ID

{

set

{

base.ID = value;

}

}

public override Unit Height

{

get

{

return base.Height;

}

set

{

base.Height = value;

}

}

public override Unit Width

{

get

{

return base.Width;

}

set

{

base.Width = value;

}

}

#region OnPreRender

/// <summary>

/// The OnPreRender event. Use this event to raise any events that need to be fired before our controls are rendered.

/// </summary>

protected override void OnPreRender(EventArgs e)

{

base.OnPreRender(e);

string resourceName = "CustomControls.validatenumeric.js";

ClientScriptManager cs = this.Page.ClientScript;

cs.RegisterClientScriptResource(typeof(CustomControls.NumbericTextBox), resourceName);

}

#endregion OnPreRender

protected override void CreateChildControls()

{

this.Controls.Clear();

TextBox txtBox1 = new TextBox();

//- Numeric TextBox

txtBox1.ID = this.ID;

txtBox1.Width = this.Width;

txtBox1.Height = this.Height;

txtBox1.ToolTip = "Numeric Value Only";

txtBox1.Attributes.Add("onkeypress", "return IsNumbericValue(event)");

txtBox1.Style.Add("text-align", "right");

//- Add the control to the environment

this.Controls.Add(txtBox1);

}

}

}

------------------------------------------------- 
Javascript Located in the Root Folder
------------------------------------------------------------------------- 

function IsNumbericValue(evt) {

var charCode = (evt.which) ? evt.which : event.keyCode;

if (charCode != 46 && charCode > 31 && (charCode < 48 || charCode > 57)) {

return false;

}

else {

// If the number field already has . then don't allow to enter . again.

if (evt.target.value.search(/\./) > -1 && charCode == 46) {

return false;

}

return true;

}

}

 
-------------------------------------------------
Register the C# code as follow 
------------------------------------------------- 

<%@ Register Namespace="CustomControls" Assembly="__code" tagprefix="cc1" %>

-------------------------------------------------
Intantiate the control as follow 
------------------------------------------------- 
 
<cc1:NumbericTextBox id="txtBox1" runat="server" />

<asp:Button ID="btnSubmit" Text="Submit" runat="server" />

 
What am I doing wrong?
 
TIA 
 
 

 


Answers (6)