Click here to Skip to main content
15,893,814 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a form that has validations on it. When a certain validation fires I would like to still submit the form. I have a code in place for this to happen but I think I have it wrong. I have an aspx code and a behind code. The message code will not fire and the page will not redirect. Which one will I not use and still get what I need to happen?

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


The C# code:

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 Table2014 (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);

        cmd.ExecuteNonQuery();
        con.Close();

        if (Page.IsValid)
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('You Have Successfully Submitted the Enrollment Profile');", true);
        }
        else
        {
            Response.Redirect("Gradrate.aspx");
        }
Posted
Comments
♥…ЯҠ…♥ 2-Dec-13 23:04pm    
Then have validation for field as per your requirement and remove validation for optional fields...
Computer Wiz99 2-Dec-13 23:14pm    
Well, the validation that fire is the rangevalidation. When it fires I still want to submit the form, display the successful message and redirect to the next page. Only thing I have done is to submit the form once but I changed the settings a few times so I don't know which one works.
Computer Wiz99 2-Dec-13 23:50pm    
Ok, I can set the <asp:Button ID="ButtonSubmit" runat="server" CausesValidation="False" onclick="ButtonSubmit_Click"
Text="Submit" Width="75px" />.
It submits but there is a problem. I get this error, "Page.IsValid cannot be called before validation has taken place. It should be queried in the event handler for a control that has CausesValidation=True and initiated the postback, or after a call to Page.Validate.". How can I change this to where the successful message fires and redirects to the next page?

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