Friday, April 10, 2009

Create a textbox dynamically and validate for numeric value

Hi

This article deals with how to create the textbox dynamically and validate for numeric value only.

place a empty panel in aspx page(Its name panel1).Add the text box in form load event by using the below code


protected void Page_Load(object sender, EventArgs e)
{
TextBox txt = new TextBox();
txt.Attributes.Add("onkeypress", "return AllowNumeric(window.event);");
Panel1.Controls.Add(txt);
}



txt.Attributes.Add("onkeypress", "return NumericOnly(window.event);");
The above mentioned c# statement links the client onkeypress event with the NumericOnly function

Add the below script function in script area in aspx page







ikeycode value consist the ascii value for the keyed character. if we keyed other than the ascii value of greater than 47 and less than 58 means it w'nt accept. It produces the alert message and won't accept the keyed value.



1 comment:

  1. Good start. Informative one for beginners.

    Great! All the Best!!

    Regards,
    E Thamizharasan

    ReplyDelete