一個Ajax.NET的查詢執行個體

來源:互聯網
上載者:User

因為項目中只能用.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"]);

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.