简体   繁体   English

ASP.NET执行JavaScript仅在条件满足时才在C#代码中确认警报消息

[英]ASP.NET Executing JavaScript Confirm alert message in C# code behind only if condition is satisfied

I am using a confirmation window that will add a new value to database if the user clicks OK in the popup window. 我正在使用一个确认窗口,如果用户在弹出窗口中单击“确定”,它将向数据库添加新值。 Also i have to verify a specific criteria before that. 此外,我必须在此之前验证具体标准。 I have button and text box and the text box contains a required field validator. 我有按钮和文本框,文本框包含必填字段验证器。 So if i click the button this validator fires first. 因此,如果我单击按钮,此验证器将首先触发。

I will enter a number in this text box and press add, it will fetch a name value corresponding to this number from database and if name is found then it should ask a confirmation "Do you want to add this name?" 我将在此文本框中输入一个数字并按添加,它将从数据库中获取与此数字对应的名称值,如果找到名称,则应询问确认“是否要添加此名称?” and if name is not found then it should just popup a alert saying "name not found". 如果找不到名称,那么它应该只弹出一个提示“找不到名字”。 If number value is less than 6 then it will show another popup saying "number not valid". 如果数字值小于6,那么它将显示另一个弹出窗口“数字无效”。 I have done this as given below. 我已按照以下说明完成此操作。

ASP.NET ASP.NET

<asp:TextBox ID="text_add" runat="server" MaxLength="6"></asp:TextBox>
<asp:RequiredFieldValidator ID="required_add_" ControlToValidate="text_add" ErrorMessage="Required" runat="server">Required</asp:RequiredFieldValidator>
<asp:Button ID="button_add" runat="server" Text="Add" OnClientClick="Confirm()" OnClick="button_add_Click" />

JavaScript JavaScript的

<script type = "text/javascript">
    function Confirm() {
        if (Page_ClientValidate()) {
            var confirm_value = document.createElement("INPUT");
            confirm_value.type = "hidden";
            confirm_value.name = "confirm_value";
            if (confirm("Do you confirm?")) {
                confirm_value.value = "Yes";
            } else {
                confirm_value.value = "No";
            }
            document.forms[0].appendChild(confirm_value);
        }
    }
</script>

C# C#

protected void button_add_Click(object sender, EventArgs e)
{
    if (text_add.Text.Length < 6)
    {
        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Number not valid!')", true);
    }
    else
    {
        //fetch name from DB
        if (//name found)
        {
            string confirmValue = Request.Form["confirm_value"];
            if (confirmValue == "Yes")
            {
                //add the name
            }
        }
        else
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Name not found!')", true);
        }
    }
}

Here what happens is whenever i enter a number into text box and click the button, the Confirm() function is executed at first even if the number is less than 6 digits and in case i enter 6 digits and the name is not found in database same way the Confirm() function is executed. 这里发生的事情就是每当我在文本框中输入一个数字并单击按钮时,即使数字小于6位数,也会首先执行Confirm()函数,如果我输入6位数字并且在数据库中找不到该名称同样执行Confirm()函数。 If i enter number less than 6 digits the confirm box comes first and after that the alert saying "number not valid" comes. 如果我输入少于6位的数字,则首先出现确认框,然后出现“数字无效”的提示。 How can i fire the confirm() function only if the conditions are met. 如何满足条件时,如何触发confirm()函数。 I want to fire the confirm() function only if the button press event goes into the if (//name found) condition. 我只想在按钮按下事件进入if (//name found)条件时才触发confirm()函数。

EDIT 编辑

I have removed the OnClientClick from the button and then changed the C# code to the following 我已从按钮中删除了OnClientClick ,然后将C#代码更改为以下内容

protected void button_add_Click(object sender, EventArgs e)
{
    if (text_add.Text.Length < 6)
    {
        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Number not valid!')", true);
    }
    else
    {
        //fetch name from DB
        if (//name found)
        {
            ScriptManager.RegisterStartupScript(this, typeof(string), "confirm", "Confirm();", true);
            string confirmValue = Request.Form["confirm_value"];
            if (confirmValue == "Yes")
            {
                this.AddData(sender, e);
            }
        }
        else
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Name not found!')", true);
        }
    }
}

protected void AddData(object sender, EventArgs e)
{
    // add data
}

I have made a seperte function to add data. 我已经创建了一个seperte函数来添加数据。 I have added the ScriptManager to open confirm box and removed the OnClientClick in button. 我已添加ScriptManager以打开确认框并删除OnClientClick in按钮。 Now when i press the button the confirm box opens only if all conditions are satisfied. 现在当我按下按钮时,只有满足所有条件时才会打开确认框。 But when i press OK in confirm box nothing happens. 但是当我在确认框中按OK时没有任何反应。 The AddData function is not executed. AddData函数未执行。

Change your confirm function to this: 将您的确认功能更改为:

function Confirm() {
    if (Page_ClientValidate() && 
  document.getElementById('text_add').value.length >5) {
        var confirm_value = document.createElement("INPUT");
        confirm_value.type = "hidden";
        confirm_value.name = "confirm_value";
        if (confirm("Do you confirm?")) {
            confirm_value.value = "Yes";
        } else {
            confirm_value.value = "No";
        }
        document.forms[0].appendChild(confirm_value);
    }
   else return false;
} 

And change The 并改变

onClientClick=Confirm();

to this: 对此:

onClientClick= return Confirm()

to avoid submit under 6 length text. 避免提交6长度文本。

You must have a hidden field between your post backs that shows it is in first post back or after confirmation: 您的帖子后面必须有一个隐藏字段,表明它在第一篇文章后面或确认后:

<asp:HiddenField ID="isReadyForSave" runat="server" Value="false"/>

And change your code: 并更改您的代码:

protected void button_add_Click(object sender, EventArgs e)
{
 if(isReadyForSave.Value == "true" && Request.Form["confirm_value"] == "yes")
 {
  AddData();
  isReadyForSave.Value = "false";
  return;
 }
if (text_add.Text.Length < 6)
{
    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Number not valid!')", true);
}
else
{
    //fetch name from DB
    if (//name found)
    {
        ScriptManager.RegisterStartupScript(this, typeof(string), "confirm", "Confirm();", true);


            isReadyForSave.Value = "true";

    }
    else
    {
        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Name not found!')", true);
    }
}
}

And change you javascript confirm() function to this: 并将javascript confirm()函数更改为:

function Confirm() {
if (Page_ClientValidate() && 
 document.getElementById('text_add').value.length >5) {
    var confirm_value = document.createElement("INPUT");
    confirm_value.type = "hidden";
    confirm_value.name = "confirm_value";
    if (confirm("Do you confirm?")) {
        confirm_value.value = "Yes";
    } else {
        confirm_value.value = "No";
    }
    document.forms[0].appendChild(confirm_value);
    return true;
}
else return false;
} 

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

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