Home > OS >  How to write a conditional that alerts a user if a text box is changed a second time
How to write a conditional that alerts a user if a text box is changed a second time

Time:05-20

What I am trying to do is create a conditional statement that will alert the user if a text box is changed after initially filling that field.

For instance if they enter their name into the text box and then at a later point enter a different name into that text box, I want it to alert the user.

Below is my code of both the text box control as well as a .textchanged event.

Text Box Control:

<div >
    <asp:TextBox ID="txtSearchOC" runat="server" Width="250px" Text="" AutoPostBack="True" TabIndex="13" Font-Bold="True"
        ForeColor="#822402" BorderColor="#822402" AutoCompleteType="Disabled" ClientIDMode="Static" />
</div>

.textChanged Event Code:

Protected Sub txtSearchOC_TextChanged(sender As Object, e As EventArgs) Handles txtSearchOC.TextChanged
    Dim searchOCText As String = txtSearchOC.Text
    Dim isTheSame As Boolean = False
    If searchOCText = txtSearchOC.Text Then
        isTheSame = True
    End If
    If isTheSame = True AndAlso searchOCText <> txtSearchOC.Text Then
        Call txtSearchOC_Warning()
    End If

End Sub

My thought process was to store the first name in a variable called "searchOCText" and then have a boolean that would hold true if searchOCText = txtSearchOC.Text, I would then use that boolean value to test if the text in the text box had changed, but I'm not terribly sure where to go from here as I am pretty new to programming.

So far I have tested the procedure "txtSearchOC_Warning()" and on its own it works and displays the message I am wanting to display. However when I try to call it from the TexChanged procedure nothing happens and I am at a loss as to why. So I am left to believe that the problem lies within my conditional and that I need to start digging there.

Any and all help would be greatly appreciated.

CodePudding user response:

It's better to use JS to perform this validation since it's more efficient and doesn't require a PostBack to trigger. Check the following simple script:

<script>
let oldVal=document.getElementById("<%=txtSearchOC.ClientID%>").value;
function checkChange(val) {
if(oldVal!="")//To check if this change is a first time change
{
  alert("The input value has changed to: "   val);
}
 oldVal=val;
}
</script>

And your Text Box definition will be:

<asp:TextBox ID="txtSearchOC" runat="server"
onClientClick="checkChange(this.value)" AutoPostBack="false" ...

The problem with your approach is that searchOCText will be always equal to txtSearchOC after the PostBack. Line Dim searchOCText As String = txtSearchOC.Text

If you still want to use this approach you may store searchOCText in a Session like this:

Try
If Session("searchOCText").ToString()= txtSearchOC.Text Then
   isTheSame = True
End If
Catch 
End Try
Session("searchOCText")=txtSearchOC.Text

CodePudding user response:

Looks like your code has some small mistakes:

TextChanged raised on programmatically setting value and any typing, every stroke. Better to use the LostFocus event.

Variable isTheSame is redundant (always True) because the event raises after text changing.

Actually, your message will never be shown when changing the user contents or not, because you compare user input with the same user input (inequality searchOCText <> txtSearchOC.Text always be False).

I think it is the simplest solution.

Public Class ExtTextBox
    Inherits TextBox
    Private LastUserInput As String
    Protected Sub ExtTextBox_LostFocus(sender As Object, e As EventArgs) Handles Me.LostFocus
        Select Case True
            Case LastUserInput Is Nothing
                LastUserInput = Text
            Case Text <> LastUserInput
                If MsgBox("Apply change?", MsgBoxStyle.YesNo Or MsgBoxStyle.Question) = MsgBoxResult.Yes Then
                    LastUserInput = Text
                Else
                    Text = LastUserInput
                End If
        End Select
    End Sub
End Class

Variable LastUserInput (initially Nothing) stores user input.

The first branch of Select Case stores the first user input in LastUserInput.

The second branch updates stored input or restored text according to user decision where the user points to any other element after any edit excluding the first one. You can put any code into the second branch to process new input.

  • Related