手寫AJAX實現GridView無重新整理分頁

來源:互聯網
上載者:User

AJAXTOOLKIT用起來雖然很方便,但有時會出點問題,我遇到的問題就是使用了AJAXTOOLKIT裡面的控制項的頁面有時會出現重新整理失敗,或者該頁面內的GRIDVIEW裡面的按鈕點擊了有時沒反應,有時又有反應。在網上搜尋原來是AJAXTOOLKIT有BUG,有些人也或多或少遇到過一些類似的問題。因此我決定自己手寫AJAX代碼來實現頁面的無重新整理效果,這個過程中實現GridView無重新整理分頁是重點也很有代表性。下面給出實現過程。

後台代碼如下:

 //要將GRIDVIEW轉換成HTML必須實重載下面VerifyRenderingInServerForm,方法體內為空白就行了。

public override void VerifyRenderingInServerForm(Control control)
    {
        //base.VerifyRenderingInServerForm (control);
    }

 protected void Page_Load(object sender, EventArgs e)
    {

//首次訪問頁面顯示GRIDVIEW裡面的第一頁資料

        if (!IsPostBack && Request.QueryString.Count== 0)
        {
            Session["page"] = "1";

            MySqlConnection mcon = new MySqlConnection("server=localhost;database=test;uid=root;pwd=123");
            mcon.Open();
            MySqlCommand mcom = new MySqlCommand("select * from test limit " + 0 + "," + "100", mcon);
            MySqlDataAdapter ma = new MySqlDataAdapter(mcom);
            DataSet ds = new DataSet();
            ma.Fill(ds);
            GridView1.DataSource = ds.Tables[0];
           
           
           
            DataBind();
            mcon.Close();
            mcon.Dispose();
            mcom.Dispose();
            ma.Dispose();
            ds.Dispose();
        }

//回傳的時候判斷是下一頁還是上一頁顯示新頁面的資料
        else
        {
          

            if (Request.QueryString.Count > 0)
            {
                int page = Int32.Parse(Session["page"].ToString());

                if (Request.QueryString["id"].Contains("next"))//"NEXT"表示下一頁
                {

                    page++;

                }
                else if (Request.QueryString["id"].Contains("pre"))//“PRE”為上一頁
                {
                    page--;
                }
                    Session["page"] = page;
                    int beginNum=(page-1)*100;
                    MySqlConnection mcon = new MySqlConnection("server=localhost;database=test;uid=root;pwd=123");
                    mcon.Open();
                    MySqlCommand mcom = new MySqlCommand("select * from test limit "+beginNum+","+"100", mcon);
                    MySqlDataAdapter ma = new MySqlDataAdapter(mcom);
                    DataSet ds = new DataSet();
                    ma.Fill(ds);
                    GridView1.DataSource = ds.Tables[0];
                    DataBind();

//重點在下面,gridview通過STRINGWRITER將其html內容輸出到HTML輸出資料流裡去

                    System.IO.StringWriter stringWrite = new StringWriter();
                    System.Web.UI.HtmlTextWriter writer = new System.Web.UI.HtmlTextWriter(stringWrite);
                  
                  
                 

                    GridView1.RenderControl(writer);
                   String Result = stringWrite.ToString();

                    Response.Write(Result);
                  
                    Response.End();//這個地方表示輸出的內容在這裡結束,將之前的內容發送到輸出資料流,因此輸出結果只有

//GRIDVIEW的HMTL,如果省略RESPONSE.END()。將輸出怎個頁面的HMTL代碼,在瀏覽器端還要重新截取

//GRIDVIEW 的HTML代碼,不方便,效率也不高,因為只需要更新GRIDVIEW的內容
                    DataBind();
                    mcon.Close();
                    mcon.Dispose();
                    mcom.Dispose();
                    ma.Dispose();
                    ds.Dispose();
               
            }
        }
     
    }

 

瀏覽器代碼的ajax就很簡單了,將GRIDVIEW所在DIV的INNERHTML替換成傳回的的值就行了。

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.