Could any one tell me why this is not working, I think I have the correct coding here.
Located in the App_Code folder
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
///
/// The OnPreRender event. Use this event to raise any events that need to be fired before our controls are rendered.
///
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);
}
}
}
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 Namespace="CustomControls" Assembly="__code" tagprefix="cc1" %>
<asp:Button ID="btnSubmit" Text="Submit" runat="server" />
Jose SaizPosted Oct 4, 2015, 9:49 AM
Thanks Manas for the reply, but your suggestion only works if the javascript(validatenumeric.js) is present as part of the deployed project, that is why I had the code as follow.
string resourceName = "CustomControls.validatenumeric.js";
ClientScriptManager cs = this.Page.ClientScript;
cs.RegisterClientScriptResource(typeof(CustomControls.NumbericTextBox), resourceName);
Per Microsoft and expert the above code should embed the javascript code as part of the custom control and no need for the file(validatenumeric.js) to be deployed with the web project, I may be wrong or missing some code that needs to be part of the above mentioned in order for the javascript function to be completely embedded in the custom control and works without the need of the physical javascript file.
Thanks again for your effort and help.
Manas MohapatraPosted Oct 4, 2015, 1:58 AM
Jose SaizPosted Oct 3, 2015, 2:09 PM
I assume that the javascript is loaded based on this shown on the view source.
<script src="/TestNumeriControl/WebResource.axd?d=aoixVmmqlARi6tMLR10tRqziO42AypoBYF1F0ngIQilMfEcSNZh-6RqCFr6QBtVD8WdRKfpQtJ93MW-P6lBMnogj3qSXYZYbz2VMBpcAW5fEBQMZJueZweGWQX6SxXtsbHCTIdFaS_lS9yfa5hKGEg2&t=635794625031786312" type="text/javascript">script>
Manas MohapatraPosted Oct 3, 2015, 10:31 AM
Jose SaizPosted Oct 3, 2015, 10:13 AM
Yes, it does take a look
---------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------
Manas MohapatraPosted Oct 3, 2015, 10:08 AM