Compare values of gridview txtboxes before insert

kdeutsch
kdeutsch used Ask the Experts™
on
I need to compare the old value of my textbox to the new value that was put into the textbox to see if anything changed else I don't want to save anything in the row and move to next row.  Here is my gridiview with bound txtBoxes and my initial code behind.

<asp:TemplateField HeaderText="ACTUAL">
                       <ItemTemplate>
                           <asp:TextBox ID="txtActual" runat="server" Text='<%# Eval("ACTUAL_VALUE") %>' Width="60" MaxLength="8"></asp:TextBox>
                       </ItemTemplate>
                   </asp:TemplateField>
                    <asp:TemplateField HeaderText="TARGET">
                       <ItemTemplate>
                           <asp:TextBox ID="txtTarget" runat="server" Text='<%# Eval("TARGET_VALUE") %>' Width="60" MaxLength="8"></asp:TextBox>
                       </ItemTemplate>
                   </asp:TemplateField>
                    <asp:TemplateField HeaderText="TOLERANCE">
                       <ItemTemplate>
                           <asp:TextBox ID="txtTolerance" runat="server" Text='<%# Eval("TOLERANCE_RAW") %>' Width="120" MaxLength="8"></asp:TextBox>
                       </ItemTemplate>
                   </asp:TemplateField>



I have a linkbutton to save it all. but I need to compare values so that I am not saving information that does not need to be saved becuase of the expensive long trip of the update process to an oracle server over a link from sql.

For Each dgi As GridViewRow In Gridmain.Rows
            Dim Actualval As String = CType(dgi.FindControl("txtActual"), TextBox).Text
            Dim Targetval As String = CType(dgi.FindControl("txtTarget"), TextBox).Text
            Dim Tolval As String = CType(dgi.FindControl("txtTolerance"), TextBox).Text
            Dim BMVal1 As String = CType(dgi.FindControl("txtBm1"), TextBox).Text
            Dim BMVal2 As String = CType(dgi.FindControl("txtBm2"), TextBox).Text
            Dim BMVal3 As String = CType(dgi.FindControl("txtBm3"), TextBox).Text
            Dim BMVal4 As String = CType(dgi.FindControl("txtBm4"), TextBox).Text
            Dim BMVal5 As String = CType(dgi.FindControl("txtBm5"), TextBox).Text
        Next
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Since you are saving all rows at once, you have a couple of options:
1. With a datasourceID: you can have a hiddenfield control in each item template which contains the old value that you can compare in your loop.
<asp:HiddenField ID="hdnActual" runat="server" Value='<%# Eval("ACTUAL_VALUE") %>' />

2. If you bind the datasource, then you have an option store the original table in a session variable, profile variable, or in the viewstate.   This adds a level of complexity and has pros/cons like your expensive trip to the oracle server.


Author

Commented:
CmdoProg2:
the information we pull is directly from Cognos, but to update it , the information goes to a different table until the calculations are run (Metrics Program).  So we don't pull any ID's that are specific to any row except for the date field with is the first field but a label.  So if I am understanding you , you are loading the same page with hideen fields with the same values. but how would this affect each row since I have 8 txtboxes per row and 12 rows.  Tha is a lot of hidden fields.
kdeutsch:
Yes, it is.  The gridview has in its rowupdating event arguement the old and new values; however, it is for a single row in edit mode.

       For Each dgi As GridViewRow In Gridmain.Rows
            Dim Actualval As String = CType(dgi.FindControl("txtActual"), TextBox).Text
            Dim ActualOrg as String = CType(dgi.FindControl("hdnActual"), HiddenField).Value
            Dim Targetval As String = CType(dgi.FindControl("txtTarget"), TextBox).Text
            Dim TargetOrg as String = CType(dgi.FindControl("hdnTarget"), HiddenField).Value
...
            IF Actualval <> ActualOrg and Targetval  <> TargetOrg THEN
               ' Do the update
            END IF
        Next
oops,
  Change the compare logic to:
            IF Actualval <> ActualOrg OrElse Targetval  <> TargetOrg THEN
               ' Do the update
            END IF

Author

Commented:
CmdoProg2:
Just to be aware I am not using the row updating event as everything resides in my code behind.  I am saving the griwdview via a linkbutton that sits below it as part of an update panel.  below is my full gridview.  and then the code I have posted is part of the linkbutton click event.
 <asp:GridView ID="Gridmain" runat="server" AutoGenerateColumns="false" AlternatingRowStyle-BackColor="LightGray" 
			        HeaderStyle-HorizontalAlign="Center" HeaderStyle-BackColor="Steelblue" CellSpacing="2" RowStyle-HorizontalAlign="Center">
			            <Columns>
			                <asp:BoundField DataField="Date" HeaderText="MONTH" />
			                <asp:TemplateField HeaderText="ACTUAL">
			                    <ItemTemplate>
			                        <asp:TextBox ID="txtActual" runat="server" Text='<%# Eval("ACTUAL_VALUE") %>' Width="60" MaxLength="8"></asp:TextBox>
			                    </ItemTemplate>
			                </asp:TemplateField>
			                 <asp:TemplateField HeaderText="TARGET">
			                    <ItemTemplate>
			                        <asp:TextBox ID="txtTarget" runat="server" Text='<%# Eval("TARGET_VALUE") %>' Width="60" MaxLength="8"></asp:TextBox>
			                    </ItemTemplate>
			                </asp:TemplateField>
			                 <asp:TemplateField HeaderText="TOLERANCE">
			                    <ItemTemplate>
			                        <asp:TextBox ID="txtTolerance" runat="server" Text='<%# Eval("TOLERANCE_RAW") %>' Width="120" MaxLength="8"></asp:TextBox>
			                    </ItemTemplate>
			                </asp:TemplateField>
			                <asp:TemplateField HeaderText="FY10">
			                    <ItemTemplate>
			                        <asp:TextBox ID="txtBM1" runat="server" Text='<%# Eval("BENCHMARK_VALUE") %>' Width="60" MaxLength="8"></asp:TextBox>
			                    </ItemTemplate>
			                </asp:TemplateField>
			                <asp:TemplateField HeaderText="FY11">
			                    <ItemTemplate>
			                        <asp:TextBox ID="txtBM2" runat="server" Text='<%# Eval("BENCHMARK2_VALUE") %>' Width="60" MaxLength="8"></asp:TextBox>
			                    </ItemTemplate>
			                </asp:TemplateField>
			                 <asp:TemplateField HeaderText="FY12">
			                    <ItemTemplate>
			                        <asp:TextBox ID="txtBM3" runat="server" Text='<%# Eval("BENCHMARK3_VALUE") %>' Width="60" MaxLength="8"></asp:TextBox>
			                    </ItemTemplate>
			                </asp:TemplateField>
			                 <asp:TemplateField HeaderText="FY13">
			                    <ItemTemplate>
			                        <asp:TextBox ID="txtBM4" runat="server" Text='<%# Eval("BENCHMARK4_VALUE") %>' Width="60" MaxLength="8"></asp:TextBox>
			                    </ItemTemplate>
			                </asp:TemplateField>
			                <asp:TemplateField HeaderText="FY14">
			                    <ItemTemplate>
			                        <asp:TextBox ID="txtBM5" runat="server" Text='<%# Eval("BENCHMARK5_VALUE") %>' Width="60" MaxLength="8"></asp:TextBox>
			                    </ItemTemplate>
			                </asp:TemplateField>
			                <asp:BoundField DataField="INTERNAL_FISCAL_YEAR_VALUE" HeaderText="FY YEAR" />
			                <asp:BoundField DataField="TIME_PERIOD_SID" HeaderText="TIME_PERIOD" />
			            </Columns>
			        </asp:GridView>

Open in new window

Author

Commented:
CmdoProg2:
Ok see what you mean with the hidden fields, just started working with .net 4.0 and 3.5 and in my old 1.1 we did not have this ability and I had to hide fields  and then grab data but you cannot turn columns invisible with the new gridviews because it does not render and you can't get data, this is the way to do it.  thanks a ton, now I can go back and change an eariler project to work same way with easier coding.

<ItemTemplate>
                                          <asp:TextBox ID="txtActual" runat="server" Text='<%# Eval("ACTUAL_VALUE") %>' Width="60" MaxLength="8"></asp:TextBox>
                                          <asp:HiddenField ID="txtActualHid" runat="server" Value='<%# Eval("ACTUAL_VALUE") %>' />
                                      </ItemTemplate>

Author

Commented:
Awesome, saves me a lot of coding.

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