在按钮上单击警告消息,并在字段中更改文本

alert message on button click and text changes in the fields

本文关键字:字段 文本 按钮 单击 警告 消息      更新时间:2023-09-26

我有一个场景需要解决。我有2个文本框的名字,姓氏和一个按钮来保存这些字段。在页面加载时,我们正在填充当我们改变这些文本字段中的文本并点击"保存"按钮时,我们需要显示一个消息/警报,如

"You're about change the name of the person, john, smith to Daniel, clark". Do you wanna proceed? 

如果用户对消息/警报的响应是Yes,那么只有我们应该允许保存这些更改。

代码片段:

<asp:TextBox ID="txtfirstName"  runat="server" required="true" caption="First Name:" ></asp:TextBox>                           
               <asp:TextBox ID="txtlastName"  runat="server" caption="Last Name:"></asp:TextBox>
 <asp:Button ID="btnSave" runat="server"  Text="Save" CausesValidation="False"  />&nbsp;
背后:

只有当用户选择更改 名称时,我们才需要在后台调用该代码。
 Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click

如果文本字段相同,则不需要显示任何确认消息。你能告诉我怎么处理这个吗?

这应该是你正在寻找的:)

JavaScript是格式化的,所以你可以很容易地阅读它。你通常会把JavaScript放在一行,或者你可以创建一个函数,然后调用这个函数。

也查找确认()JavaScript,你可以给你的弹出框一个适当的标题和他们的其他选项。

<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:HiddenField ID="hdnfirstName" runat="server" Value="name" />
        <asp:HiddenField ID="hdnlastName" runat="server" Value="last" />
        <asp:TextBox ID="txtfirstName" runat="server"></asp:TextBox>
        <asp:TextBox ID="txtlastName" runat="server"></asp:TextBox>
        <asp:Button ID="btnSave" runat="server" Text="Save" runat="server" 
        OnClientClick="if(document.getElementById('hdnfirstName').value !== document.getElementById('txtfirstName').value || 
                      document.getElementById('hdnlastName').value !== document.getElementById('txtlastName').value){
                            if (confirm('You''re about change the name of the ' + 
                                         document.getElementById('hdnlastName').value + ', ' + 
                                         document.getElementById('hdnfirstName').value + ' to ' + 
                                         document.getElementById('txtlastName').value + ', ' + 
                                         document.getElementById('txtfirstName').value + 
                                         '. Do you wanna proceed?')) 
                            {
                                return true; 
                            } else { 
                                return false; 
                            }
                      } else {
                        return true;
                      }" />
    </div>
    </form>
</body>
</html>