Solved

ASP.NET : How to highlight differences between two form views with the same text boxes between them

Posted on 2014-10-04
7
441 Views
Last Modified: 2014-10-05
I have two FormViews on a web page:  One shows a row of data (fields, columns) from a database table, the other the same row of data.  The difference between them is that one of the FormViews show one or more fields that a user changed (Updates to the record).  I want to highlight in one or both of the FormViews, which pair(s) of the same fields are different between them.

Example:

FormView1.................................................FormView2
Name: Joe                                                Name: Joe
Phone Num: 800-999-9999                  Phone Num: 888-999-9999
etc.                                                            etc.

I would like to highlight the phone number because it's been updated.  The real FormViews will have many fields so it's not easy for someone looking at the two FormViews to spot any changes unlike in the trivial example above.

How to approach this problem or some code (VB or C# is ok) would be appreciated. Thanks!
0
Comment
Question by:Brad Aberg
  • 4
  • 3
7 Comments
 
LVL 34

Expert Comment

by:Mike Eghtebas
ID: 40361768
Recently, I used a similar concept in my application. This was for a WinForm but I am sure it could be adapted for use in asp.net (vb.net), declare:    

'To  initialize with the 2nd column textbox control names and their matching value pairs (in not postback)  
Dim dataPair As New Dictionary(Of String, String)  

'To  initialize with the 2nd column textbox control names and True (in not postback)                      
Dim boldataPairMatching As New Dictionary(Of String, Boolean)  

Form this point on, you caan revise the following winform code to suit your needs (in postback):
   Private Sub TestRevised(sender As Object, e As EventArgs) Handles FirstNameTextBox.TextChanged, LastNameTextBox.TextChanged, AddressTextBox.TextChanged, _
         CityTextBox.TextChanged, StateTextBox.TextChanged, ZipCodeTextBox.TextChanged, PhoneTextBox.TextChanged, EmailTextBox.TextChanged

        Try

            ' find if any of the text boxes has be modified
            ' to enable/disable btnSave accordingly
            Dim bolSaveYN As Boolean = False
            Dim txt As TextBox = TryCast(sender, TextBox)

            If bolSaveNew Then Exit Sub

            If bolChangeText = True Then
                If dataPair.Item(txt.Name) = txt.Text Then 'same as old value
                    boldataPairMatching(txt.Name) = True
                Else
                    boldataPairMatching(txt.Name) = False
                End If
            End If

            For Each pair In boldataPairMatching
                If pair.Value = False Then
                    bolSaveYN = True
                End If
                ' MessageBox.Show(intEdits & ", " & dataPair.Item(txt.Name) & ", " & txt.Text)
            Next

            Me.btnSave.Enabled = bolSaveYN

        Catch ex As Exception

            MessageBox.Show(ex.Message)

        End Try

    End Sub

Open in new window

Mike
0
 
LVL 34

Accepted Solution

by:
Mike Eghtebas earned 500 total points
ID: 40361774
Below, I tried to modify to suit your need. But it needs more massaging:
  
        Try
            ' somehow, store text boxt control just edited in txt
            Dim txt As TextBox = TryCast(sender, TextBox)

            ' compare to see the value is the same
            If dataPair.Item(txt.Name) = txt.Text Then 
                boldataPairMatching(txt.Name) = True    'same for txt
            Else
                boldataPairMatching(txt.Name) = False   'not the same for txt
            End If

            ' loop thru
            For Each pair In boldataPairMatching
                If pair.Value = False Then
                   'remove the red border
                Else
                   'make border re
                End If
            Next
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

BTW, this is how I initialized my dictionary variables:
       Try
            ' first time FirstNameTexBox gets focuses dataPair gets intilized and
            ' bolLoadDictionaryYN gets set to false to prevent updating dataPair dictionary
            If bolLoadDictionaryYN Then
                dataPair.Add("FirstNameTextBox", Me.FirstNameTextBox.Text)
                dataPair.Add("LastNameTextBox", Me.LastNameTextBox.Text)
                dataPair.Add("AddressTextBox", Me.AddressTextBox.Text)
                dataPair.Add("CityTextBox", Me.CityTextBox.Text)
                dataPair.Add("StateTextBox", Me.StateTextBox.Text)
                dataPair.Add("ZipCodeTextBox", Me.ZipCodeTextBox.Text)
                dataPair.Add("PhoneTextBox", Me.PhoneTextBox.Text)
                dataPair.Add("EmailTextBox", Me.EmailTextBox.Text) 'boldataPair

                ' with intial bolChangeText = false to prevent text box change event  
                ' firing when the form gets first uploads 
                boldataPairMatching.Add("FirstNameTextBox", True)
                boldataPairMatching.Add("LastNameTextBox", True)
                boldataPairMatching.Add("AddressTextBox", True)
                boldataPairMatching.Add("CityTextBox", True)
                boldataPairMatching.Add("StateTextBox", True)
                boldataPairMatching.Add("ZipCodeTextBox", True)
                boldataPairMatching.Add("PhoneTextBox", True)
                boldataPairMatching.Add("EmailTextBox", True)
            End If

Open in new window

0
 

Author Comment

by:Brad Aberg
ID: 40361810
This is great.  Thank you very much eghtebas (Mike).
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

Author Closing Comment

by:Brad Aberg
ID: 40361811
Thanks!  This was my first question on Experts-Exchange.  I appreciate your detailed answer.
0
 
LVL 34

Expert Comment

by:Mike Eghtebas
ID: 40361813
Do you mind sharing your solution? I am sure there are lots I could learn from you. In particular, I want to to know how you got control names? etc.

Thanks,

Mike
0
 

Author Comment

by:Brad Aberg
ID: 40362482
Mike,

My code is pretty simple and basic because I am a beginner at ASP.NET.  Here's a snippet showing comparison of one pair of textbox text contents.

Protected Sub SqlDataSource3_Load(sender As Object, e As EventArgs) Handles SqlDataSource3.Load
    Dim foo As TextBox = TryCast(FormView1.FindControl("LastNameTextBox"), TextBox)
    Dim bar As TextBox = TryCast(FormView2.FindControl("LastNameTextBox"), TextBox)
    If foo IsNot Nothing Then

      If bar IsNot Nothing Then

        If foo.Text <> bar.Text Then

          bar.BackColor = Drawing.Color.Yellow
          foo.BackColor = Drawing.Color.Yellow

        End If
      End If
End Sub

Open in new window


Tomorrow I am going to see if I can use your more sophisticated answer to improve my code.  The way my code is n ow, I'll have to include lines for every pair of textboxes in the formview (about 35 or so).  If I can use to loop through I will have a more elegant solution. Thanks again.
0
 
LVL 34

Expert Comment

by:Mike Eghtebas
ID: 40362715
Oh, I thought you already have it finalized. Yes looping through the text boxes is the way to go.

If you decided to post a new question on the text boxes, add a link here so I could post a solution.

Mike
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question