關於asp.net假分頁的刪除操作的隨筆

来源:http://www.cnblogs.com/ContinuousLearning/archive/2017/06/16/7028360.html
-Advertisement-
Play Games

作為一個新人,上周負責優化一個後臺管理系統,遇到一個問題:點擊刪除按鈕之後,頁面又回到了第一頁。 而我需要達到的效果是:點擊了刪除按鈕之後,原來是那一頁,刪除後還是在那一頁。 由於項目是已經驗收了的,所以我不能改動太大。我在網上頁找了很久,也有許多解決方案,但都不太適合。 這個系統數據不多,所以用的 ...


作為一個新人,上周負責優化一個後臺管理系統,遇到一個問題:點擊刪除按鈕之後,頁面又回到了第一頁。

而我需要達到的效果是:點擊了刪除按鈕之後,原來是那一頁,刪除後還是在那一頁。

由於項目是已經驗收了的,所以我不能改動太大。我在網上頁找了很久,也有許多解決方案,但都不太適合。

這個系統數據不多,所以用的假分頁,使用了Repeater控制項,後臺將資料庫中的數據全部查詢出來放到Repeater中,在通過jquery進行分頁,下麵是我寫的一個簡單的測試項目

前臺代碼如下:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="PagingTestDome1.Index" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <script src="js/jquery-1.6.2.min.js"></script>
    <title></title>
    <style type="text/css">
        a{
            text-decoration:none;color:#686868;
        }
        a:hover{
            color:#6287ef;
        }
        .page_set
        {
            float: left;
            margin-top: 50px;
        }

        .table_content
        {
            text-align: center;
        }
        .page_set a
        {
            border: 0.5px solid #999;
            padding: 1px 4px;
        }
        .page_set p
        {
            display: inline;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <h3>假分頁——刪除</h3>
        <asp:Repeater ID="rp_news" runat="server" OnItemDataBound="rp_news_ItemDataBound" OnItemCommand="rp_news_ItemCommand">
            <HeaderTemplate>
                <table id="tableinfo" border="1px" style="border-collapse: collapse;" bordercolor="#eeeeee" cellpadding="2px" cellspacing="0">
                    <tr style="height:22px; background-color:#eeeeee;  text-align:center;">
                        <th style="width:300px; font-weight:normal;">編號</th>
                        <th style="width:300px; font-weight:normal;">商品名</th>
                        <th style="width:300px; font-weight:normal;">價格</th>
                        <th style="width:80px; font-weight:normal;">刪除</th>
                    </tr>
            <tbody id="trId">
            </HeaderTemplate>
            <ItemTemplate>
                <asp:Panel ID="plItem" runat="server">
                    <tr style="height:17px; background-color:White; text-align:center;">
                        <asp:Label ID="lb_ID1" runat="server" Text='<%# Eval("id")%>'></asp:Label>
                        <td><asp:Label ID="lb_ID" runat="server" Text='<%# Eval("id")%>'></asp:Label></td>
                        <td><asp:Label ID="lb_name" runat="server" Text='<%# Eval("goodsName")%>'></asp:Label></td>
                        <td><asp:Label ID="lb_price" runat="server" Text='<%# Eval("price")%>'></asp:Label></td>
                        <td><asp:ImageButton ID="lbtDelete" ImageUrl="img/delete.png" runat="server" CommandName="Delete" /></td>
                    </tr>
                </asp:Panel>
            </ItemTemplate>
            <FooterTemplate>
            </tbody>
                </table>
            </FooterTemplate>
        </asp:Repeater>   
        <div class="page_set">
            <p id="pages"></p>
            <p id="sjzl"></p> 
            <a  href="#" id="btn_first">首頁</a>
            <a  href="#" id="btn_previous">上一頁</a>
            <a  href="#" id="btn_next">下一頁</a>
            <a  href="#" id="btn_last">尾頁</a> 
            <p>轉到 </p>
            <input style="width:40px;height:16px;" maxlength="5" id="changePage" />
            <p>頁 </p>
            <a  href="#" id="btn_change">跳轉</a>
        </div>
        <div class="clear"></div>
        <script type="text/javascript">
        var pageSize = 5;    //每頁顯示的記錄條數
        var curPage = 0;        //當前頁
        var lastPage;        //最後頁
        var direct = 0;        //方向
        var len;            //總行數
        var page;            //總頁數
        var begin;
        var end;
        var cPage=0;
    $(document).ready(function display() {
            len = $("#trId tr").length;    // 求這個表的總行數,剔除第一行介紹
            page = len % pageSize == 0 ? len / pageSize : Math.floor(len / pageSize) + 1; //根據記錄條數,計算頁數
            curPage = 1;    // 設置當前為第一頁
            displayPage(1); //顯示第一頁
            document.getElementById("pages").innerHTML = "當前 " + curPage + "/" + page + "";    // 顯示當前多少頁
            document.getElementById("sjzl").innerHTML = "數據總量 " + len + "";        // 顯示數據量
        $("#btn_first").click(function firstPage() {    // 首頁
                curPage = 1;
                direct = 0;
                displayPage();
            });
            $("#btn_previous").click(function frontPage() {    // 上一頁
                direct = -1;
                displayPage();
            });
            $("#btn_next").click(function nextPage() {    // 下一頁
                direct = 1;
                displayPage();
            });
            $("#btn_last").click(function lastPage() {    // 尾頁
                curPage = page;
                direct = 0;
                displayPage();
            });
            $("#btn_change").click(function changePage() {    // 轉頁
                curPage = document.getElementById("changePage").value * 1;
                if (!/^[1-9]\d*$/.test(curPage)) {
                    alert("請輸入正整數");
                    return;
                }
                if (curPage > page) {
                    alert("超出數據頁面");
                    return;
                }
                direct = 0;
                displayPage();
            });
        });
            function displayPage() {
                if (curPage <= 1 && direct == -1) {
                    direct = 0;
                    alert("已經是第一頁了");
                    return;
                } else if (curPage >= page && direct == 1) {
                    direct = 0;
                    alert("已經是最後一頁了");
                    return;
                }

                lastPage = curPage;

                // 修複當len=1時,curPage計算+得0的bug
                if (len > pageSize) {
                    curPage = ((curPage + direct + len) % len);
                } else {
                    curPage = 1;
                }
                document.getElementById("pages").innerHTML = "當前 " + curPage + "/" + page + "";        // 顯示當前多少頁
                begin = (curPage - 1) * pageSize; // 起始記錄號
                end = begin + 1 * pageSize - 1;    // 末尾記錄號
          if (end > len) end = len;
                $("#trId tr").hide();    // 首先,設置這行為隱藏
                $("#trId tr").each(function (i) {    // 然後,通過條件判斷決定本行是否恢復顯示
                    if ((i >= begin && i <= end))//顯示begin<=x<=end的記錄
                        $(this).show();
                });
            }
        
    </script>
    </div>
    </form>
</body>
</html>

後臺代碼如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using BLL;
using Model;

namespace PagingTestDome1
{
    public partial class Index : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack) {
                Bind();
            }
        }
        protected void Bind() {
            BLL.tbl_goods goods = new BLL.tbl_goods();
            IList<Model.tbl_goods> List=new List<Model.tbl_goods>();
            List = goods.GetList();
            rp_news.DataSource = List;
            rp_news.DataBind();

        }

        protected void rp_news_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Panel pn = (Panel)e.Item.FindControl("plItem");
                Label id = (Label)pn.FindControl("lb_ID1");
                Label name = (Label)pn.FindControl("lb_name");
                id.Visible = false;
                ((ImageButton)e.Item.FindControl("lbtDelete")).Attributes.Add("onclick", string.Format("JavaScript:return confirm('你確認要刪除 {0} 嗎');", name.Text));
            }
        }

        protected void rp_news_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            
            if (e.CommandName == "Delete")
            {
                Panel pn = (Panel)e.Item.FindControl("plItem");
                Label id = (Label)pn.FindControl("lb_ID1");
                int ID = Convert.ToInt32(id.Text);
                BLL.tbl_goods goods = new BLL.tbl_goods();
                goods.Delete(ID);
                Response.Write("<script>alert('刪除成功!!!')</script>");
                Bind();
            }
        }

    }
}

這裡的刪除按鈕是用的ImageButton,這是一個提交按鈕,所以點擊了之後頁面會刷新。

在不能改動太大的前提下,要達到我所需要的效果。

有一個思路就是將當前的頁碼保存下來,點擊刪除按鈕後,通過jquery控制它跳到保存下來的頁碼的那一頁。

但是在前臺頁碼中,分頁處的代碼都是html標簽,不具有保存頁碼的功能,所以就需要改下前臺的代碼了。

將分頁處的input標簽:

<input style="width:40px;height:16px;" maxlength="5" id="changePage" />

改為TextBox:

<asp:TextBox ID="changePage" runat="server" Width="40" Height="16" MaxLength="5"></asp:TextBox> 

用asp.net所帶的TextBox來保存頁碼就可以了。

那麼,分頁的jquery也需要改一下了:

<script type="text/javascript">
        var pageSize = 5;    //每頁顯示的記錄條數
        var curPage = 0;        //當前頁
        var lastPage;        //最後頁
        var direct = 0;        //方向
        var len;            //總行數
        var page;            //總頁數
        var begin;
        var end;
        var cPage=0;


        $(document).ready(function display() {
            len = $("#trId tr").length;    // 求這個表的總行數,剔除第一行介紹
            page = len % pageSize == 0 ? len / pageSize : Math.floor(len / pageSize) + 1; //根據記錄條數,計算頁數
            if (document.getElementById("changePage").value * 1 == "") {
                curPage = 1;    // 設置當前為第一頁
                displayPage(1); //顯示第一頁
            } else {
                curPage = document.getElementById("changePage").value * 1;
                displayPage(curPage);//跳轉到保存的頁碼處
            }
            
            document.getElementById("pages").innerHTML = "當前 " + curPage + "/" + page + "";    // 顯示當前多少頁
            document.getElementById("sjzl").innerHTML = "數據總量 " + len + "";        // 顯示數據量



            $("#btn_first").click(function firstPage() {    // 首頁
                curPage = 1;
                direct = 0;
                displayPage();
                $("#changePage").val(curPage);//保存頁碼
            });
            $("#btn_previous").click(function frontPage() {    // 上一頁
                direct = -1;
                displayPage();
                $("#changePage").val(curPage);//保存頁碼
            });
            $("#btn_next").click(function nextPage() {    // 下一頁
                direct = 1;
                displayPage();
                $("#changePage").val(curPage);//保存頁碼
            });
            $("#btn_last").click(function lastPage() {    // 尾頁
                curPage = page;
                direct = 0;
                displayPage();
                $("#changePage").val(curPage);//保存頁碼
            });
            $("#btn_change").click(function changePage() {    // 轉頁
                curPage = document.getElementById("changePage").value * 1;
                if (!/^[1-9]\d*$/.test(curPage)) {
                    alert("請輸入正整數");
                    return;
                }
                if (curPage > page) {
                    alert("超出數據頁面");
                    return;
                }
                direct = 0;
                displayPage();
            });
        });
            function displayPage() {
                if (curPage <= 1 && direct == -1) {
                    direct = 0;
                    alert("已經是第一頁了");
                    return;
                } else if (curPage >= page && direct == 1) {
                    direct = 0;
                    alert("已經是最後一頁了");
                    return;
                }

                lastPage = curPage;

                // 修複當len=1時,curPage計算+得0的bug
                if (len > pageSize) {
                    curPage = ((curPage + direct + len) % len);
                } else {
                    curPage = 1;
                }


                document.getElementById("pages").innerHTML = "當前 " + curPage + "/" + page + "";        // 顯示當前多少頁

                begin = (curPage - 1) * pageSize; // 起始記錄號
                end = begin + 1 * pageSize - 1;    // 末尾記錄號


                if (end > len) end = len;
                $("#trId tr").hide();    // 首先,設置這行為隱藏
                $("#trId tr").each(function (i) {    // 然後,通過條件判斷決定本行是否恢復顯示
                    if ((i >= begin && i <= end))//顯示begin<=x<=end的記錄
                        $(this).show();
                });
            }
        
    </script>

好了,這樣差不多就達到了我所需要的效果了。

結語:我是一個小菜鳥,第一次寫隨筆,歡迎大神指點,不喜勿噴。

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • supervisor 介紹: 這是一款用python編寫的進程管理工具,可以守護他管理的所有進程,防止異常退出,以及提供一個可視化的web界面來手動管理,打開關閉重啟各種應用,界面如下: 關於在centos上安裝supervisor: 1、通過yum安裝: 2、配置supervisor: 我們去用v ...
  • Asp.net core 以及MVC4.6中如何全局對用戶輸入做Trim空白處理?本文實例分享 ...
  • 最近項目需要做一個客戶查詢狀態系統,當前上位機缺少服務功能,於是找到了networkcomms 開源框架,作為項目使用. 最新版networkcomms 下載地址:https://github.com/MarcFletcher/NetworkComms.Net 下載直接vs打開 新建伺服器端 在別的 ...
  • 人總是很忙的,但是一個人就是一個人,不存在分身術。 假設有個人王大柱,他是光明中學的校長,還是光明村的村委會成員,同時還是他兒子的父親。 那麼我們可以這麼想:王大柱是一個類的具體的實現對象,這類名叫“王大柱類”,而王大柱類實現了三個介面:“I光明中學校長”、“I光明村村委會成員”、“I父親”。 畫圖 ...
  • 1、確定控制項應該繼承的基類 從錶面上看,目前WPF自帶常用控制項中,沒有一個是接近這個表盤控制項的,但將該控制項拆分就能夠發現,該控制項的每個子部分都是在WPF中存在的,因此我們需要將各個子控制項組合才能形成這個表盤控制項,因此我們直接定義一個Dashboard類,繼承自Control類。 2、設置Dashbo ...
  • 前臺: 後臺: ...
  • #region 請求Url,不發送數據 /// /// 請求Url,不發送數據 /// public static string RequestUrl(string url) { return RequestUrl(url, "POST"); } #endregion #region 請求Url,不... ...
  • 1.IIS下配置自己的網站,添加主機名 2.修改hosts文件(C://Windows/System32/drivers/etc) 3.VS中配置項目Web伺服器(選擇外部主機) ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...