繁体   English   中英

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

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

我正在使用一个确认窗口,如果用户在弹出窗口中单击“确定”,它将向数据库添加新值。 此外,我必须在此之前验证具体标准。 我有按钮和文本框,文本框包含必填字段验证器。 因此,如果我单击按钮,此验证器将首先触发。

我将在此文本框中输入一个数字并按添加,它将从数据库中获取与此数字对应的名称值,如果找到名称,则应询问确认“是否要添加此名称?” 如果找不到名称,那么它应该只弹出一个提示“找不到名字”。 如果数字值小于6,那么它将显示另一个弹出窗口“数字无效”。 我已按照以下说明完成此操作。

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的

<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#

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);
        }
    }
}

这里发生的事情就是每当我在文本框中输入一个数字并单击按钮时,即使数字小于6位数,也会首先执行Confirm()函数,如果我输入6位数字并且在数据库中找不到该名称同样执行Confirm()函数。 如果我输入少于6位的数字,则首先出现确认框,然后出现“数字无效”的提示。 如何满足条件时,如何触发confirm()函数。 我只想在按钮按下事件进入if (//name found)条件时才触发confirm()函数。

编辑

我已从按钮中删除了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
}

我已经创建了一个seperte函数来添加数据。 我已添加ScriptManager以打开确认框并删除OnClientClick in按钮。 现在当我按下按钮时,只有满足所有条件时才会打开确认框。 但是当我在确认框中按OK时没有任何反应。 AddData函数未执行。

将您的确认功能更改为:

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;
} 

并改变

onClientClick=Confirm();

对此:

onClientClick= return Confirm()

避免提交6长度文本。

您的帖子后面必须有一个隐藏字段,表明它在第一篇文章后面或确认后:

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

并更改您的代码:

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);
    }
}
}

并将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