Incorporating a gridview (ASP.net) inside an update panel, I included a "Save" button that triggers a loop through all the rows of the grid view to pass data to a stored procedure for updating each row. However, this process proved to be slow and resulted in database updates even when no changes occurred.
To address this issue, I made an adjustment by adding a hidden field within my gridview:
<asp:TemplateField>
<ItemTemplate>
<input type="hidden" id="hdnIsChanged" runat="server" />
</ItemTemplate>
</asp:TemplateField>
The idea behind adding this hidden field was to track any changes made to textboxes or dropdown values in a gridview row by updating the hidden field value to 1. This modification was implemented in the gvLineItems_RowDataBound
event as follows:
Dim hiddenField As HtmlInputHidden = DirectCast(e.Row.FindControl("hdnIsChanged"), HtmlInputHidden)
'Tracking line item date
Dim tLID As TextBox = CType(e.Row.FindControl("txtLineItemDate"), TextBox)
tLID.Attributes.Add("onchange", "document.getElementById('" + hiddenField.ClientID + "').value=1")
'Tracking the amount field
Dim ta As TextBox = CType(e.Row.FindControl("txtAmount"), TextBox)
ta.Attributes.Add("onchange", "document.getElementById('" + hiddenField.ClientID + "').value=1")
The intention was to set the value to 1 upon change. Subsequently, in the save button function, the following logic would execute:
For Each Row As GridViewRow In Me.gvLineItems.Rows
Dim hiddenField As HtmlInputHidden = DirectCast(Row.FindControl("hdnIsChanged"), HtmlInputHidden)
If (hiddenField.Value = "1") Then
'perform the update...
The problem arises during debugging as the value of hiddenField.Value
consistently remains at 1 whether there were actual changes made in the textbox or not. I came across a similar discussion on this forum thread.
While it seemed to have resolved the issue for others, I continue to face the challenge where the value always registers as 1...