Click here to Skip to main content
15,899,825 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a form that uses Range Validations. The Range Validations fires for the code behind that I have. When a user enters data that is 20% Lower/Higher than what is in the textbox the Validation fires. This works fine. What the problem is when a user enters data into the last textbox and clicks away or tabs the rangevalidator stops firing. I also have a TextBox_TextChange for the last textbox to do a math operator. Will that stop the validator from firing? If so, How can I fix it? Why does this happen? I want the rangevalidator to stay firing when the user clicks submit to save the data also. I set the code in .aspx to this:

ASP.NET
<asp:Button ID="ButtonSubmit" runat="server" CausesValidation="False" onclick="ButtonSubmit_Click" 
                    Text="Submit" Width="75px" />


I still want to submit the data with the validation still firing because the user will have to print out the form to show the validation. What do I have wrong that needs to be corrected?

C#
protected void ButtonSubmit_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["HotConnectionString"].ConnectionString);
        con.Open();

        SqlCommand cmd = new SqlCommand("Insert into Table2013 (INST_ID, FT_UNDERGR, DATE, FT_GRAD, FTE_UNDERG, FTE_GRAD, NON_CREDIT, TOTAL_FTE, FCFTUHC, FCFTPBHC, FCPTUHC, FCPTPBHC, NCHC, FTEYR, TIME) values (@INST_ID, @FT_UNDERGR, @DATE, @FT_GRAD, @FTE_UNDERG, @FTE_GRAD, @NON_CREDIT, @TOTAL_FTE, @FCFTUHC, @FCFTPBHC, @FCPTUHC, @FCPTPBHC, @NCHC, @FTEYR, @TIME)Insert into Table12012 (INST_ID, FT_UNDERGR, DATE, FT_GRAD, FTE_UNDERG, FTE_GRAD, NON_CREDIT, TOTAL_FTE, FCFTUHC, FCFTPBHC, FCPTUHC, FCPTPBHC, NCHC, FTEYR, TIME) values (@INST_ID, @FT_UNDERGR, @DATE, @FT_GRAD, @FTE_UNDERG, @FTE_GRAD, @NON_CREDIT, @TOTAL_FTE, @FCFTUHC, @FCFTPBHC, @FCPTUHC, @FCPTPBHC, @NCHC, @FTEYR, @TIME)", con);

        cmd.CommandType = CommandType.Text;

        cmd.Parameters.AddWithValue("@INST_ID", TextBoxINST_ID.Text);
        cmd.Parameters.AddWithValue("@FT_UNDERGR", TextBoxFTUG.Text);
        cmd.Parameters.AddWithValue("@FT_GRAD", TextBoxFTG.Text);
        cmd.Parameters.AddWithValue("@FTE_UNDERG", TextBoxTHUGDR.Text);
        cmd.Parameters.AddWithValue("@FTE_GRAD", TextBoxTHGDR.Text);
        cmd.Parameters.AddWithValue("@NON_CREDIT", TextBoxNCCDR.Text);
        cmd.Parameters.AddWithValue("@TOTAL_FTE", TextBoxTCNC.Text);
        cmd.Parameters.AddWithValue("@FCFTUHC", TextBoxTNFUG.Text);
        cmd.Parameters.AddWithValue("@FCFTPBHC", TextBoxTNFG.Text);
        cmd.Parameters.AddWithValue("@FCPTUHC", TextBoxTNCPUG.Text);
        cmd.Parameters.AddWithValue("@FCPTPBHC", TextBoxTNCPG.Text);
        cmd.Parameters.AddWithValue("@NCHC", TextBoxTNNCC.Text);
        cmd.Parameters.AddWithValue("@FTEYR", lblYEAR.Text);
        cmd.Parameters.AddWithValue("@DATE", TextBoxDATE.Text);
        cmd.Parameters.AddWithValue("@TIME", lblTime.Text);

        try
        {
            cmd.ExecuteScalar();
            ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('You Have Successfully Submitted the Enrollment Profile');", true);
            con.Close();
            Response.Redirect("Gradrate.aspx");
            
        }
        catch (Exception er)
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('You Have Successfully Submitted the Enrollment Profile');", true);
            Response.Redirect("Gradrate.aspx");
        }


        TextBoxFTUG.Text = string.Empty;
        TextBoxFTG.Text = string.Empty;
        TextBoxTHUGDR.Text = string.Empty;
        TextBoxTHGDR.Text = string.Empty;
        TextBoxNCCDR.Text = string.Empty;
        TextBoxTCNC.Text = string.Empty;
        TextBoxTNFUG.Text = string.Empty;
        TextBoxTNFG.Text = string.Empty;
        TextBoxTNCPUG.Text = string.Empty;
        TextBoxTNCPG.Text = string.Empty;
        TextBoxTNNCC.Text = string.Empty;
        TextBoxTHUG.Text = string.Empty;
        TextBoxTHG.Text = string.Empty;
        TextBoxT1234.Text = string.Empty;
        TextBoxNCC.Text = string.Empty;
        TextBoxTHCAS.Text = string.Empty;


Here is the aspx code:

ASP.NET
<asp:RangeValidator ID="RangeValidatorLYTNFUG" runat="server" 
                    ControlToValidate="TextBoxTNFUG" CssClass="style40" 
                    ErrorMessage="Number is Higher/Lower than 20%" ForeColor="Red" Type="Integer"></asp:RangeValidator>


It is like this will all the range validators.

Code Behind for the RangeValidation:

C#
protected void Page_Load (object sender, EventArgs e)

{
RangeValidatorLYTNFUG.MinimumValue = Convert.ToString(Convert.ToInt32(TextBoxLYTNFUG.Text) - Convert.ToInt32(TextBoxLYTNFUG.Text) * 20 / 100);
        RangeValidatorLYTNFUG.MaximumValue = Convert.ToString(Convert.ToInt32(TextBoxLYTNFUG.Text) + Convert.ToInt32(TextBoxLYTNFUG.Text) * 20 / 100);

        RangeValidatorLYTNFG.MinimumValue = Convert.ToString(Convert.ToInt32(TextBoxLYTNFG.Text) - Convert.ToInt32(TextBoxLYTNFG.Text) * 20 / 100);
        RangeValidatorLYTNFG.MaximumValue = Convert.ToString(Convert.ToInt32(TextBoxLYTNFG.Text) + Convert.ToInt32(TextBoxLYTNFG.Text) * 20 / 100);

        RangeValidatorLYTNCPUG.MinimumValue = Convert.ToString(Convert.ToInt32(TextBoxLYTNCPUG.Text) - Convert.ToInt32(TextBoxLYTNCPUG.Text) * 20 / 100);
        RangeValidatorLYTNCPUG.MaximumValue = Convert.ToString(Convert.ToInt32(TextBoxLYTNCPUG.Text) + Convert.ToInt32(TextBoxLYTNCPUG.Text) * 20 / 100);

        RangeValidatorLYTNCPG.MinimumValue = Convert.ToString(Convert.ToInt32(TextBoxLYTNCPG.Text) - Convert.ToInt32(TextBoxLYTNCPG.Text) * 20 / 100);
        RangeValidatorLYTNCPG.MaximumValue = Convert.ToString(Convert.ToInt32(TextBoxLYTNCPG.Text) + Convert.ToInt32(TextBoxLYTNCPG.Text) * 20 / 100);

        RangeValidatorLYTNNCC.MinimumValue = Convert.ToString(Convert.ToInt32(TextBoxLYTNNCC.Text) - Convert.ToInt32(TextBoxLYTNNCC.Text) * 20 / 100);
        RangeValidatorLYTNNCC.MaximumValue = Convert.ToString(Convert.ToInt32(TextBoxLYTNNCC.Text) + Convert.ToInt32(TextBoxLYTNNCC.Text) * 20 / 100);
}


Here is the Textbox_TextChange code:

C#
protected void TextBoxTNNCC_TextChanged(object sender, EventArgs e)
    {
        int a = Convert.ToInt32(TextBoxTNFUG.Text);
        int b = Convert.ToInt32(TextBoxTNFG.Text);
        int c = Convert.ToInt32(TextBoxTNCPUG.Text);
        int d = Convert.ToInt32(TextBoxTNCPG.Text);
        int f = Convert.ToInt32(TextBoxTNNCC.Text);
        TextBoxTHCAS.Text = Convert.ToString(a + b + c + d + f);
Posted
Updated 4-Dec-13 2:43am
v5
Comments
Sergey Alexandrovich Kryukov 3-Dec-13 11:44am    
As far as I can understand, validation information only exists on the client side. It does not persist when the page is loaded or reloaded. You can do different things: store most recent validation information in session variables, post them to the server side, prevent the posting at all (if validation fails), something like that. Hard to say more exactly from the first glance; it also depends on how you want it...
—SA
Computer Wiz99 3-Dec-13 11:49am    
Ok. In this case the validation for the range fires the way I want it to but when the user clicks on submit button the validation clears and the data gets saved into the database. I would like to have the validation to stay firing when the user clicks on submit and the data is saved then display the successful message then redirect to the next page.
Sergey Alexandrovich Kryukov 3-Dec-13 12:03pm    
I just don't know how your validation works. If it is called in form's onsubmit, and the handlers returns false, there is no form submission. This is what I would advise to use. You can check it up if you load the page with the form in a browser and look at it using "View page source"...
—SA
Computer Wiz99 3-Dec-13 12:07pm    
I have updated the code to show what I am using for the validation on the range. I have put the .aspx code and behind code.
Computer Wiz99 3-Dec-13 11:53am    
Now what happens is the rangevalidation stops firing once the user tabs or clicks to the last textbox. How can I stop this?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900