ASP.net button clears cell colors before they are detected

Murray Brown
Murray Brown used Ask the Experts™
on
Hi

I have an ASP.net button that runs code that checks the cell backcolors.
The problem is that none of the cell colors are detected as blue because they were cleared on a postback before
the code runs. How do I run the VB.net code before the colors are cleared?
    Protected Sub btnSaveChanges_Click(sender As Object, e As EventArgs) Handles btnSaveChanges.Click

        Try

            Me.lblSaveChangesError.Text = ""
            Dim oDateList As String = TryCast(Session("oDateList"), String)
            Dim arrDates As Object = Split(oDateList, ",")
            Dim oNumberDays As Integer = UBound(arrDates) + 1
            Dim oTextColumnName As String
            Dim oColumnIndex As Integer
            Dim oCellValue As String
            Dim oToolTip As String
            Dim T As TextBox
            Dim oBackColor As System.Drawing.Color

            For r As Integer = 0 To Me.GridView1.Rows.Count - 1
                For d As Integer = 1 To oNumberDays
                    oColumnIndex = d + 1 'First column index is 2
                    oBackColor = Me.GridView1.Rows(r).Cells(oColumnIndex).BackColor
                    If oBackColor = System.Drawing.Color.Blue Then
                        'so if cell ccolor is blue....
                        oTextColumnName = "D" & CStr(d)
                        T = Me.GridView1.Rows(r).FindControl(oTextColumnName)
                        oCellValue = T.Text
                        oToolTip = T.ToolTip

                    End If
                Next d
            Next r


        Catch ex As Exception
            Me.lblSaveChangesError.Text = ex.Message
        End Try
    End Sub

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Chief Technology Ninja
Distinguished Expert 2018
Commented:
Hi Murray,

In your page_load, you can write code like this:
If Not IsPostback Then
' Call the code that needs to run before your page is lost.
End If

Open in new window


You will have to take out that grid processing code and create a function which does this processing and call it from the Page_Load.
Another thing you can do is, EnableViewState for a given page/control to true that way it will preserve the values which were set by user interaction/client side scripts. Refer to: https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.control.enableviewstate?view=netframework-4.8

Regards,
Chinmay.
Murray BrownASP.net/VSTO Developer

Author

Commented:
ok..great
thanks very much

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial