一個兩分熟的牛排跟一個四分熟的牛排見了面為什麼卻不打招呼?
作者:Ahan 日期:2005-05-10 23:53
ASP.Net按鈕控制項防止重覆送出的方式
作者:Ahan 日期:2005-04-25 21:59
防止使用者將表單重覆送出,目的在保持資料的完整性與一致性,同時也可保護伺服器不被Web Bot類程式的侵擾,解決方案依架構來區分可分為Server端及Client等,Server端的解決方案最簡單的是用Session來記錄每條連線的Submit狀態,讓重覆送出的PostBack不會對程式碼產生重覆執行的效果,方法較為可靠,但也較耗伺服器資源,本文針對使用Client端的客製化屬性將表單按鈕做特別的處理,確保表單可以不受干擾的執行。
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;
private void Page_Load(object sender, System.EventArgs e)
{
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
Button1.Attributes.Add("onclick","javascript:" +
Button1.ClientID + ".disabled=true;" +
this.GetPostBackEventReference(Button1));
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Button1.Click +=
new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void Button1_Click(object sender, System.EventArgs e)
{
System.Threading.Thread.Sleep(5000);
//Response.Redirect("http://www.msn.com");
}
}
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;
private void Page_Load(object sender, System.EventArgs e)
{
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
Button1.Attributes.Add("onclick","javascript:" +
Button1.ClientID + ".disabled=true;" +
this.GetPostBackEventReference(Button1));
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Button1.Click +=
new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void Button1_Click(object sender, System.EventArgs e)
{
System.Threading.Thread.Sleep(5000);
//Response.Redirect("http://www.msn.com");
}
}
DataGrid表格光棒設定方法
作者:Ahan 日期:2005-04-23 10:18
我們通常使用表格列出資料時,若未將表格介面做適當的處理,將造成使用者閱讀困難的問題,使用交錯表格顏色搭配滑鼠移動時的光棒效果(附圖1)是個還算不錯的處理方式,單純的網頁很好做,但要怎麼在ASP.Net的DataGrid上實作這樣子的方法,以下是我使用的方式,可能有點笨,如果有更好的方式,歡迎交流指正。
(附圖1)顏色交錯表格列與光棒

(附圖1)顏色交錯表格列與光棒
DataGrid OnItemDataBound時將Attribute設定Mouse動作屬性
<script language="c#" runat="server">
private void DG_ItemDataBound(object Sender, DataGridItemEventArgs e){
if(e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem){
e.Item.Attributes.Add
("onmouseover","trMouseColor(this)");
e.Item.Attributes.Add
("onmouseout","trMouseColor(this)");
}
}
</script>
private void DG_ItemDataBound(object Sender, DataGridItemEventArgs e){
if(e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem){
e.Item.Attributes.Add
("onmouseover","trMouseColor(this)");
e.Item.Attributes.Add
("onmouseout","trMouseColor(this)");
}
}
</script>
使用event.type捕捉滑鼠事件型態,再針對每種狀態做處理,因為使用了交錯表格顏色,因此必須將變色之前的顏色值做儲存,本例將TR原色使用物件.name來做暫存,不知道有沒有更好的辦法
<script language="javascript">
function trMouseColor(objTRColor){
var strColor = objTRColor.style.backgroundColor;
objTRColor.style.backgroundColor = strColor;
switch (event.type){
case 'mouseover':
objTRColor.style.backgroundColor = '#FFFF00';
objTRColor.name = strColor;
break;
case 'mouseout':
objTRColor.style.backgroundColor = objTRColor.name;
break;
default:
break;
}
}
</script>
function trMouseColor(objTRColor){
var strColor = objTRColor.style.backgroundColor;
objTRColor.style.backgroundColor = strColor;
switch (event.type){
case 'mouseover':
objTRColor.style.backgroundColor = '#FFFF00';
objTRColor.name = strColor;
break;
case 'mouseout':
objTRColor.style.backgroundColor = objTRColor.name;
break;
default:
break;
}
}
</script>
參考資料
RSS + XslTransform.Transform產生XHTML的方法
作者:Ahan 日期:2005-04-20 23:04
之前做了RSS的xml format,相信在xml底下一定會有更多應用,加上必須準備中華電信的行動校園應用比賽,因此嘗試以RSS + XSLT的方式將既有之RSS轉換成XHTML,實作完成並通過W3C XHTML1.0 Strict 及 XHTML1.1 的Validate,將部份心得寫下,更加詳細內容已經有人寫的很好了(如參考資料),請自行參考。
<xsl:text disable-output-escaping="yes">
這個標籤可以讓輸入的字串以html格式表玩出來,如輸入<代表<符號,請注意XslTransform.Transform會自動在產生出來的html文件的<head></head>加上<meta>標籤,可是確不是XHTML格式,因此如果你在<head>裡沒有定義<meta>標籤的話,就必須在<xsl:text disable-output-escaping="yes">寫入<head>,反之就可以寫在<xsl:template match="/rss">裡就可以了。
RSS2Xhtml.aspx - RSS+XSLT轉換XHTML的處理檔
string strXmlPath = "C:\XMLLocation";
XslTransform xslt = new XslTransform();
xslt.Load("http://localhost/RSS2XHTML.xsl");
xslt.Transform("http://localhost/RSSWriter.aspx",strXmlPath+"RSS2XHTML.html",null);
getFileDownload(strXmlPath+"RSS2XHTML.html");
//為了N-Tier所寫的html output handler,不在本文討論內
XslTransform xslt = new XslTransform();
xslt.Load("http://localhost/RSS2XHTML.xsl");
xslt.Transform("http://localhost/RSSWriter.aspx",strXmlPath+"RSS2XHTML.html",null);
getFileDownload(strXmlPath+"RSS2XHTML.html");
//為了N-Tier所寫的html output handler,不在本文討論內
<xsl:text disable-output-escaping="yes">
這個標籤可以讓輸入的字串以html格式表玩出來,如輸入<代表<符號,請注意XslTransform.Transform會自動在產生出來的html文件的<head></head>加上<meta>標籤,可是確不是XHTML格式,因此如果你在<head>裡沒有定義<meta>標籤的話,就必須在<xsl:text disable-output-escaping="yes">寫入<head>,反之就可以寫在<xsl:template match="/rss">裡就可以了。
RSS2XHTML.xsl 檔案片段
<xsl:text disable-output-escaping="yes">
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>網頁Title</title>
<style type="text/css">
body { margin-top:0px; margin-bottom:25px; text-align:center; font-family: verdana, sans-serif; font-size: 80%; line-height: 1.45em; }
#block { margin:0px auto; width:600px; text-align:left; }
p { padding-top: 0px; margin-top: 0px; }
h1 { font-size: 120%; padding-bottom: 0px; margin-bottom: 0px; }
h2 { font-size: 100%; margin-bottom: 0px; }
</style>
</head>
</xsl:text>
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>網頁Title</title>
<style type="text/css">
body { margin-top:0px; margin-bottom:25px; text-align:center; font-family: verdana, sans-serif; font-size: 80%; line-height: 1.45em; }
#block { margin:0px auto; width:600px; text-align:left; }
p { padding-top: 0px; margin-top: 0px; }
h1 { font-size: 120%; padding-bottom: 0px; margin-bottom: 0px; }
h2 { font-size: 100%; margin-bottom: 0px; }
</style>
</head>
</xsl:text>

參考資料

