asp.Net+ajax単純ページングインスタンス分析

7955 ワード

本文の例はaspを述べた.Net+ajax単純ページング実装方法.皆さんの参考にしてください.具体的には以下の通りです.
ここには2つあります.aspxファイル、Defaultと申します.aspx、AjaxOperationsという名前です.aspxは、最初はいくつかのテストデータを表示し、次はページングを処理するために使用されます.jsフォルダの下にはtestJsもあります.jsのファイルです.ajax操作の核心部分です.いいですね.code is cheapです.コードを見てください:

/*testJs.js*/
//       document.getElementById /document.all
function $(s) { if (document.getElementById) { return eval('document.getElementById("' + s + '")'); } else { return eval('document.all.' + s); } }
//    XMLHttpRequest  ,   ajax   
function createXMLHTTP() {
 var xmlHttp = false;
 var arrSignatures = ["MSXML2.XMLHTTP.5.0", "MSXML2.XMLHTTP.4.0",
       "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP",
       "Microsoft.XMLHTTP"];
 for (var i = 0; i < arrSignatures.length; i++) {
  try {
   xmlHttp = new ActiveXObject(arrSignatures[i]);
   return xmlHttp;
  }
  catch (oError) {
   xmlHttp = false; //ignore
  }
 }
 // throw new Error("MSXML is not installed on your system."); 
 if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
  xmlHttp = new XMLHttpRequest();
 }
 return xmlHttp;
}
//window.onload = showPages(1, 10, 100);
//cP    , tP    ,tN     
function showPages(cP, tP, tN) {
 //          
 if (cP >= tP) {
  cP = tP;
 }
 //      1 
 if (cP < 1) {
  cP = 1;
 }
 var trPg = $("trPager");
 var newCellOne = trPg.insertCell(0);
 newCellOne.width = "20%";
 var newCellTwo = trPg.insertCell(1);
 newCellTwo.width = "15%";
 newCellTwo.id = "pgSummary";
 var newCellThree = trPg.insertCell(2);
 newCellThree.width = "45%";
 newCellThree.id = "pgNumContext";
 var newCellFour = trPg.insertCell(3)
 newCellFour.width = "20%";
 newCellTwo.innerHTML = " " + tN + "   " + cP + "/" + tP + " ";
 var pageHtml = " トップページ";
 pageHtml += " ページ";
 for (var i = 1; i < tP + 1; i++) {
  var numColor = "";
  if (i == 1) numColor = "red";
  pageHtml += "" + i + "";
 }
 pageHtml += " のページ";
 pageHtml += "  ページ";
 pageHtml += "  " +
   "";
 newCellThree.innerHTML = pageHtml;
}
//       js  ,                     
function forward() {
 if (!(/^([-]){0,1}([0-9]){1,}$/.test($("pgNumber").value))) {
  //        
  alert("        !");
  $("pgNumber").focus();
  $("pgNumber").select();
 }
 else
  gotoPage($("pgNumber").value);
}
//                
function handleEnterOnPgNumber() {
 if (event.keyCode == 13) {
  forward();
  return false;
 }
 return true;
}
function gotoPage(oNum) { //      0      (  ,  )
 if (oNum > 0) {
  var totalNumber = parseInt($("totalNum").innerText); //    
  var curPgNumber = parseInt($("cuPgNumber").innerText);
  var totalPgNumber = parseInt($("sumPgNumber").innerText); //    
  if (parseInt(oNum) <= totalPgNumber) {
   chgPages(oNum, totalPgNumber, totalNumber);
   for (var k = 1; k < totalPgNumber + 1; k++) {
    $("numPg" + k).style.color = "";
   }
   $("numPg" + oNum).style.color = "red";
   getPagerInfo(oNum);
  }
  else {
   alert("        !");
   $("pgNumber").focus();
   $("pgNumber").select();
   return;
  }
 }
}
function goToNextPrev(oNum) {
 var addNum = parseInt(oNum);
 var totalNumber = parseInt($("totalNum").innerText); //    
 var curPgNumber = parseInt($("cuPgNumber").innerText);
 var totalPgNumber = parseInt($("sumPgNumber").innerText); //    
 //         ,          //          ,         
 if ((curPgNumber + addNum) > 0 && (curPgNumber + addNum) <= totalPgNumber) {
  chgPages(parseInt(curPgNumber + addNum), totalPgNumber, totalNumber);
  for (var k = 1; k < totalPgNumber + 1; k++) {
   $("numPg" + k).style.color = "";
  }
  $("numPg" + parseInt(curPgNumber + addNum)).style.color = "red";
  getPagerInfo(parseInt(curPgNumber + addNum));
 }
}
function chgPages(cuPg, toPg, tNum) {
 // $("totalNum").innerHTML = tNum;
 $("cuPgNumber").innerHTML = cuPg;
 //$("sumPgNumber").innerHTML = toPg;
}
function getPagerInfo(oNum) { //     ,    
 var xmlReq = createXMLHTTP();
 xmlReq.open("post", "/AjaxOperations.aspx?pgNumber=" + oNum, true);
 xmlReq.onreadystatechange = function() {
  if (xmlReq.readyState == 4) {
   if (xmlReq.status == 200) {
    //xmlReq.responseText         
    $("tbTest").innerHTML = xmlReq.responseText;
   }
   else {
    $("tbTest").innerHTML = "       ,   ...";
    //alert("Connect the server failed!");
   }
  }
 }
 xmlReq.send(null);
}


Default.aspx:





 
 
 


 </code></pre> 1ページ<br>Default.aspx.cs: <pre><code>
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebTest2008
{
 public partial class Default : System.Web.UI.Page
 {
  protected void Page_Load(object sender, EventArgs e)
  {
  }
 }
}

</code></pre>  <br>AjaxOperations.aspx:  <pre><code>
    
 </code></pre>  <br>AjaxOperations.aspx.cs: <pre><code>
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebTest2008
{
 public partial class AjaxOperations : System.Web.UI.Page
 {
  protected void Page_Load(object sender, EventArgs e)
  {
   if (!string.IsNullOrEmpty(Request["pgNumber"]))
   {
    //int pgNum = Convert.ToInt32(Request["pgNumber"]);
    Response.Write(" " + Request["pgNumber"] + " ");
   }
  }
 }
}

</code></pre>  <br>Okは、 のマシン(vs 2008)でテストに  し、  なajaxページング  が  しました.<br>  で べたajaxプログラム  に  つことを っています.</disable_script></code></pre><div class="container-fluid p-0">

                                    <script async
                                            src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-3219747971601011"
                                            crossorigin="anonymous"></script>
                                    <!-- jpdebug-blog-source-div -->
                                    <ins class="adsbygoogle"
                                         style="display:block"
                                         data-ad-client="ca-pub-3219747971601011"
                                         data-ad-slot="3872614099"
                                         data-ad-format="auto"
                                         data-full-width-responsive="true"></ins>
                                    <script>
                                        (adsbygoogle = window.adsbygoogle || []).push({});
                                    </script>
                                </div>

                                


                            </div>

                            <div class="row mb-4  pt-4 pb-4"><div class="col-6 text-primary ">
                                    <a class=" btn btn-outline-dark  small  float-left  "
                                       href="/p/2195208">
                                        <svg xmlns="http://www.w3.org/2000/svg" width="1em" height="1em"
                                             viewBox="0 0 24 24"
                                             fill="none" stroke="currentColor" stroke-width="2"
                                             stroke-linecap="round"
                                             stroke-linejoin="round" class="feather feather-arrow-left">
                                            <line x1="19" y1="12" x2="5" y2="12"></line>
                                            <polyline points="12 19 5 12 12 5"></polyline>
                                        </svg>

                                        eclipse構成windDDK環境書き込みコードコンパイル</a>
                                </div><div class="col-6  text-primary ">
                                    <a class=" btn btn-outline-dark small float-right "
                                       href="/p/2195216">beegoフレームワーク学習フィルタ(restfulリクエストの実装)
                                        <svg xmlns="http://www.w3.org/2000/svg" width="1em" height="1em"
                                             viewBox="0 0 24 24"
                                             fill="none" stroke="currentColor" stroke-width="2"
                                             stroke-linecap="round"
                                             stroke-linejoin="round" class="feather feather-arrow-right">
                                            <line x1="5" y1="12" x2="19" y2="12"></line>
                                            <polyline points="12 5 19 12 12 19"></polyline>
                                        </svg>


                                    </a>
                                </div></div>


                        </article>


                    </div>

                    <!-- end row -->
                </div> <!-- end card-body -->
            </div> <!-- end card -->
        </div>
        <!-- end chat area-->

        <!-- start user detail -->
        <div class="col-12 col-xxl-3 col-xl-3 ">

            <div class="card sticky-top" style="top: 20px;">
                <div class="row">


                    <aside class="col-md-12 col-xxl-12 d-none d-md-block   pt-0">
                        <div class="sticky">

                            <div class="container-fluid p-0 m-0 bg-light-off ">
                                <script async
                                        src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-3219747971601011"
                                        crossorigin="anonymous"></script>
                                <!-- jpdebug-right-top-1 -->
                                <ins class="adsbygoogle"
                                     style="display:inline-block;width:300px;height:600px"
                                     data-ad-client="ca-pub-3219747971601011"
                                     data-ad-slot="1251860546"></ins>
                                <script>
                                    (adsbygoogle = window.adsbygoogle || []).push({});
                                </script>
                            </div>


                        </div>

                    </aside>

                </div>
                <div class="card-body">


                    <div class="row ">


                        <aside class="col-md-12 col-xxl-12 d-none d-md-block  pt-3">
                            <div class="sticky"><ul id="toc" class="nav flex-column toc overflow-scroll">

                                    <h4 class="nav-link h4 relate_nav_title text-dark" disabled>関連内容</h4><li class="nav-item">
                                        <a data-scroll class="nav-link text-black-50"
                                           href="/p/3508931">Azure App Service で反応する</a>
                                    </li><li class="nav-item">
                                        <a data-scroll class="nav-link text-black-50"
                                           href="/p/3508928">最近の ssd で docker wsl2 を使用</a>
                                    </li><li class="nav-item">
                                        <a data-scroll class="nav-link text-black-50"
                                           href="/p/3508925">Función de carpeta de datos de la aplicación Drive Kit con 3 lineas de código</a>
                                    </li><li class="nav-item">
                                        <a data-scroll class="nav-link text-black-50"
                                           href="/p/3508918">Padrão State em Python</a>
                                    </li><li class="nav-item">
                                        <a data-scroll class="nav-link text-black-50"
                                           href="/p/3508909">Rails のページネーションと検索機能 - Gem なし</a>
                                    </li><li class="nav-item">
                                        <a data-scroll class="nav-link text-black-50"
                                           href="/p/3508908">【Vulkan】シェーダリフレクションで手動バインディングから解放されたい</a>
                                    </li><li class="nav-item">
                                        <a data-scroll class="nav-link text-black-50"
                                           href="/p/3508907">MacでUbuntu serverを立てるのにmultipassを使ってみた</a>
                                    </li><li class="nav-item">
                                        <a data-scroll class="nav-link text-black-50"
                                           href="/p/3508906">初心者向けNestJS(Typescript)バックエンド開発講座</a>
                                    </li><li class="nav-item">
                                        <a data-scroll class="nav-link text-black-50"
                                           href="/p/3508905">WindowsのHyper-Vでminikubeの基本的な使い方</a>
                                    </li></ul><!--                                <div class="container-fluid p-0">-->
<!--                                    <script async-->
<!--                                            src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-3219747971601011"-->
<!--                                            crossorigin="anonymous"></script>-->
<!--                                    <!– jpdebug-post-right –>-->
<!--                                    <ins class="adsbygoogle"-->
<!--                                         style="display:block"-->
<!--                                         data-ad-client="ca-pub-3219747971601011"-->
<!--                                         data-ad-slot="8535334719"-->
<!--                                         data-ad-format="auto"-->
<!--                                         data-full-width-responsive="true"></ins>-->
<!--                                    <script>-->
<!--                                        (adsbygoogle = window.adsbygoogle || []).push({});-->
<!--                                    </script>-->
<!--                                </div>-->


                            </div>

                        </aside>
                    </div>
                </div> <!-- end card-body -->
            </div> <!-- end card-->
        </div> <!-- end col -->
        <!-- end user detail -->
    </div>


</div><!-- footer -->
<footer class="bg-light p-3 mt-5">
    <div class="container">
        <div class="row gutter-3">

            <div class="col-12 col-md-12 text-white text-center text-lowercase">
                <p class="mb-0 text-black-50 small text-lowercase">©2022 jpdebug.com. All Rights Reserved. | <a
                        href="/privacy" target="_blank" class="text-dark" rel="nofollow">Privacy Policy</a> | <a
                        href="/contact_us"
                        target="_blank"
                        class="text-dark" rel="nofollow">Contact
                    US</a> | <a href="/sitemap.xml" target="_blank" class="text-dark" rel="nofollow">Sitemap</a></p>

            </div>

        </div>
    </div>
</footer>
<!-- / footer -->

<!-- Bootstrap 4 Version -->
<div class="nk-cookie-banner alert alert-secondary text-center mb-0 small" role="alert"
     style="background: rgba(255, 255, 255, .6);border-color:#f2f2f2;-webkit-backdrop-filter: blur(10px);backdrop-filter: blur(20px);">
    🍪このウェブサイトは、あなたが我々のウェブサイトで最高の経験を得ることを確実とするために、クッキーを使います。
    <a href="/privacy" target="blank" rel="nofollow">プライバシー条項の表示</a>
    <button type="button" class="btn btn-primary btn-sm ml-3 shadow-none" onclick="window.nk_hideCookieBanner()">
        同意します
    </button>
</div><!-- JS Global Compulsory hs-builder:build-delete -->
<script src="https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.min.js"></script>


<script src="http://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.2.0/highlight.min.js"></script>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>

<script src="https://cdn.jsdelivr.net/npm/clipboard@2.0.8/dist/clipboard.min.js"></script>

<script src="/static/all_theme_package/quick-website.min.js"></script>

<script src="/static/all_theme_package/cookie-banner.js"></script><script>
    var _hmt = _hmt || [];
    (function () {
        var hm = document.createElement("script");
        hm.src = "https://hm.baidu.com/hm.js?d95bbf01f0dd90da512ecedf1d9661bd";
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(hm, s);
    })();
</script>

<script async src="//static.getclicky.com/101379076.js"></script>

<!--<script>-->
<!--    $(document).ready(function () {-->

<!--        $("pre").addClass("language-none");-->

<!--    });-->
<!--</script>-->
<script src="/static/all_theme_package/auto-enable-prism.js"></script>
<script src="/static/all_theme_package/prism.min.js"></script></body>

</html><script src="/cdn-cgi/scripts/7d0fa10a/cloudflare-static/rocket-loader.min.js" data-cf-settings="1f354fbe5fa3ce294417e1ae-|49" defer></script>