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: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?
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: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:
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:
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);