2010年6月28日 星期一

Gridview 無資料時顯示標頭 header

//dt為DataTable判斷是否為0筆資料
if (dt.Rows.Count == 0)
{
dt.Columns[1].AllowDBNull = true;
dt.Rows.Add(dt.NewRow());
GridView1.DataSource = dt;
GridView1.DataBind();
int columnCount = GridView1.Rows[0].Cells.Count;
GridView1.Rows[0].Cells.Clear();
GridView1.Rows[0].Cells.Add(new TableCell());
GridView1.Rows[0].Cells[0].ColumnSpan = columnCount;
GridView1.Rows[0].Cells[0].Text = "無資料!";
GridView1.RowStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;
}
else
{
GridView1.DataSource = dt;
GridView1.DataBind();
}
參考至:http://blog.finalevil.com/2008/11/aspnetgridview01.html

2010年6月23日 星期三

使用 window.showModalDialog 無法彈出視窗問題

//showModalDialog_1.aspx 新增一個iframe

//按鈕內容
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
String filename = hidden2.Value + "_" + hidden1.Value + ".xls";
Response.Write("<script language=javascript>window.location.href='download.aspx?check=true&filename="+ filename +"';</script>");
}
//download.aspx 的page_load加上以下內容
//(必須判斷網頁傳值,不然畫面會無限postback)
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["check"] == "true")
{
String filename = Request.QueryString["filename"];
Response.Redirect("~\\E_mail\\attached\\" + filename);
}}

寫給自已看... showModalDialog 與 LinkButton doPostBack

function upload1() {
var cust_no = document.getElementById("Label10");
returnValue = window.showModalDialog("../E_upload/upload_1.aspx", cust_no.innerText, "dialogWidth=450px;dialogHeight=350px");
}
jQuery(document).ready(function() {
var tmp = window.dialogArguments;
//mail_no
var hidden1 = document.getElementById("hidden1");
//rfq_no
var hidden2 = document.getElementById("hidden2");
var tmparray = tmp.split(",");
var hidden3 = document.getElementById("hidden3");
hidden1.value = tmparray[0];
hidden2.value = tmparray[1];
if (hidden3.value != "load") {
hidden3.value = "load";
__doPostBack('LinkButton2', '');
}
});

DataTable 匯出excel

protected void export_excel(DataTable dt, String filename)
{
System.IO.StringWriter sw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(sw);
GridView gv = new GridView();
Response.Clear();
Response.AppendHeader("Content-Disposition", "attachment; filename= " + filename + ".xls");
Response.ContentType = "application/vnd.ms-excel";
Response.Charset = "utf-8";
//避免文數字問題(004 => 4),強制轉文字
Response.Write(" <style>");
Response.Write("td{mso-number-format:\"\\@\";}");
Response.Write("</style>");
gv.DataSource = dt;
gv.DataBind();
gv.RenderControl(hw);
Response.Write(sw.ToString());
Response.End();
}

ASP.NET (使用C#) 處理excel檔案

自動產生excel檔 並儲存到server
請先加入參考~
Microsoft.Office.Interop.Excel.Application oExcel = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook oBook = (Microsoft.Office.Interop.Excel.Workbook)oExcel.Workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
Microsoft.Office.Interop.Excel.Worksheet oSheet = oBook.Worksheets[1] as Microsoft.Office.Interop.Excel.Worksheet;
//Microsoft.Office.Interop.Excel.Range objExcel_RG = null;
object oMissing = System.Reflection.Missing.Value;
oBook = oExcel.ActiveWorkbook;
oSheet = (Microsoft.Office.Interop.Excel.Worksheet)oBook.Worksheets[1];
oSheet.Cells[1, 1] = "test";
if (System.IO.File.Exists(Server.MapPath("~\\E_mail\\attached\\abc.xls")) == true)
{
System.IO.File.Delete(Server.MapPath("~\\E_mail\\attached\\abc.xls"));
}
oBook.SaveAs(Server.MapPath("~\\E_mail\\attached\\abc.xls"), oMissing, oMissing, oMissing, oMissing, oMissing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlShared, oMissing, oMissing, oMissing,
oMissing,oMissing);
oSheet = null;
oBook = null;
oExcel.Quit();
oExcel = null;
GC.Collect();
非不得以採用此方法 此方法有可能造成EXCEL PROCESS 無法自動刪除問題存在

2010年6月22日 星期二

Delphi 6 與 ASP.NET 使用 Web Service 資料傳輸 (由ASP.NET 傳XML Delphi 6接收)

Delphi 6 引用ASP.NET的Web Service後呼叫test() function 回傳xml 注意事項: 1. DataSet ds 為要傳送給Delphi的資料 2. 將ds轉成xml後再傳送 3. 注意中文問題,要轉成BIG5 4. 假使Delhpi方面只能接收xml內容 請在表頭處強制加上即可中文顯示正常 5. Delphi編輯xml請使用XML Mapping Tool
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Transactions;
using System.Xml;
///

/// tls_export_test 的摘要描述
///
[WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] // 若要允許使用 ASP.NET AJAX 從指令碼呼叫此 Web 服務,請取消註解下一行。 // [System.Web.Script.Services.ScriptService] public class tls_export_test : System.Web.Services.WebService { public tls_export_test() { //如果使用設計的元件,請取消註解下行程式碼 //InitializeComponent(); } [WebMethod] public XmlDataDocument test() { XmlDeclaration xmldec; XmlDataDocument xml = new System.Xml.XmlDataDocument(ds); xml.DataSet.EnforceConstraints = false; xmldec = xml.CreateXmlDeclaration("1.0", "BIG5", null); xml.PrependChild(xmldec); return xml; }}

Delphi 6 與 ASP.NET 使用 Web Service 資料傳輸 (Delphi 6傳XML 由ASP.NET接收)

Delphi 6 引用ASP.NET的Web Service後呼叫test(xml) function 傳入xml 注意事項:
1. xml為Delphi 6傳過來的值 2. 將xml轉成ds後再進行處理
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Transactions;
using System.Xml;
///
/// tls_import_test 的摘要描述/// 
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// 若要允許使用 ASP.NET AJAX 從指令碼呼叫此 Web 服務,請取消註解下一行。
// [System.Web.Script.Services.ScriptService]
public class tls_import_test : System.Web.Services.WebService
{
public tls_import_test()
{
//如果使用設計的元件,請取消註解下行程式碼
//InitializeComponent();
}
[WebMethod]
public string test(XmlDocument xml)
{
...................................
DataSet ds = new DataSet();
XmlNodeReader reader = new XmlNodeReader(xml);
ds.ReadXml(reader);
}}

2010年6月17日 星期四

GridView 標題排序 來源為 DataTable 換頁

//將撈出的資料儲存至DataTable dt
Protected Sub init_gv()
.............................
.............................
conn.Open()
cmd = New SqlCommand(sqlstr, conn)
dt.Load(cmd.ExecuteReader) //dt為最後撈出的結果
conn.Close()
ViewState("Dt") = dt
Dim dv As New DataView(dt)
dv.Sort = Session("sort_staus")
GridView1.DataSource = dv
GridView1.DataBind()
End Sub

//換頁Sorting不會亂掉
Protected Sub GridView1_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridView1.PageIndexChanging
GridView1.PageIndex = e.NewPageIndex
Dim dt As DataTable = DirectCast(ViewState("Dt"), DataTable)
Dim dv As New DataView(dt)
dv.Sort = Session("sort_staus")
GridView1.DataSource = dv
GridView1.DataBind()
End Sub
//加入Sorting
Protected Sub GridView1_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles GridView1.Sorting
Dim dt As DataTable = DirectCast(ViewState("Dt"), DataTable)
Dim dv As New DataView(dt)
If ViewState("Sort").ToString() = " asc" Then
ViewState("Sort") = " desc"
Else
ViewState("Sort") = " asc"
End If
dv.Sort = e.SortExpression + ViewState("Sort").ToString()
Session("sort_staus") = dv.Sort
GridView1.DataSource = dv
ViewState("Dt") = dv.Table
GridView1.DataBind()
End Sub