简体   繁体   English

ASP.NET C#用JS确认对话框插入记录前无法检查条件的原因为何?

[英]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. 在最终将记录插入数据库之前,我想用C#按钮单击操作,建议用户使用带有确认对话框的javascript函数填充一些重要的文本框。 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函数也不会启动。 JS function starts only if there si return; JS函数仅在返回 si时启动 in the end of C# JS call. 在C#JS调用的末尾。 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. 如果您想继续在服务器端检查此逻辑,那么我想最简单的方法是仅在不满足条件的情况下执行SQL逻辑。 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. 这是几乎相同的逻辑,但else有指示,如果SQL代码只能被执行if条件不满足

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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