zoukankan      html  css  js  c++  java
  • Asp.Net异步数据绑定

    前台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 id="form1" runat="server">
        <div>
            <asp:GridView ID="Output" BackColor="#ffffff" BorderColor="#999999" runat="server"
             BorderStyle="none" BorderWidth="1px" CellPadding="3" GridLines="vertical" Width="100%" AutoGenerateColumns="False" OnRowDataBound="Output_RowDataBound">
                <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
                <RowStyle BackColor="#EEEEEE" ForeColor="Black" />
                <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
                <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
                <AlternatingRowStyle BackColor="#efefef" />
                <Columns>
                    <asp:TemplateField HeaderText="ID">
                        <ItemTemplate>
                            <asp:Label ID="lblID" runat="server" Font-Size="12px"></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简单的异步数据绑定操作。

  • 相关阅读:
    趣味算法:国王和100个囚犯(据说是腾讯的面试题)
    限制文本框的输入(兼容FF,IE)
    切换家里和公司网络的脚本
    onchange,onpropertychange,oninput的使用
    正则匹配html标记
    javascript清空数组的三种方法
    正则查找重复
    (正则)提取页面里的img标签
    javascript类型检测(转载)
    查看sqlserver信息
  • 原文地址:https://www.cnblogs.com/VirtualMJ/p/757416.html
Copyright © 2011-2022 走看看