-1

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.

1
  • Thanks. OK basicly i underestand your logic. Have you idea how should I check if user filled or not telnr textbox before inserting ? Commented Jan 8, 2019 at 13:27

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Yes but point is that user will decide in confirm dialog should inserting continue or should be stopped to enter telnr... And in the end of JS call is return; which stopped execution. I tried this many times...How to cotinue inserting without entering telnr ? Because I just wont to suggest, enter telnr if you want if not ok continue with inserting without tlenr.
@jastog: In your JavaScript confirmation logic you could potentially re-submit the form. See this: stackoverflow.com/questions/3242365/… Or perhaps you could perform the check entirely client-side in the first place, which would remove this check from your server-side code entirely and use JavaScript to intercept the form submit event, check the input value, and either cancel or submit the form accordingly. If you're relatively new to JavaScript that could be a learning curve, but worth the effort.
@jastog: Another alternative, if you want to keep this server-side and not do much JavaScript work, would be to use a custom form instead of a JavaScript confirmation dialog. It would still take some work, but you could use your server-side logic to conditionally display a panel of controls which ask for confirmation and have their own submit button, and that button performs the SQL logic.
Thank you David, I'll try your suggestion checking on client side...kind regards.
As I googled a lot and I noticed many people meet this problem. Finally I used ajaxToolkit:ModalPopupExtender . Solution for me (Thnx to Ron) I found here: stackoverflow.com/questions/26174452/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.