一?ajax.net的查??例_ajax教程

网络整理 - 07-26

因??目中只能用.net 1.1,所以atlas是用不了了,只能玩玩AjaxProfession.net, 下面是一?查?的案例。 在?面上放置一?DataGrid1,各?查??果均放入其中,在client端的JS中?SQL?句??server端的方法,?行一?查?,?能控制??行是否展?一?新的查?。可?DataGrid1的?容?出到excel中、模?gmail的Ajax的loading?用

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;
namespace eol
{
    /**////
    /// index 的摘要描述。
    ///
    public class index : System.Web.UI.Page
    {
        protected System.Web.UI.WebControls.TextBox TextBox1;
        protected WebControlLibrary.InputCalendar startTime;
        protected WebControlLibrary.InputCalendar endTime;
        protected WebControlLibrary.InputCalendar pStartTime;
        protected WebControlLibrary.InputCalendar pEndTime;
        protected System.Data.SqlClient.SqlConnection Conn=new SqlConnection();
        protected System.Web.UI.WebControls.TextBox eformsn;
        protected System.Web.UI.WebControls.Label Label1;
        protected System.Web.UI.WebControls.DataGrid DataGrid1=new DataGrid();
        protected System.Web.UI.WebControls.Button Button2;
        protected System.Web.UI.HtmlControls.HtmlInputText applicant;
        public string toExcel;
   
        private void Page_Load(object sender, System.EventArgs e)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(eol.index));
        }

        public DataSet DB(string dataName, string sqlCmd)
        {
            string connStr="server=172.16.0.120;uid=msikdb;pwd=dbmsik;database="+dataName;
            Conn.ConnectionString=connStr;
            try
            {
                Conn.Open();
            }
            catch(Exception ex)
            {
                Conn.Close();
                Response.Write(ex.Message);
            }
            SqlCommand cmd=new SqlCommand(sqlCmd, Conn);
            SqlDataAdapter da=new SqlDataAdapter();
            da.SelectCommand=cmd;
            DataSet ds = new DataSet();
            da.Fill(ds);
            Conn.Close();
            return ds;
        }

        Web Form ??工具?生的程式?#region Web Form ??工具?生的程式?
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: 此? ASP.NET Web Form ??工具所需的呼叫。
            //
            InitializeComponent();
            base.OnInit(e);
        }
       
        /**////
        /// 此???工具支援所必?的方法 - ?勿使用程式???器修改
        /// ??方法的?容。
        ///
        private void InitializeComponent()
        {   
            this.Conn = new System.Data.SqlClient.SqlConnection();
            this.Button2.Click += new System.EventHandler(this.Button2_Click);
            this.DataGrid1.ItemCreated += new System.Web.UI.WebControls.DataGridItemEventHandler(this.getPageIndexStyle);
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion


        private void getPageIndexStyle(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
        {
           
            if(e.Item.ItemType == ListItemType.Pager)
            {
                e.Item.Cells[0].Text="Total Row:"+this.DataGrid1.Items.Count.ToString();
            }
        }
       
        [AjaxPro.AjaxMethod]
        public string AjaxData(string sSql, bool clientEvent)
        {
            if (clientEvent)
            {
                DataGrid1.ItemDataBound+=new DataGridItemEventHandler(DataGrid1_ItemDataBound);
            }
//            DataGrid1.AllowPaging=true;
//            DataGrid1.PagerStyle.Position=PagerPosition.Top;
//            DataGrid1.PagerStyle.Mode=PagerMode.NumericPages;
//            DataGrid1.ItemCreated+=new DataGridItemEventHandler(getPageIndexStyle);
            DataGrid1.DataSource=DB("query",sSql);
            DataGrid1.HeaderStyle.BackColor=System.Drawing.Color.Blue;
            DataGrid1.Width=Unit.Percentage(100);
            DataGrid1.HeaderStyle.ForeColor=System.Drawing.Color.White;
            DataGrid1.DataBind();
           
           
            System.Text.StringBuilder sb=new System.Text.StringBuilder();
            System.IO.StringWriter sw=new System.IO.StringWriter(sb);
            System.Web.UI.HtmlTextWriter htw=new HtmlTextWriter(sw);
            this.DataGrid1.RenderControl(htw);
            string s=sb.ToString().Replace(" 上午 12:00:00", "");
            this.toExcel=s;
            Session["txtExcel"]=s;
            return s;
        }
       
        public void setToExcel()
        {
            //下面?行非常重要,attachment ??表示作?附件下?,你能改成online在?打?
            //filename=FileFlow.xls 指定?出文件的名?,注意其?展名和指定文件?型相符,能?:.doc    .xls    .txt  .htm  
            Response.AppendHeader("Content-Disposition","attachment;filename=EOL_"+eformsn.Text+".xls");
            Response.ContentEncoding=System.Text.Encoding.GetEncoding("big5"); 
            //Response.ContentType指定文件?型 能?application/ms-excel,application/ms-word ,application/ms-txt,application/ms-html或其他??器可直接支持文? 
            Response.ContentType = "application/ms-excel";

            Response.Write(this.toExcel);
            Response.Write(Session["txtExcel"]);
            Session.Abandon();
            Response.End();
        }

        private void Button2_Click(object sender, System.EventArgs e)
        {
            setToExcel();
        }

        private void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            ListItemType itemtype=(ListItemType)e.Item.ItemType;
            if (itemtype!=ListItemType.Header && itemtype!=ListItemType.Footer && itemtype!=ListItemType.Separator)
            {
                e.Item.Attributes.Add("ondblclick", "showDetail("+e.Item.Cells[1].Text+");");
                e.Item.Attributes.Add("onmouseover","this.className=mouseOn");
                e.Item.Attributes.Add("onmouseout", "this.className=");
            }
        }

    }
}
Client端的js: