前台代码:
<%@ 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>
<fieldset>
<legend>UpdatePanel</legend>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
<asp:UpdateProgress ID="UpdateProgress1" runat="server">
<ProgressTemplate>
正在加载数据.............
</ProgressTemplate>
</asp:UpdateProgress>
<asp:GridView ID="GridView1" runat="server"
onrowcreated="GridView1_RowCreated"
onpageindexchanging="GridView1_PageIndexChanging" Width="1000px"
onrowdatabound="GridView1_RowDataBound"
EmptyDataText="没有查询到您所需要的数据" PageSize="15" AllowPaging="True" >
<FooterStyle CssClass="GridViewFooterStyle" />
<RowStyle CssClass="GridViewRowStyle" />
<SelectedRowStyle CssClass="GridViewSelectedRowStyle" />
<PagerStyle CssClass="GridViewPagerStyle" />
<AlternatingRowStyle CssClass="GridViewAlternatingRowStyle" />
<HeaderStyle CssClass="GridViewHeaderStyle" />
</asp:GridView>
</fieldset>
</ContentTemplate>
</asp:UpdatePanel>
</div> </form> </body> </html>
后台cs代码:
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(); } }