說明: 一般使用RadioButtonList控制項,其Item預設為垂直排列(vertical),例:
o 選項一
o 選項二
該怎麼改成水平排列(horizontal)?
o 選項一 o 選項二
作法:
只要將此控制項的RepeatDirection屬性改為horizontal即可。
參考資料:
http://blog.yam.com/highscope/article/31505801
2013年10月24日 星期四
2013年10月23日 星期三
[ASP.NET] 子視窗日曆傳值回母視窗控制項上
說明:點選母視窗上的TextBox後,彈出一日曆子視窗,點選日期後回傳至母視窗TextBox上。
作法:
Parent.aspx
<head runat="server">
<title></title>
<script language="javascript" type="text/javascript">
function SetDate(ClientID)
{
var Url = '../modaldialog/SetDate.aspx?DialogClientID=' + ClientID;
window.open(Url, 'Calendar', 'width=335, height=170');
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:TextBox ID="txtDate" runat="server" onclick="SetDate(this.id)"></asp:TextBox>
</form>
</body>
Son.aspx
protected void Calendar1_SelectionChanged(object sender, System.EventArgs e)
{
if (Request.QueryString["DialogClientID"] != null)
{
string strClientID = Request["DialogClientID"].ToString().Trim();
StringBuilder sbJScript = new StringBuilder();
sbJScript.Append("window.opener.document.getElementById('" + strClientID + "').value = '" + this.Calendar1.SelectedDate.ToString("yyyy/MM/dd") + "';");
sbJScript.Append("window.close();");
ClientScript.RegisterStartupScript(this.GetType(), "ReturnValue", sbJScript.ToString(), true);
}
}
參考資料:
http://social.msdn.microsoft.com/Forums/zh-TW/b415ff68-2dcc-4e1c-b202-e41b4e29f8a9/a-popup-b-a-
作法:
Parent.aspx
<head runat="server">
<title></title>
<script language="javascript" type="text/javascript">
function SetDate(ClientID)
{
var Url = '../modaldialog/SetDate.aspx?DialogClientID=' + ClientID;
window.open(Url, 'Calendar', 'width=335, height=170');
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:TextBox ID="txtDate" runat="server" onclick="SetDate(this.id)"></asp:TextBox>
</form>
</body>
Son.aspx
protected void Calendar1_SelectionChanged(object sender, System.EventArgs e)
{
if (Request.QueryString["DialogClientID"] != null)
{
string strClientID = Request["DialogClientID"].ToString().Trim();
StringBuilder sbJScript = new StringBuilder();
sbJScript.Append("window.opener.document.getElementById('" + strClientID + "').value = '" + this.Calendar1.SelectedDate.ToString("yyyy/MM/dd") + "';");
sbJScript.Append("window.close();");
ClientScript.RegisterStartupScript(this.GetType(), "ReturnValue", sbJScript.ToString(), true);
}
}
參考資料:
http://social.msdn.microsoft.com/Forums/zh-TW/b415ff68-2dcc-4e1c-b202-e41b4e29f8a9/a-popup-b-a-
2013年10月22日 星期二
[ASP.NET] MasterPage套用多語系
說明:一般ASP.NET網頁都是override InitializeCulture method來轉換語系,但因MasterPage是繼承MasterPage class(UserControl),無提供InitializeCulture method。
作法:
1. 自訂一BasePage class(放於App_Code)並繼承Page class,在BasePage中override InitializeCulture method,再使各page從繼承Page改為namespace.BasePage,例:
BasePage.cs
namespace Web
{
public class BasePage : Page
{
protected override void InitializeCulture()
{
string culture = Session["culture"].ToString();
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(culture);
Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture);
base.InitializeCulture();
}
}
}
再搭配如DropDownList等控制項供選擇,例:
xxx.aspx
<asp:DropDownList ID="ddlLanguage" runat="server" AutoPostBack="True" onselectedindexchanged="ddlLanguage_SelectedIndexChanged">
<asp:ListItem Value="zh-TW" Text="<%$ Resources:PageResource, LanguageZh %>"></asp:ListItem>
<asp:ListItem Value="en-US" Text="<%$ Resources:PageResource, LanguageEn %>"></asp:ListItem></asp:DropDownList>
xxx.aspx.cs
protected void ddlLanguage_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.ddlLanguage.SelectedIndex > 0)
{
Session["culture"] = this.ddlLanguage.SelectedValue;
//reload request page
Server.Transfer(Request.Path);
}
}
參考資料:
http://stackoverflow.com/questions/11805897/masterpage-initializeculture-no-suitable-method-found-to-override-error
2. 透過global.asax。
作法:
1. 自訂一BasePage class(放於App_Code)並繼承Page class,在BasePage中override InitializeCulture method,再使各page從繼承Page改為namespace.BasePage,例:
BasePage.cs
namespace Web
{
public class BasePage : Page
{
protected override void InitializeCulture()
{
string culture = Session["culture"].ToString();
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(culture);
Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture);
base.InitializeCulture();
}
}
}
再搭配如DropDownList等控制項供選擇,例:
xxx.aspx
<asp:DropDownList ID="ddlLanguage" runat="server" AutoPostBack="True" onselectedindexchanged="ddlLanguage_SelectedIndexChanged">
<asp:ListItem Value="zh-TW" Text="<%$ Resources:PageResource, LanguageZh %>"></asp:ListItem>
<asp:ListItem Value="en-US" Text="<%$ Resources:PageResource, LanguageEn %>"></asp:ListItem></asp:DropDownList>
xxx.aspx.cs
protected void ddlLanguage_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.ddlLanguage.SelectedIndex > 0)
{
Session["culture"] = this.ddlLanguage.SelectedValue;
//reload request page
Server.Transfer(Request.Path);
}
}
參考資料:
http://stackoverflow.com/questions/11805897/masterpage-initializeculture-no-suitable-method-found-to-override-error
2. 透過global.asax。
2013年10月14日 星期一
[ASP.NET/ Crystal Report] 實作multiple table資料來源且table間無關聯的報表
說明:使用Crystal Reports實做一報表,繫結DB table為2個以上,且table之間無關聯,
作法:
1. 使用DataSet建立報表資料結構,新增DataTable以對應不同DB table,可不需建立DataTableAdapter方法
2. 新增Crystal Report檔,使用資料庫專家->ADO.NET資料集,選取建立的table結構
3. 將資料庫欄位拉至報表上
4. 新增一web page以呈現報表,拉一CrystalReportViewer控制項,依需求調整控制項面板
5. 在code behind繫結資料來源:
例:
//需import該dll
using CrystalDecisions.CrystalReports.Engine;
private ReportDocument rdReport;
protected void Page_Load(object sender, EventArgs e)
{
rdReport = new ReportDocument();
if (!IsPostBack)
{
BindReport();
}
}
private void BindReport()
{
DataSet dsReport = GetReportData();
string strRptName = Page.MapPath("ReportView.rpt");
rdReport.Load(strRptName);
rdReport.SetDataSource(dsReport);
CrystalReportViewer1.ReportSource = rdReport;
CrystalReportViewer1.DataBind();
}
private DataSet GetReportData()
{
DataSet dsReturn = new DataSet();
DataTable dtTemp = null;
//此處以DataTable回傳各DB table資料,若table之間有關聯,可以join方式回傳即可,無關聯則取得各table資料,再依Step1建立之table name加進DataSet中即可
dtTemp = 各sql回傳資料;
dtTemp.TableName = "Step1中建立的table name";
dsReturn.Tables.Add(dtTemp);
return dsReturn;
}
參考資料:
http://csharp.net-informations.com/dataset/dataset-multiple-tables-sqlserver.htm
http://www.blueshop.com.tw/board/show.asp?subcde=BRD20110419171449396
http://www.functionx.com/csharp2/dataset/Lesson02b.htm
作法:
1. 使用DataSet建立報表資料結構,新增DataTable以對應不同DB table,可不需建立DataTableAdapter方法
2. 新增Crystal Report檔,使用資料庫專家->ADO.NET資料集,選取建立的table結構
3. 將資料庫欄位拉至報表上
4. 新增一web page以呈現報表,拉一CrystalReportViewer控制項,依需求調整控制項面板
5. 在code behind繫結資料來源:
例:
//需import該dll
using CrystalDecisions.CrystalReports.Engine;
private ReportDocument rdReport;
protected void Page_Load(object sender, EventArgs e)
{
rdReport = new ReportDocument();
if (!IsPostBack)
{
BindReport();
}
}
private void BindReport()
{
DataSet dsReport = GetReportData();
string strRptName = Page.MapPath("ReportView.rpt");
rdReport.Load(strRptName);
rdReport.SetDataSource(dsReport);
CrystalReportViewer1.ReportSource = rdReport;
CrystalReportViewer1.DataBind();
}
private DataSet GetReportData()
{
DataSet dsReturn = new DataSet();
DataTable dtTemp = null;
//此處以DataTable回傳各DB table資料,若table之間有關聯,可以join方式回傳即可,無關聯則取得各table資料,再依Step1建立之table name加進DataSet中即可
dtTemp = 各sql回傳資料;
dtTemp.TableName = "Step1中建立的table name";
dsReturn.Tables.Add(dtTemp);
return dsReturn;
}
參考資料:
http://csharp.net-informations.com/dataset/dataset-multiple-tables-sqlserver.htm
http://www.blueshop.com.tw/board/show.asp?subcde=BRD20110419171449396
http://www.functionx.com/csharp2/dataset/Lesson02b.htm
2013年10月1日 星期二
[ASP.NET] 如何在GridView加上CheckBox全選與取消全選
說明:
在GridView加上CheckBox針對目標列資料勾選或全選以刪除資料。
作法:
//xxx.aspx
<GridView>
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="cbxHead" runat="server" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="cbxRow" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</GridView>
//xxx.aspx.cs
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
CheckBox cbxHead = (CheckBox)e.Row.FindControl("cbxHead");
cbxHead.Attributes.Add("onclick", "for(i=0;i<" + GridView1.ClientID + ".all.tags('input').length;i++){if (" + GridView1.ClientID + ".all.tags('input')[i].type=='checkbox'){" + GridView1.ClientID + ".all.tags('input')[i].checked=checked}}");
}
在GridView加上CheckBox針對目標列資料勾選或全選以刪除資料。
作法:
//xxx.aspx
<GridView>
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="cbxHead" runat="server" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="cbxRow" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</GridView>
//xxx.aspx.cs
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
CheckBox cbxHead = (CheckBox)e.Row.FindControl("cbxHead");
cbxHead.Attributes.Add("onclick", "for(i=0;i<" + GridView1.ClientID + ".all.tags('input').length;i++){if (" + GridView1.ClientID + ".all.tags('input')[i].type=='checkbox'){" + GridView1.ClientID + ".all.tags('input')[i].checked=checked}}");
}
}
2013年9月26日 星期四
[ASP.NET/AjaxToolKit] 使用CalendarExtender遇到問題紀錄
紀錄一下,使用上遇到的問題:
1. 找不到任何適用特定文化特性或中性文化特性的資源。請確定您已在編譯時期正確地將 "AjaxControlToolkit.Properties.Resources.resources" 嵌入或連結至組件 "AjaxControlToolkit" 中,或所有需要的附屬組件均為可載入且已完整簽署。
Ans: 無加入ToolkitScriptManager或ScriptManager控制項。
2. 加入ToolkitScriptManager控制項後,發生MasterPage的JavaScript或JQuery運作不正常
做法:
1. 在ScriptManager或ToolkitScriptManager控制項加上ScriptMode="Release",此屬性用來決定用戶端指令碼程式庫該使用debug抑或Release的版本,ScriptMode預設為Auto,會跟著web.config的deployment retail的屬性走,預設為false,因此ScriptMode="Debug"。AjaxToolKit官網也建議將ScriptMode="Release",http://www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/ToolkitScriptManager/ToolkitScriptManager.aspx
//xxx.aspx
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server" ScriptMode="Release"></asp:ToolkitScriptManager>
<asp:TextBox ID="txtDate" runat="server"></asp:TextBox>
<asp:CalendarExtender ID="txtDate_CalendarExtender" runat="server" Enabled="True" TargetControlID="txtDate"></asp:CalendarExtender>
2. 也可能是以下板友列出的原因:
http://social.msdn.microsoft.com/Forums/zh-TW/81016eba-6e82-479a-b426-fc53f8b1f3ac/calendarextender-
參考資料
http://weblogs.asp.net/lorenh/archive/2008/02/15/speed-up-load-time-of-ajax-control-toolkit-controls-while-debugging.aspx
http://msdn.microsoft.com/zh-tw/library/system.web.ui.scriptmanager.scriptmode.aspx
http://msdn.microsoft.com/zh-tw/library/ms228298.aspx
1. 找不到任何適用特定文化特性或中性文化特性的資源。請確定您已在編譯時期正確地將 "AjaxControlToolkit.Properties.Resources.resources" 嵌入或連結至組件 "AjaxControlToolkit" 中,或所有需要的附屬組件均為可載入且已完整簽署。
Ans: 無加入ToolkitScriptManager或ScriptManager控制項。
2. 加入ToolkitScriptManager控制項後,發生MasterPage的JavaScript或JQuery運作不正常
做法:
1. 在ScriptManager或ToolkitScriptManager控制項加上ScriptMode="Release",此屬性用來決定用戶端指令碼程式庫該使用debug抑或Release的版本,ScriptMode預設為Auto,會跟著web.config的deployment retail的屬性走,預設為false,因此ScriptMode="Debug"。AjaxToolKit官網也建議將ScriptMode="Release",http://www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/ToolkitScriptManager/ToolkitScriptManager.aspx
//xxx.aspx
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server" ScriptMode="Release"></asp:ToolkitScriptManager>
<asp:TextBox ID="txtDate" runat="server"></asp:TextBox>
<asp:CalendarExtender ID="txtDate_CalendarExtender" runat="server" Enabled="True" TargetControlID="txtDate"></asp:CalendarExtender>
2. 也可能是以下板友列出的原因:
http://social.msdn.microsoft.com/Forums/zh-TW/81016eba-6e82-479a-b426-fc53f8b1f3ac/calendarextender-
參考資料
http://weblogs.asp.net/lorenh/archive/2008/02/15/speed-up-load-time-of-ajax-control-toolkit-controls-while-debugging.aspx
http://msdn.microsoft.com/zh-tw/library/system.web.ui.scriptmanager.scriptmode.aspx
http://msdn.microsoft.com/zh-tw/library/ms228298.aspx
2013年9月25日 星期三
[C#] Math.Round/Ceiling/Floor用法
說明:
.NET Framework 中的 Round 方法會執行「四捨六入五成雙」(Banker's Rounding),將結尾為 .5 的數字捨入或進位為最接近的偶數,而不是下一個較高的位數。
例如,2.5會捨入為 2,而 3.5 會進位為 4 (當資料交易很大時,此法有助於避免系統性地偏向較高位數的值)。
因此:
Math.Round方法有提供MidpointRounding列舉型別:
//當某個數字剛好位於另外兩個數字之間的中點時,將其捨入成為距離最近的偶數。
ToEven = 0,
//當某個數字剛好位於另外兩個數字之間的中點時,朝向遠離零的方向將其捨入成距離最近的數字。
AwayFromZero = 1,
選擇AwayFromZero即是我們熟知的算術四捨五入,
Math.Round(2.5, 0, MidpointRounding.AwayFromZero) = 3。
P.S.
在 T-SQL 中,Round函式則會一律往遠離 0 的方向捨入。因此 2.5 會捨入為 3,
但Round(0.5, 0)會出現「轉換 expression 到資料類型 numeric 時發生算術溢位錯誤。」
這應該是因為0.5的資料類型是numeric(1, 1),進位後為1.0導致有效位數不夠。
因此Round(0.4, 0)不會有錯誤。
可使用Round(CAST (0.5 AS numeric(2,1)), 0)增加有效位數或使用字串類型做處理(Round('0.5', 0))
參考資料:
http://www.dotblogs.com.tw/jeff-yeh/archive/2009/06/15/8834.aspx
http://msdn.microsoft.com/zh-tw/library/ms131274(VS.80).aspx
http://msdn.microsoft.com/zh-tw/library/bb882648(v=vs.90).aspx
http://sharedderrick.blogspot.tw/2009/06/round-sql-server-2008.html
http://www.dotblogs.com.tw/jaigi/archive/2011/05/11/24822.aspx
.NET Framework 中的 Round 方法會執行「四捨六入五成雙」(Banker's Rounding),將結尾為 .5 的數字捨入或進位為最接近的偶數,而不是下一個較高的位數。
例如,2.5會捨入為 2,而 3.5 會進位為 4 (當資料交易很大時,此法有助於避免系統性地偏向較高位數的值)。
因此:
Math.Round方法有提供MidpointRounding列舉型別:
//當某個數字剛好位於另外兩個數字之間的中點時,將其捨入成為距離最近的偶數。
ToEven = 0,
//當某個數字剛好位於另外兩個數字之間的中點時,朝向遠離零的方向將其捨入成距離最近的數字。
AwayFromZero = 1,
選擇AwayFromZero即是我們熟知的算術四捨五入,
Math.Round(2.5, 0, MidpointRounding.AwayFromZero) = 3。
P.S.
在 T-SQL 中,Round函式則會一律往遠離 0 的方向捨入。因此 2.5 會捨入為 3,
但Round(0.5, 0)會出現「轉換 expression 到資料類型 numeric 時發生算術溢位錯誤。」
這應該是因為0.5的資料類型是numeric(1, 1),進位後為1.0導致有效位數不夠。
因此Round(0.4, 0)不會有錯誤。
可使用Round(CAST (0.5 AS numeric(2,1)), 0)增加有效位數或使用字串類型做處理(Round('0.5', 0))
參考資料:
http://www.dotblogs.com.tw/jeff-yeh/archive/2009/06/15/8834.aspx
http://msdn.microsoft.com/zh-tw/library/ms131274(VS.80).aspx
http://msdn.microsoft.com/zh-tw/library/bb882648(v=vs.90).aspx
http://sharedderrick.blogspot.tw/2009/06/round-sql-server-2008.html
http://www.dotblogs.com.tw/jaigi/archive/2011/05/11/24822.aspx
訂閱:
文章 (Atom)