我在我的asp.net頁面中有一個ListView
表。對每一行都有刪除按鈕。點擊按鈕時,相應的項目將被刪除。在ListView中的項目被刪除後刷新DataPager
我已經完成了所有這些。也可以在刪除項目後刷新數據。
這裏是我的代碼:
ASPX
<asp:ListView ID="CategoriesList" runat="server" ItemPlaceholderID="PlaceHolder1" OnPagePropertiesChanging="OnPagePropertiesChanging">
<LayoutTemplate>
<table>
<tr><th>Header</th></tr>
<asp:PlaceHolder runat="server" ID="PlaceHolder1"></asp:PlaceHolder>
</table>
<asp:DataPager ID="DataPager" runat="server" PagedControlID="CategoriesList" PageSize="3" QueryStringField="page">
<Fields>
//fields goes here
</Fields>
</asp:DataPager>
</LayoutTemplate>
<ItemTemplate>
<tr><td> //item goes here </td></tr>
</ItemTemplate>
</asp:ListView>
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindListView();
}
}
private void BindListView()
{
// SQL connection string, command, etc to get the data from database
DataTable DTable = new DataTable();
SDAdapter.Fill(DTable);
CategoriesList.DataSource = DTable;
CategoriesList.DataBind();
}
protected void OnPagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
{
(CategoriesList.FindControl("DataPager") as DataPager).SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
BindListView();
}
刪除項目後,該數據被再次調用BindListView();
更新。它完美的作品。
我的問題是當最後一頁只存在一個項目並且它被刪除時,DataPager
不會轉到上一頁。相反,它保持在沒有數據的同一頁面中,甚至沒有表格。
如何刷新DataPager並在當前頁面的最後一項被刪除後轉到上一頁?
最簡單的解決方法是將召回BindListView()方法,刪除 –
後,請仔細閱讀問題_「刪除項目後,該數據是通過調用BindListView()更新;再次」 _。它更新數據。但是當最後一個項目被刪除時,將顯示一個沒有數據的空白頁面。 – Munawir
如果您檢測到表(DTable)爲空,您可以重定向嗎? –