前台aspx页面:
<%@ Page Language="C#" Async="true" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
</head>
<body>
    <form ></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </div>
    </form>
</body>
</html>

后台cs文件:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;


// 异步数据绑定
public partial class _Default : System.Web.UI.Page
{
    private SqlConnection _connection;
    private SqlCommand _command;
    private SqlDataReader _reader;
    bool isBind = false;
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            this.PreRenderComplete += new EventHandler(_Default_PreRenderComplete);
            //this.PreRenderComplete += new EventHandler(Page_PreRenderComplete);
            AddOnPreRenderCompleteAsync(
                new BeginEventHandler(BeginAsyncOperation),
                new EndEventHandler(EndAsyncOperation)
            );
        }
    }

    void _Default_PreRenderComplete(object sender, EventArgs e)
    {
        if (isBind == false)
        {
            Output.DataSource = _reader;
            Output.DataBind();
            isBind = true;
        }
       
       //throw new Exception("The method or operation is not implemented.");
    }

    IAsyncResult BeginAsyncOperation(object sender, EventArgs e, AsyncCallback cb, object state)
    {
        string connection = ConfigurationManager.ConnectionStrings["PubsConnectionString"].ConnectionString.ToString();
        _connection = new SqlConnection(connection);


        _connection.Open();
        _command = new SqlCommand("select * from titles", _connection);
        return _command.BeginExecuteReader(cb, state);

    }

    public void EndAsyncOperation(IAsyncResult ar)
    {
        _reader = _command.EndExecuteReader(ar);
    }

    public void Page_PreRenderComplete(object sender, EventArgs e)
    {
        //throw new Exception("The method or operation is not implemented.");
        if (isBind == false)
        {
            Output.DataSource = _reader;
            Output.DataBind();
            isBind = true;
        }
       
    }

    public override void Dispose()
    {
        if (_connection != null)
            _connection.Close();
        base.Dispose();
    }

    protected void Output_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label lblID = (Label)e.Row.FindControl("lblID");
            lblID.Text = DataBinder.Eval(e.Row.DataItem, "title_id").ToString() ;
        }
    }
}

WebConfig配置
<connectionStrings>
  <add name="PubsConnectionString" connectionString="server=localhost;database=pubs;uid=sa;pwd=123;Asynchronous Processing =true" providerName="System.Data.SqlClient"/>

实现一个GridView简单的异步数据绑定操作。

相关文章: