开发者

gridview row update only when changed, javascript not changing value :(

开发者 https://www.devze.com 2023-03-19 15:47 出处:网络
I\'ve got a gridview (ASP.net) inside of an update panel.I also have a \"Save\" button so that when I click \"Save\" it loops through all the rows of a grid view and passes the data to a stored proced

I've got a gridview (ASP.net) inside of an update panel. I also have a "Save" button so that when I click "Save" it loops through all the rows of a grid view and passes the data to a stored procedure to update each row. This was too slow as sometimes I would be updating the database even though a change did not occur.

I decided to add a field in my gridview like so:

<asp:TemplateField>
      <ItemTemplate>
          <input type="hidden" id="hdnIsChanged" runat="server" />
      </ItemTemplate>
 </asp:TemplateField>

That is, I added a hidden field and the idea was that if a textbox or drop down value changed in my gridview row I would update this hidden field with the value 1. So I added this to my gvLineItems_RowDataBound event:

Dim hiddenField As HtmlInputHidden = DirectCast(e.Row.FindControl("hdnIsChanged"), HtmlInputHidden)

    'the line item date
    Dim tLID As T开发者_运维知识库extBox = CType(e.Row.FindControl("txtLineItemDate"), TextBox)
    tLID.Attributes.Add("onchange", "document.getElementById('" + hiddenField.ClientID + "').value=1")

    'the amount field
    Dim ta As TextBox = CType(e.Row.FindControl("txtAmount"), TextBox)
    ta.Attributes.Add("onchange", "document.getElementById('" + hiddenField.ClientID + "').value=1")

The idea was so that onchange it would set the value 1. Then in my save button I'd do something to this effect:

  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 issue I am having is when I debug I see that hiddenField.Value is always 1 whether I change a value in the textbox or not. I found this similiar post: http://forums.asp.net/t/1592125.aspx/1

It seems to work for that guy, but for me the value is always 1...


getElementById, lowercase the last D.


Its now working all of a sudden, it seems to fail for any textbox that is associated with a calendarextender (ajax). So I got rid of the rowdatabound for this one field and it works now...strange.

0

精彩评论

暂无评论...
验证码 换一张
取 消