C# 使用“确定/取消”确认对话框

C# 使用“确定/取消”确认对话框,c#,asp.net,webforms,C#,Asp.net,Webforms,我正在使用WebForms GridView控件。当用户在一行上单击“更新”时,我想对照其他记录检查他们输入的值 因此,如果我返回true,我希望显示一个确认对话框,询问用户是否希望继续更新 javascript确认对话框可能不起作用,因为我并不总是想显示此对话框,只有当输入的值满足特定条件时才显示 有什么建议吗?如果您使用的是WebForms(?),则需要向GridView模板添加一些验证控件 下面是一个使用比较器的示例: <EditItemTemplate> <as

我正在使用WebForms GridView控件。当用户在一行上单击“更新”时,我想对照其他记录检查他们输入的值

因此,如果我返回true,我希望显示一个确认对话框,询问用户是否希望继续更新

javascript确认对话框可能不起作用,因为我并不总是想显示此对话框,只有当输入的值满足特定条件时才显示

有什么建议吗?

如果您使用的是WebForms(?),则需要向GridView模板添加一些验证控件

下面是一个使用
比较器的示例:

<EditItemTemplate>
    <asp:TextBox ID="EditUnitPrice" runat="server"
      Text='<%# Bind("UnitPrice", "{0:c}") %>'
      Columns="6"></asp:TextBox>
    <asp:CompareValidator ID="CompareValidator1" runat="server"
        ControlToValidate="EditUnitPrice"
        ErrorMessage="The price must be greater than or equal to zero and
                       cannot include the currency symbol"
        Operator="GreaterThanEqual" Type="Currency"
        ValueToCompare="0">*</asp:CompareValidator>
</EditItemTemplate>

*

我建议使用
RowDataBound
事件检查这些条件,并在需要时添加确认对话框

编辑:比较日期,如果日期不同,则显示确认信息

看这个演示

<script type="text/javascript">        
   validateInput = function(inputDate, compareDate, confirmButtonID) {           
       var confirmButton = document.getElementById(confirmButtonID);       
       if (confirmButton) {               
           $(confirmButton).one("click", function() {
               var result = dates.compare(inputDate, compareDate);
               if (result != 0){ //change to suit your needs
                   return confirm("Are you sure you want to save these changes?");        
               }
               return true;
           });                                                                                 
       }
    }             
</script>
GridView
RowDataBound
事件中,为每一行分配
onchange
函数:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    var inputCtrl = e.Row.FindControl("txtEnteredDate") as TextBox;
    if (inputCtrl != null)
    {
        var updateButtonCtrl = e.Row.FindControl("btnUpdate") as Button;
        if (updateButtonCtrl != null)
        {
            inputCtrl.Attributes["onchange"] = string.Format("return validateInput(this.value, '{0}', '{1}');", DataBinder.Eval("DateToCompare"), updateButtonCtrl.ClientID);
        }
    }
}    
jQuery确认对话框


如果您需要比常规JavaScript确认对话框更灵活的对话框,可以将上面的更改改为使用作为确认

我正在使用WebForms,我的验证需要查询数据库。此事件不起作用,因为我不知道用户要更改为哪个值,直到他们单击“编辑”链接,更新该值,然后单击“更新”,以便根据用户在字段中输入的内容显示确认?例如,如果用户更改文本框中的值,则显示确认信息?是。。我有一个带有日历扩展器的文本框。如果用户将日期更改为与另一条记录重叠的日期,则在用户确认此更改将导致记录被删除之前,我不想提交更改。@MichaelWarren:编辑了我的答案,并提供了一个比较日期和添加条件确认的解决方案。你是什么意思?如果用户单击“确定”,对话框将返回true,否则返回false。上面的代码只是返回确认值,假设当他们单击“确定”时,您希望执行回发并更新记录。
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    var inputCtrl = e.Row.FindControl("txtEnteredDate") as TextBox;
    if (inputCtrl != null)
    {
        var updateButtonCtrl = e.Row.FindControl("btnUpdate") as Button;
        if (updateButtonCtrl != null)
        {
            inputCtrl.Attributes["onchange"] = string.Format("return validateInput(this.value, '{0}', '{1}');", DataBinder.Eval("DateToCompare"), updateButtonCtrl.ClientID);
        }
    }
}