使用ASP.NET中的确认窗口弹出窗口和RequiredFieldValidator

时间:2017-01-03 04:10:40

标签: javascript c# jquery asp.net

我使用以下代码通过确认从数据库中删除记录。

ASP.NET

<asp:TextBox ID="text_delete" Width="70%" Height="60px" ValidationGroup="del" CssClass="textbox" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="required_desc" CssClass="validator" ValidationGroup="del" ControlToValidate="text_delete" ErrorMessage="Reason Required" runat="server">Reason Required</asp:RequiredFieldValidator>
<asp:Button ID="button_delete" CssClass="button" ValidationGroup="del" Text="Delete" runat="server" OnClientClick="Confirm()" OnClick="button_delete_Click" />

的JavaScript

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

C#

protected void button_delete_Click(object sender, EventArgs e)
{
    string confirmValue = Request.Form["confirm_value"];
    if (confirmValue == "Yes")
    {
        // Delete the data
    }
    if (confirmValue == "No")
    {
        // Do not delete data
    }
}

但是我希望用户首先在文本框中输入删除的原因,然后才会出现确认弹出窗口。即使我使用了必需的字段验证器,验证器也只能在弹出窗口之后触发。如何首先触发验证器,并且只有在输入原因时才会出现删除确认的弹出窗口。我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

如果您想要更具体,可以使用Page_ClientValidate()或Page_ClientValidate(“组名”)在脚本中调用必需的字段验证。

if(Page_ClientValidate()){
     ....popup confirmation code
     ....
}  

Working RequiredFieldValidator along with javascript