gridview 行仅在更改时更新,javascript 不更改值:(
我在更新面板中有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
getElementById,小写最后一个 D。
getElementById, lowercase the last D.
它现在突然工作了,对于与日历扩展程序(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.