多重编辑/保存网格(hiddenfield + javascript)

发布于 2025-01-02 13:01:23 字数 359 浏览 0 评论 0原文

我有一个包含 10 行的 gridview,其中 1 列是可以编辑的组合框。当我单击“编辑”按钮时,所有 10 行都变得可编辑,并且我想跟踪我更改了哪些行,这样如果仅对其中 2 行进行了更改,我就不会将所有 10 行保存到数据库中他们。到目前为止我的想法:

在网格中创建一个额外的隐藏列(就像一个告诉该行已编辑的复选框) 在组合框上的 SelectedIndexChanged 上,我将找到一个脚本,该脚本告诉我当前正在编辑的网格行,并在该行的隐藏列中设置复选标记

然后,当我按“保存”时,我可以遍历网格线并查看哪些有复选标记,然后将这些行保存到数据库

Javascript 不是我的强项,因此任何帮助/提示将不胜感激,或者完全不同的解决方案也可以工作:)

I got a gridview which contains 10 rows where 1 of the columns is a combobox that can be edited. When I click on an "edit" button, all of the 10 rows becomes editable and I want to keep track of which rows I have changed so that I dont save all 10 rows to the DB if there has only been made changes to 2 of them. My toughts so far:

Make an extra hidden column in the grid (like a checkbox that tells that the row is edited)
On SelectedIndexChanged on the combobox im going to find a script that tells me the grid-row im currently editing and sets the checkmark in the hidden column for that row

Then when i press "Save" I can run through the lines of the grid and see which ones has the checkmark and then save those rows to the DB

Javascript is not my strong side so any help/tips would be appreciated, or a completely different solution would work too :)

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

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

发布评论

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

评论(1

唱一曲作罢 2025-01-09 13:01:23

我的解决方案:
添加了一个

在网格的 ItemTemplate

然后在 RowDataBound 上的网格中我找到了隐藏字段
Dim hdnField As HtmlInputHidden = DirectCast(e.Row.FindControl("hdnIsChanged"), HtmlInputHidden)

和 dropdownlist
Dim ddlTest As DropDownList = CType(e.Row.FindControl("ddlTest"), DropDownList)
ddlTest.Attributes.Add("onChange", "Test('" + hdnField.ClientID + "');")

<script type="text/javascript">
function Test(hiddenField) {
    var test = document.getElementById(hiddenField)
        test.value = "1"
}
</script>

最后,我可以遍历网格的行并查看哪些行已更改:

For Each r As GridViewRow In gvTest.Rows
    Dim hdnField As HtmlInputHidden = DirectCast(r.FindControl("hdnIsChanged"), HtmlInputHidden)

    If hdnField.Value = "1" Then
        // do updates
        End If
Next

My solution:
Added a
<input type="hidden" id="hdnIsChanged" runat="server" />
in the ItemTemplate of the grid

Then in the grid on RowDataBound i found the hiddenField
Dim hdnField As HtmlInputHidden = DirectCast(e.Row.FindControl("hdnIsChanged"), HtmlInputHidden)

and dropdownlist
Dim ddlTest As DropDownList = CType(e.Row.FindControl("ddlTest"), DropDownList)
ddlTest.Attributes.Add("onChange", "Test('" + hdnField.ClientID + "');")

<script type="text/javascript">
function Test(hiddenField) {
    var test = document.getElementById(hiddenField)
        test.value = "1"
}
</script>

Finally I could run through the rows of the grid and see which ones had changed:

For Each r As GridViewRow In gvTest.Rows
    Dim hdnField As HtmlInputHidden = DirectCast(r.FindControl("hdnIsChanged"), HtmlInputHidden)

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