将更改保存在gridview的可编辑文本框中
本文关键字:编辑 文本 gridview 保存 存在 | 更新日期: 2023-09-27 18:04:22
我有这个gridview
<asp:GridView ID="gv1" runat="server" AllowPaging="True" AutoGenerateColumns="False"
EmptyDataText="No elements" EnableSortingAndPagingCallbacks="True" PageSize="5"
CssClass="table table-striped table-bordered table-hover table-responsive datatable">
<Columns>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:CheckBox ID="chkElement" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="30" >
<ItemStyle Width="30px" />
</asp:BoundField>
<asp:TemplateField HeaderText="Performance">
<ItemTemplate>
<asp:TextBox ID="txtPerformance" runat="server" AutoPostBack="true" OnTextChanged="txtPerformance_TextChanged" Text= '<%# Bind("Performance") %>' CssClass="form-control input-width-medium" />
</ItemTemplate>
</asp:TemplateField>
<EmptyDataRowStyle CssClass="titletable" />
<HeaderStyle CssClass="titletable" />
<PagerSettings PageButtonCount="15" />
<RowStyle CssClass="table table-striped table-bordered table-hover table-responsive datatable"/>
</asp:GridView>
这个gridview的每一行都有一个文本框,它绑定了sql查询的可数据结果,所以用户可以在gridview页面的任何一行编辑文本框。
我的问题是如何在用户编辑文本框中捕获行,因此gridview有非常页,当返回到页面时,他们已经编辑了一行,此更改不保存
gridview项模板没有设置为跟踪这样的更改。您需要向文本框中添加一个事件和一些隐藏控件,以允许您获取手动进行更改所需的信息。
这个项目应该为您提供您需要的信息,并且已经有一些自定义控件,应该为您开箱即用。
作者有这篇文章,将给出一个更详细的解释为什么会发生这种情况,如果你想自己解决这个问题,而不是使用他们提供的控件,你可以做些什么来修复它
只是一些代码片段,让您开始(您可以查找的搜索词)。
不是一个容易回答的问题。价值不低于50,000 rep或5000$:D
<asp:SqlDataSource ID="SqlDataSource1" runat="server"></asp:SqlDataSource>
<asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource1" DataKeyNames="order_detailsID"
OnItemDataBound="ListView1_ItemDataBound" OnPreRender="ListView1_PreRender"
OnItemCommand="ListView1_OnItemCommand" OnItemUpdating="ListView1_ItemUpdating">
<itemtemplate>
<ul class="products">
<li><%#Container.DataItem("name")%></li>
<li id="price" runat="server" class="amount"><%#Container.DataItem("price")%></li>
<li class="quantity"><asp:TextBox ID="quantity" runat="server" text='<%#Bind("quantity")%>' /></li>
</ul>
</itemtemplate>
Protected Sub ListView1_OnItemCommand(ByVal sender As Object, ByVal E As ListViewCommandEventArgs)
Protected Sub ListView1_ItemDataBound(ByVal sender As Object, ByVal e As ListViewItemEventArgs)
Protected Sub ListView1_PreRender(ByVal sender As Object, ByVal e As EventArgs)
Protected Sub ListView1_ItemUpdating(ByVal sender As Object, ByVal e As ListViewUpdateEventArgs)
Protected Sub updateOrderDetails(ByVal sender As Object, ByVal e As EventArgs)
SqlDataSource1.ConnectionString = config.connectString
SqlDataSource1.UpdateCommand = "IF (@quantity > 0) " _
+ "UPDATE order_details SET quantity = @quantity WHERE order_detailsID = @order_detailsID " _
+ "ELSE DELETE FROM order_details WHERE order_detailsID = @order_detailsID"
Dim dataItem As ListViewDataItem
For Each dataItem In ListView1.Items
ListView1.UpdateItem(dataItem.DataItemIndex, True)
Next
End Sub