简体   繁体   English

单击按钮并在字段中更改文本时发出警报消息

[英]alert message on button click and text changes in the fields

I have one scenario to resolve. 我有一种情况要解决。 I have 2 text boxes for first name, last name and one button to save those fields. 我有2个用于输入名字,姓氏和一个按钮的文本框,用于保存这些字段。 On the page load, we're filling those text fields with some data from the database, When we changed the text in any of those text fields and click on "Save" button, we need to display a message/alert like 在页面加载时,我们用数据库中的一些数据填充这些文本字段,当我们更改这些文本字段中的任何文本并单击“保存”按钮时,我们需要显示一条消息/警告,例如

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

If user responses to message/alert with Yes, then only we should allow those changes to save. 如果用户对“消息/警报”的回答为“是”,则只有我们应允许保存这些更改。

Code Snippets: 代码段:

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

Behind: 背后:

we need to call this code behind only after user chooses to change the name 只有在用户选择更改名称后,我们才需要调用此代码

 Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click

If the text fields are same, we no need to display any acknowledgement message. 如果文本字段相同,则无需显示任何确认消息。 Can you please let me know how to do to this one.? 您能告诉我该怎么做吗?

This should do what you are looking for :) 这应该做您想要的:)

The JavaScript is formatted so you can easily read it. JavaScript已格式化,因此您可以轻松阅读它。 You would typically put the JavaScript on 1 line or you could create a function and just call the function. 您通常会将JavaScript放在一行上,也可以创建一个函数并仅调用该函数。

Also look up the confirm() JavaScript you can give your pop up a proper title and their are other options. 还要查找confirm()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>

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

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