1. 程式人生 > >asp.net gridview實現正在載入效果方案一

asp.net gridview實現正在載入效果方案一

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
  
</head>
<body>
     <form id="form1" runat="server">
    <div>
       <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>    
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
        <asp:Label ID="Label2" runat="server" Text="請輸入使用者名稱"></asp:Label><input type="text" id="text2" /><asp:Button
            ID="Button2" runat="server" Text="查詢" onclick="Button1_Click" />
         <asp:UpdateProgress ID="UpdateProgress1" runat="server">
        <ProgressTemplate>
        正在載入資料.............
        </ProgressTemplate>
        </asp:UpdateProgress>
            <asp:GridView ID="GridView1" runat="server" Width="347px" AllowPaging="True" 
                AutoGenerateColumns="False" PageSize="2" 
                onpageindexchanging="GridView1_PageIndexChanging">
                <Columns>
                    <asp:BoundField HeaderText="編號" DataField="orderid"/>
                    <asp:BoundField HeaderText="使用者名稱" DataField="customerid"/>
                    <asp:BoundField HeaderText="密碼"  DataField="shipvia"/>
                    <asp:BoundField HeaderText="類別"  DataField="shipname"/>
                </Columns>
            </asp:GridView>
        </ContentTemplate>        
            <Triggers>
                <asp:AsyncPostBackTrigger ControlID="Button2" EventName="Click" />
            </Triggers>
        </asp:UpdatePanel>
    </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 System.Data.SqlClient;
using System.Data;

public partial class _Default : System.Web.UI.Page
{
   
    

    protected void Page_Load(object sender, EventArgs e)
    {
        
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        System.Threading.Thread.Sleep(3000);
        databind();
    }
    public void databind()
    {
        string sql = "SELECT [orderid], [customerid], [shipvia], [shipname] FROM [orders]";
        string con = "Data Source=.;Initial Catalog=northwind;User ID=sa;Password=19790601";
        SqlConnection mycon = new SqlConnection(con);
        mycon.Open();
        SqlDataAdapter mydt = new SqlDataAdapter(sql, mycon);
        DataSet ds = new DataSet();
        mydt.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
        mycon.Close();
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        databind();
    }
}