Faisal Ansari

Faisal Ansari

  • NA
  • 451
  • 830.2k

How to Apply run time Validation?

Jul 11 2011 2:01 AM
hi guys how r u ?

my Question is that,

i am applying runtime regular expression validation on textbox which was bounded with gridview and its working correctly but the problem is that data is being submitted after occuering validation  and thats not i want or thats not should be 

my code is below:

    Project1TableAdapter loadProject;
    ProjectAnswerTableAdapter insertAnswer;
    string data;
    int qid = 18;

    protected void Page_Load(object sender, EventArgs e)
    {
        loadProject = new Project1TableAdapter();
        if (!Page.IsPostBack)
        {
            ddlProject.DataTextField = "projectName";
            ddlProject.DataValueField = "project_official_id";
            ddlProject.DataSource = loadProject.GetProject();
            ddlProject.DataBind();

            ddlProject.Items.Insert(0, "Select Project");
            //Validation();
        }
        Validation();
    }

    private void Validation()
    {
        foreach (GridViewRow gvr in GridView1.Rows)
        {
            if (qid == Convert.ToInt32(gvr.Cells[0].Text))
            {
                RegularExpressionValidator regEx = new RegularExpressionValidator();
                regEx.ID = "RegularExpressionValidator1";
                regEx.ControlToValidate = "txtAnswer";
                regEx.ValidationExpression = @"\d{2}\/\d{2}\/\d{4}";
                regEx.ErrorMessage = "Date is not Valid";
                regEx.SetFocusOnError = true;
                gvr.Cells[3].Controls.Add(regEx);
            }
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        insertAnswer = new ProjectAnswerTableAdapter();
        //Validation();
        foreach (GridViewRow gvr in GridView1.Rows)
        {
            data = ((TextBox)gvr.Cells[2].FindControl("txtAnswer")).Text;
            string questionID = gvr.Cells[0].Text;
            Response.Write("<br>" + data + " ID is : " + questionID.ToString());
            //Validation();
        }
        
    }

please Reply Soon

Thanks.

Answers (3)