Detecting Control Changes

  • Thread starter Thread starter Michael Albanese
  • Start date Start date
M

Michael Albanese

I have an .aspx page which retrieves a record from the
database and populates each field in a ASP:Textbox. I
allow the users to change the contents of individual text
boxes and they click on 'Save' button to save their
changes. Quite often users do not click 'Save' and go to
some other page. How can I programmatically detect that
they have changed one or more text boxes and prompt them
to click on 'Save' button?

Thanks,

Michael
 
Hi Michael,

I think you may need try and catch the TextChanged event of textbox and
maintain a viewstate variable ot indicate if the text has changed.

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'populate the textbox
If Not IsPostBack Then
TextBox1.Text = "hello"
TextBox2.Text = "world"
viewstate("change") = False
'initialize the st
End If
End Sub

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged
viewstate("change") = True
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
If viewstate("change") Then
Dim scriptString As String = "<script language=JavaScript>
alert('Please save first')"
scriptString += "</script>"
If (Not Me.IsStartupScriptRegistered("Startup")) Then
Me.RegisterStartupScript("Startup", scriptString)
End If
Else
Response.Redirect("http://www.google.com")
End If
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
'Do save issue
viewstate("change") = False
End Sub

If you have any concern please post here.

Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Back
Top