简体   繁体   中英

Asp.net C# check conditions with JS confirm dialog before insert record not fire why?

I have several textboxes for user input and button to save record. Before finally insert records to DB, in C# button click action I'd like to suggest user to fill some important textboxes using javascript function with confirm dialog. If user choose yes code should break and focus textbox. If not, code should continue and insert record. That's my goal. But, JS function is not starting even condition is true. JS function starts only if there si return; in the end of C# JS call. Here is my code:

    protected void savenew_Click(object sender, EventArgs e)
        {            if (string.IsNullOrEmpty(arr.Text)) { ScriptManager.RegisterStartupScript(this, GetType(), "alertMessage", "alert('Missing arr..');", true); arr.Focus(); return; }


// HERE IS PROBLEM: 

            if (string.IsNullOrEmpty(telnr.Text))
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "CallMyFunction", "corectyesno()", true);  return;
            }

  try
            {

                SqlCommand MyCommand = new SqlCommand( etc...



//and JS function:
         function corectyesno()
            {
                if (confirm('Do you want to proceed ?')) {
                    return true;
                }
                else {
                    return false;
                }
            }

My language is not english so please excuse me for my grammar. Next, programming is my hobby so... Thanks to everyone who can help me and suggest better approach.

If you want to continue to check this logic server-side then I imagine the simplest approach would be to only perform your SQL logic if the condition is not met. Perhaps something as simple as:

if (string.IsNullOrEmpty(telnr.Text))
{
    Page.ClientScript.RegisterStartupScript(this.GetType(), "CallMyFunction", "corectyesno()", true);  return;
}
else
{
    try
    {
        // your SQL code...
    }
    // etc.
}

It's almost the same as your logic, but the else is there to indicate that the SQL code should only be executed if the if condition is not met.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM