gridview 行仅在更改时更新,javascript 不更改值:(

发布于 2024-11-19 08:44:35 字数 1585 浏览 4 评论 0原文

我在更新面板中有一个 gridview (ASP.net)。我还有一个“保存”按钮 这样,当我单击“保存”时,它会循环遍历网格视图的所有行,并将数据传递到存储过程以更新每一行。这太慢了,因为有时即使没有发生更改,我也会更新数据库。

我决定在我的 gridview 中添加一个字段,如下所示:

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

也就是说,我添加了一个隐藏字段,其想法是,如果我的 gridview 行中的文本框或下拉值发生更改,我将用值 1 更新此隐藏字段。所以我将此添加到我的 gvLineItems_RowDataBound 事件中:

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

    'the line item date
    Dim tLID As TextBox = 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")

这个想法是这样 onchange 它将设置值 1。然后在我的保存按钮中我会执行一些操作以实现此效果:

  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...

我遇到的问题是当我调试时我懂了无论我是否更改文本框中的值,hiddenField.Value 始终为 1。我发现了这个类似的帖子: http://forums.asp.net/t/1592125.aspx /1

这似乎对那个人有用,但对我来说,值始终是 1...

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 TextBox = 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...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笑着哭最痛 2024-11-26 08:44:35

getElementById,小写最后一个 D。

getElementById, lowercase the last D.

花之痕靓丽 2024-11-26 08:44:35

它现在突然工作了,对于与日历扩展程序(ajax)关联的任何文本框似乎都失败了。所以我摆脱了这一字段的 rowdatabound,现在它可以工作了......奇怪。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文