一個Ajax.NET的查詢實例

ajax

因為項目中只能用.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
{
    /**//// <summary>
    /// index 的摘要描述。
    /// </summary>
    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);
        }
       
        /**//// <summary>
        /// 此為設計工具支援所必須的方法 - 請勿使用程式碼編輯器修改
        /// 這個方法的內容。
        /// </summary>
        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:
<script language="javascript">       
var obj=document.all;
var frm=document.forms[0];
var box=document.getElementById("detail");
var list=document.getElementById("datagrid");

if(typeof ASP == "undefined") ASP={};
ASP.LoadingDemo_class = function() {};
Object.extend(ASP.LoadingDemo_class.prototype, Object.extend(new AjaxPro.AjaxClass(), {
    LongOperation: function() {
        return this.invoke("LongOperation", {}, this.LongOperation.getArguments().slice(0));
    },
    url: '/ajaxpro/ASP.LoadingDemo,App_Web_t_mu_tej.ashx'
}));
ASP.LoadingDemo = new ASP.LoadingDemo_class();

var c = 0;
AjaxPro.onLoading = function(b) {
    c++;
    window.status = c;
    var l = document.getElementById("loadinfo");
    l.style.visibility = b ? "visible" : "hidden";
       l.style.right=0;
    l.style.pixelTop=document.body.scrollTop;
}

function showDetail(n) {
    c = 0;
    ASP.LoadingDemo.LongOperation(new Function('getEolByeFormsn('+n+')'));
}

function getDataGrid(sql,Event)
{
    if(list.style.display=='none')
    {
        list.style.display='';
    }
    box.style.display=='' ? box.style.display='none':void(0);
    var p=eol.index.AjaxData(sql, Event).value;
    list.innerHTML=p;
}

function getEolByeFormsn(n)
{
    if (box.style.display=="none")
    {
        box.style.display="";
        box.innerHTML="<div id='toolbar'><div id='btnGoback' ><img src=cch.gif>GO BACK</div></div>"
    }
    list.style.display="none";
    var p=eol.index.AjaxData("select * from msi_eol_list_line t where eformsn="+n, false).value;
    box.innerHTML+=p;
}

window.onload=function ()
{
    box.style.display="none";
    list.style.display="none";
    document.getElementById("btnQuery").onclick=whenQuery;
    document.getElementById("btnToexcel").style.visibility="hidden";
    document.getElementById("btnHelp").onclick=getHelp;
}
function goBack()
{
    box.style.display='none';
    list.style.display='';
}
function whenQuery()
{
    var type=document.getElementById("types");
    var err=time=param='';
    var sql="select distinct (select Count(eformsn) from query.dbo.msi_eol_list_line where eformsn=w.eformsn) Line, w.* from (select eFormsn, Cast(Apply_Date as datetime) as ApplyDate, Applicant, LOB, Cast(p_date as datetime) as PendingDate, 'process' as Status from webflow.dbo.tb_326_1 union select eformsn, Cast(apply_date as datetime), applicant, lob, Cast(p_date as datetime), 'over' as Status from webflowdata.dbo.tb_326_1 where flowendstatus='E') w inner join query.dbo.msi_eol_list_line as e on w.eformsn=e.eformsn where (w.eformsn=e.eformsn) ";
    switch (type.value)
    {
        case "eformsn":
            if (frm.eformsn.value!='')
            {
                param="and e.eformsn='"+frm.eformsn.value+"'";
            }else{
                err="請輸入申請單號.!"
            }
            break;
        case "applytime":
            if (frm.startTime.value=='' && frm.endTime.value=='')
            {
                err="請輸入申請起止時間";
            }
            break;
        case "applicant":
            if (frm.applicant.value!="")
            {
                param+="and e.applicant='"+frm.applicant.value+"' order by w.ApplyDate";
            }else{
                err='請輸入申請者中文名';
            }
            break;
        case "lob":
            if (frm.lob.value!="")
            {
                param="and w.lob='"+frm.lob.value+"'";           
            }else{
                err="請輸入LOB";
            }
            break;
        case "pendingtime":
            if (frm.pStartTime.value!="" && frm.pEndTime.value!=""){
                param=" and w.PendingDate between cast('"+frm.pStartTime.value+"' as datetime) and cast('"+frm.pEndTime.value+"' as datetime) order by w.PendingDate";
            }else{
                err="請輸入Pending Time";
            }
            break;
        case "model":
            if (frm.model.value!=''){
                frm.startTime.value=frm.endTime.value='';
                sql='';
                param="select distinct (select count(model) from query.dbo.msi_eol_list_line where model=e.model) Total, eFormsn, Model, Applicant, LOB, Cast(Creation_date as datetime) as ApplyDate, Cast(pending_date as datetime) as PendingDate, (select Status from (select eFormsn, 'process' as Status from webflow.dbo.tb_326_1 union select eformsn, 'over' as Status from webflowdata.dbo.tb_326_1 where flowendstatus='E') as w where w.eformsn=e.eformsn) as Status from query.dbo.msi_eol_list_line as e where e.model='"+frm.model.value+"' group by Model, Applicant, LOB, Creation_date, Pending_Date, eformsn";
            }else{
                err="請輸入Model";
            }
            break;
    }
    if (err=='')
    {
        if (frm.startTime.value!='' && frm.endTime.value!='')
        {
            time="and ApplyDate between Cast('"+frm.startTime.value+"' as datetime) and Cast('"+frm.endTime.value+"' as datetime) ";
        }

        sql+=time+param;
        //document.write(sql);
        c = 0;
        ASP.LoadingDemo.LongOperation(new Function('getDataGrid("'+sql+'",true)'));
    }else{
        alert(err);
    }
}

var oPopup = window.createPopup();
function getHelp()
{
  var oPopupBody = oPopup.document.body;
  var lefter = screen.width-150;
  var topper = document.body.scrollTop;
  oPopupBody.innerHTML = helpbox.innerHTML;
  oPopup.show(lefter, topper, 280, document.body.offsetHeight, document.body);
  document.body.onmouseup = new Function("oPopup.hide();");
}
</script>

http://floatping.cnblogs.com/archive/2006/07/07/445225.html

时间: 2024-10-07 10:25:13

一個Ajax.NET的查詢實例的相关文章

這樣的查詢如何實現

问题描述 SQL查詢,如何實現查詢的前一條記錄中的某一列的值為下一條查詢記錄某一列的值例如序號,投入量,產出,良率11002020%2201818*100/203181515*100/18...其中投入量,產出都是統計量(在其他表中以一定條件查詢Count得到)請教此查詢該如何實現 解决方案 解决方案二:你的意思是第二条记录中投入量20=第一条记录中的产出,然后第三条记录中投入量18=第二条记录的产出吗?解决方案三:select*fromtable1where产出exist(selectcoun

多重條件組合查詢(一)

平常我們在開發系統時,一定不會缺少查詢弁?然而往往算個條件是無法滿足用戶的要求的,這時我們就應該設置多重條件進行組合,讓用戶查詢時以更加方便地選擇查詢條件,迅速找到他們所滿足的記錄.  現在我們假設開發一個鐘表公司的客戶訂單系統,有客戶訂單編號,類別,名稱,數量,价格等資料,這里就不將資料一一列擊,.訂單表如下:  訂單編號(ID) 類別(TYPE)名稱(NAME)數量(QTY) 价格(PRICE)  001 机芯 机芯1 100 150  001 表殼 表殼1 1000 60  001 表帶

單表多字段查詢問題

问题描述 假如有一個表,可以通過書名,作者,出版社三個字段查詢,如何實現既可以通過其中任一一個字段查詢,又可以兩兩組合查詢,甚至三者一起組合查詢. 解决方案 解决方案二:你可以在代码中先判断三者的值是否为空if书名<>''thensql="select*from表where书名=''";if作者<>''thensql=sql+"and作者=''";if出版社<>''thensql=sql+"and出版社=''"

多重條件組合查詢(一)_php基础

平常我們在開發系統時,一定不會缺少查詢弁?然而往往算個條件是無法滿足用戶的要求的,這時我們就應該設置多重條件進行組合,讓用戶查詢時以更加方便地選擇查詢條件,迅速找到他們所滿足的記錄.  現在我們假設開發一個鐘表公司的客戶訂單系統,有客戶訂單編號,類別,名稱,數量,价格等資料,這里就不將資料一一列擊,.訂單表如下:  訂單編號(ID) 類別(TYPE)名稱(NAME)數量(QTY) 价格(PRICE)  001 机芯 机芯1 100 150  001 表殼 表殼1 1000 60  001 表帶

asp.net 復雜sql查詢

问题描述 有3個數據庫DB_A,DB_B,DB_C,分別有相同結構的表A表A有字段:a員工序號,b薪資序號,c加減代號,d金額條件:比較DB_A,DB_B表A金額匯總的大小,然后將金額大的記錄導入到DB_C的表A例如:DB_A:表A:1為加項2,3為減項YG_01011100YG_01031100YG_0102220例如:DB_B:表A:YG_01011500YG_01033200YG_01051300DB_A金額為:180,DB_B金額:600,結果就是將DB_B的三條記錄導入DB_C的表A中

mysql-執行階段錯誤&amp;amp;#39;13&amp;amp;#39; 型態不符合 ----VBA 連接MYSQL多表查詢

问题描述 執行階段錯誤'13' 型態不符合 ----VBA 連接MYSQL多表查詢 ** 請教各位高手: 我在VBA中如下連接mysql單表查詢沒問題,有數據出來. ** .CommandText = Array( _ "select " _ & "proc_hist_0.prod_pn , proc_hist_0.LOT_NO, proc_hist_0.proc_ID, proc_hist_0.IN_TIME, proc_hist_0.IN_QTY, proc_hi

多重條件組合查詢(二)

接一來這部分是實現用戶提交之后的查詢結果,也是程序代碼的主体部份:  <?  $linkstr=mysql_connect("localhost","root","sa");  mysql_select_db("cx",$linkstr);  $showstr="查詢條件為:";  $querystring="select no,type,name,qty,price from orders

rails資料庫長時間查詢,導致無法接受request?

问题描述 今天在寫報表產生功能的時候發現 報表產生中的這段時間rails server無法繼續接受request我開了另一個瀏覽器 卻無法request到網頁一定要等到報表產生完後 才可以進入網頁!即使關掉瀏覽器也一樣得等資料庫比對查詢完才行自己推斷可能是下面這個原因..之前資料庫資料較少 我沒有遇過這個問題由於 rails處理client的request是單線程的處理完才能接受下一個request當資料庫需大筆比對查詢大料資料的時候 (假設要花十分鐘!)rails的server無法接受另一個r

多重條件組合查詢(二)_php基础

接一來這部分是實現用戶提交之后的查詢結果,也是程序代碼的主体部份:  <?  $linkstr=mysql_connect("localhost","root","sa");  mysql_select_db("cx",$linkstr);  $showstr="查詢條件為:";  $querystring="select no,type,name,qty,price from orders