Show "Are you sure?" Message Before clearing a textbox of data?

Simflex 301 Reputation points
2024-01-04T23:02:02.84+00:00

The following code disables a checkbox called credChk when there is data in two textboxes control ID txtcreditorname and control ID txtcreditoraddress respectively.

Here is the code:

            <asp:TemplateField HeaderText="Name">
            <headerstyle horizontalalign="Left" />
                <ItemTemplate>
                    <asp:TextBox ID="txtcreditorname" Text='<%# Eval("creditorname") %>' placeholder="Name of creditor..." runat="server" style="width:375px;" class="form-control" AutoPostBack="true" OnTextChanged="txtcreditorname_TextChanged"></asp:TextBox><br />
            <asp:CheckBox ID="credChk" runat="server" Checked="false" AutoPostBack="true" OnCheckedChanged="CredCheckChanged" /><span style="color:#ff0000">*Check this box if N/A</span>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Address">
            <ItemStyle HorizontalAlign="Left"></ItemStyle>
                <ItemTemplate>
                    <asp:TextBox ID="txtcreditoraddress" Text='<%# Eval("creditoraddress") %>' placeholder="Address..." runat="server" style="width:375px;" class="form-control" AutoPostBack="true" OnTextChanged="txtcreditorname_TextChanged"></asp:TextBox><br /><br />
                </ItemTemplate>
            </asp:TemplateField>


    Protected Sub txtcreditorname_TextChanged(sender As Object, e As EventArgs)
        For Each row As GridViewRow In grvCred.Rows
            Dim credname As TextBox = TryCast(row.FindControl("txtcreditorname"), TextBox)
            Dim credaddress As TextBox = TryCast(row.FindControl("txtcreditoraddress"), TextBox)
            Dim credSelect As CheckBox = TryCast(row.FindControl("credChk"), CheckBox)
            If credname.Text.Length > 0 AndAlso credaddress.Text.Length > 0 Then credSelect.Enabled = False Else credSelect.Enabled = True
        Next
    End Sub

The following method disables these textboxes if the credChk checkbox is checked and enables them if the checkbox is unchecked.

If the textboxes have data, then when the checkbox is checked, the data in those two textboxes are cleared.

This works fine as intended and this is the requirement from the users of this app.

There is, however, one other requirement that we are having problem implementing.

If the checkbox is checked and there is data in those textboxes, before they are cleared, a message is to pop up warning the user that s/he is about to clear data in those textboxes and whether s/he wishes to continue.

If the user clicks OK or Yes on the popped message, then the textboxes are cleared.

If the user wishes to NOT clear the textboxes, then the user clicks CANCEL.

We are having issues integrating this to the code above.

Any ideas how to handle this?

Many thanks in advance.

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,590 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,778 questions
{count} votes

Accepted answer
  1. Lan Huang-MSFT 30,176 Reputation points Microsoft Vendor
    2024-01-05T02:53:06.05+00:00

    Hi @Simflex,

    The function of popping up a warning message can refer to the following code:

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
        <script>
            $(document).ready(function () {
                $('input[type=checkbox]').click(function () {
                    var areYouSure = confirm('Are you sure you want to clean the data?');
                    if (areYouSure) {
                        $(this).prop('checked', this.checked);
                    } else {
                        $(this).prop('checked', !this.checked);
                    }
                });
            });
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:GridView ID="grvCred" runat="server" AutoGenerateColumns="false">
                    <Columns>
                        <asp:TemplateField HeaderText="Name">
                            <HeaderStyle HorizontalAlign="Left" />
                            <ItemTemplate>
                                <asp:TextBox ID="txtcreditorname" Text='<%# Eval("creditorname") %>' placeholder="Name of creditor..." runat="server" Style="width: 375px;" class="form-control" AutoPostBack="true" OnTextChanged="txtcreditorname_TextChanged"></asp:TextBox><br />
                                <asp:CheckBox ID="credChk" runat="server" AutoPostBack="true" OnCheckedChanged="CredCheckChanged" /><span style="color: #ff0000">*Check this box if N/A</span>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Address">
                            <ItemStyle HorizontalAlign="Left"></ItemStyle>
                            <ItemTemplate>
                                <asp:TextBox ID="txtcreditoraddress" Text='<%# Eval("creditoraddress") %>' placeholder="Address..." runat="server" Style="width: 375px;" class="form-control" AutoPostBack="true" OnTextChanged="txtcreditorname_TextChanged"></asp:TextBox><br />
                                <br />
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
            </div>
        </form>
    </body>
    </html>   
    
     Protected Sub CredCheckChanged(ByVal sender As Object, ByVal args As EventArgs)
            Dim selRowIndex As Integer = (CType(((CType(sender, CheckBox)).Parent.Parent), GridViewRow)).RowIndex
            Dim cb As CheckBox = CType(grvCred.Rows(selRowIndex).FindControl("credChk"), CheckBox)
            Dim credname As TextBox = CType(grvCred.Rows(selRowIndex).FindControl("txtcreditorname"), TextBox)
            Dim credaddress As TextBox = CType(grvCred.Rows(selRowIndex).FindControl("txtcreditoraddress"), TextBox)
    
            If cb.Checked Then
                credname.Text = ""
                credaddress.Text = ""
                cb.Checked = False
            End If
        End Sub
    
        Protected Sub txtcreditorname_TextChanged(ByVal sender As Object, ByVal e As EventArgs)
            For Each row As GridViewRow In grvCred.Rows
                Dim credname As TextBox = TryCast(row.FindControl("txtcreditorname"), TextBox)
                Dim credaddress As TextBox = TryCast(row.FindControl("txtcreditoraddress"), TextBox)
                Dim credSelect As CheckBox = TryCast(row.FindControl("credChk"), CheckBox)
    
                If credname.Text.Length > 0 AndAlso credaddress.Text.Length > 0 Then
                    credSelect.Enabled = False
                Else
                    credSelect.Enabled = True
                End If
            Next
        End Sub
    

    5

    Best regards,

    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.