在gridview中保存可编辑文本框中的更改

时间:2014-05-21 15:18:24

标签: c# asp.net vb.net gridview

我是这个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有非常页面,当返回页面时,他们已经编辑了一行,此更改未保存

2 个答案:

答案 0 :(得分:0)

未设置gridview项模板以跟踪此类更改。 您需要在文本框中添加一个事件和一些隐藏控件,以便您获得手动进行更改所需的信息。

this project应该为您提供所需的信息,并且已经有一些自定义控件可供您使用。

作者this post将更详细地解释为什么会发生这种情况,以及如果您想自己解决此问题而不是使用他们提供的控件,您可以采取哪些措施来解决问题

答案 1 :(得分:0)

只是一些代码片段可以帮助您入门(您可以查找的搜索字词)。

不是一个容易回答的问题。 def不值得少于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