我只是想习惯LV,我只是把这个放在一起。我的印象是LV会在插入后处理LV的插入和刷新,但事实并非如此。我甚至添加了LV.rebind(),但仍然没有...
我在第一次插入后让LV正确刷新/重新绑定,但后续插入不会产生刷新的LV。我已确认记录已正确插入数据库。
到目前为止,这是我的代码,是吗
<asp:ListView ID="ListView1" runat="server" DataKeyNames="ProductID" EnableViewState="true"
datasourceid="sqlDS" InsertItemPosition="LastItem">
<AlternatingItemTemplate>
<tr style="">
<td>
</td>
<td>
<asp:Label ID="ProductIDLabel" runat="server" Text='<%# Eval("ProductID") %>' />
</td>
<td>
<asp:Label ID="CategoryLabel" runat="server" Text='<%# Eval("Category") %>' />
</td>
<td>
<asp:Label ID="DescriptionLabel" runat="server"
Text='<%# Eval("Description") %>' />
</td>
<td>
<asp:Label ID="CostLabel" runat="server" Text='<%# Eval("Cost") %>' />
</td>
</tr>
</AlternatingItemTemplate>
<EditItemTemplate>
<tr style="">
<td>
<asp:Button ID="UpdateButton" runat="server" CommandName="Update"
Text="Update" />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel"
Text="Cancel" />
</td>
<td>
<asp:Label ID="ProductIDLabel1" runat="server"
Text='<%# Eval("ProductID") %>' />
</td>
<td>
<asp:TextBox ID="CategoryTextBox" runat="server"
Text='<%# Bind("Category") %>' />
</td>
<td>
<asp:TextBox ID="DescriptionTextBox" runat="server"
Text='<%# Bind("Description") %>' />
</td>
<td>
<asp:TextBox ID="CostTextBox" runat="server" Text='<%# Bind("Cost") %>' />
</td>
</tr>
</EditItemTemplate>
<EmptyDataTemplate>
<table runat="server" style="">
<tr>
<td>
No data was returned.</td>
</tr>
</table>
</EmptyDataTemplate>
<InsertItemTemplate>
<tr style="">
<td>
<asp:Button ID="InsertButton" runat="server" CommandName="Insert"
Text="Insert" />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel"
Text="Clear" />
</td>
<td>
</td>
<td>
<asp:TextBox ID="CategoryTextBox" runat="server"
Text='<%# Bind("Category") %>' />
</td>
<td>
<asp:TextBox ID="DescriptionTextBox" runat="server"
Text='<%# Bind("Description") %>' />
</td>
<td>
<asp:TextBox ID="CostTextBox" runat="server" Text='<%# Bind("Cost") %>' />
</td>
</tr>
</InsertItemTemplate>
<ItemTemplate>
<tr style="">
<td>
</td>
<td>
<asp:Label ID="ProductIDLabel" runat="server" Text='<%# Eval("ProductID") %>' />
</td>
<td>
<asp:Label ID="CategoryLabel" runat="server" Text='<%# Eval("Category") %>' />
</td>
<td>
<asp:Label ID="DescriptionLabel" runat="server"
Text='<%# Eval("Description") %>' />
</td>
<td>
<asp:Label ID="CostLabel" runat="server" Text='<%# Eval("Cost") %>' />
</td>
</tr>
</ItemTemplate>
<LayoutTemplate>
<table runat="server">
<tr runat="server">
<td runat="server">
<table ID="itemPlaceholderContainer" runat="server" border="0" style="">
<tr runat="server" style="">
<th runat="server">
</th>
<th runat="server">
ProductID</th>
<th runat="server">
Category</th>
<th runat="server">
Description</th>
<th runat="server">
Cost</th>
</tr>
<tr ID="itemPlaceholder" runat="server">
</tr>
</table>
</td>
</tr>
<tr runat="server">
<td runat="server" style="">
<asp:DataPager ID="DataPager1" runat="server">
<Fields>
<asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True"
ShowLastPageButton="True" />
</Fields>
</asp:DataPager>
</td>
</tr>
</table>
</LayoutTemplate>
</asp:ListView>
<asp:SqlDataSource ID="sqlDS" runat="server"
DataSourceMode="DataSet" EnableCaching="true"
ConnectionString="<%$ ConnectionStrings:ProjectDashboardConnectionString %>"
SelectCommand="usp_CafeteriaGetProducts" SelectCommandType="StoredProcedure"
FilterExpression="Category='A'"
InsertCommand="usp_CafeteriaInsertProducts" InsertCommandType="StoredProcedure">
<InsertParameters>
<asp:Parameter Name="Description" Type="String" />
<asp:Parameter Name="Category" Type="String" />
<asp:Parameter Name="Cost" Type="Decimal" />
</InsertParameters>
</asp:SqlDataSource>
代码beind:
Public Class WebForm3
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Protected Sub ListView1_ItemInserted(ByVal sender As Object, ByVal e As ListViewInsertedEventArgs) Handles ListView1.ItemInserted
sqlDS.Dispose()
sqlDS.DataBind()
ListView1.Dispose()
ListView1.DataSourceID = "sqlDS"
ListView1.DataBind()
End Sub
Protected Sub sqlProducts_ItemInserting(ByVal sender As Object, ByVal e As SqlDataSourceCommandEventArgs) Handles sqlDS.Inserting
''Do something
End Sub
Protected Sub sqlProducts_ItemInserted(ByVal sender As Object, ByVal e As SqlDataSourceStatusEventArgs) Handles sqlDS.Inserted
''Do something
End Sub
End Class
任何想法? 感谢。
答案 0 :(得分:0)
这是因为sqldatasource属性EnableCaching设置为“true”。完全删除了该属性,因为默认值为False。