Friday 23 October 2015

How to validate dynamically created control in asp.net


validate dynamic created control


Description:-




In this example we explain that how to validate Date format in dd/mm/yyyy in asp.net C# backend code at runtime validation using Regex validator provided by .Net Framework.

We all know very to validate any like Date, only number, required validation, and Range validation is easily at client side but my problem is that it will be validated by runtime.

For example in my real project in which one form name like create report in which form one is dropdown and its contain three items like string, integer and Date. And second control is textbox and one submit button. Now condition is that if user select string item from dropdown list then only string are allow to enter in below Textbox control same like if user select Date item from dropdown list then only user allow to enter Date only and it also in dd/mm/yy format and same for Integer items. So it is difficult task to validate at runtime for me at first time. And then finally I got the solution are described bellows.


Code.aspx.cs:-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;

namespace kiritblog
{
    public partial class validation : System.Web.UI.Page
    {
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            GridView grow = this.FindControl("Gridview1") as GridView;
            TextBox txtDisplayName = grow.FindControl("txtDisplayName") as TextBox;
            DropDownList drpDatatype = grow.FindControl("drpDatatype") as DropDownList;
            DropDownList drpRequiresInput = grow.FindControl("drpRequiresInput") as DropDownList;
            TextBox txtMaxLength = grow.FindControl("txtMaxLength") as TextBox;
            DropDownList drpDefaultValueType = grow.FindControl("drpDefaultValueType") as DropDownList;
            TextBox txtDefaultValue = grow.FindControl("txtDefaultValue") as TextBox;
            //commented by kirit
            if (drpDatatype.SelectedValue.ToString().Equals("DateTime"))
            {
                if (!drpDefaultValueType.Text.Equals("Null"))
                {
                    var match = Regex.Match(txtDefaultValue.Text, @"^(((0?[1-9]|1[012])-(0?[1-9]|1\d|2[0-8])|(0?[13456789]|1[012])-(29|30)|(0?[13578]|1[02])-31)-(19|[2-9]\d)\d{2}|0?2/29-((19|[2-9]\d)(0[48]|[2468][048]|[13579][26])|(([2468][048]|[3579][26])00)))$");
                    if (!match.Success)
                    {
                        trMessage.Show(lblMessage.ReportError("Please enter Date mm-dd-yyyy Properly."));
                        return false;
                    }
                }
            }
            else if (drpDatatype.SelectedValue.ToString().Equals("Integer"))
            {
                if (!drpDefaultValueType.Text.Equals("Query"))
                {
                    var match = Regex.Match(txtDefaultValue.Text, @"^[0-9]*$");
                    if (!match.Success)
                    {
                        trMessage.Show(lblMessage.ReportError("Please enter only Number value in Textbox."));
                        return false;
                    }
                }
            }
            else if (drpDatatype.SelectedValue.ToString().Equals("String"))
            {
                if (drpDefaultValueType.Text.Equals("SpecifiedValue"))
                {
                    if (txtDefaultValue.Text.Trim().Equals(""))
                    {
                        trMessage.Show(lblMessage.ReportError("Please enter value in Textbox."));
                        return false;

                    }
                }
            }

        }
    }
}


0 comments:

Post a Comment