-1
我正在嘗試創建一個網站,要求用戶選擇所提供服務的開始日期和截止日期。目前我正在嘗試創建一個例外,如果開始日期是在選定的截止日期之後。我能夠把例外,但不知道在哪裏拋出異常。我的代碼如下:如何在Visual Studio網站中引發異常項目
public partial class CalendarRange : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
// This code will only run on the first page load event
this.CalStartDate.SelectedDate = DateTime.Today;
this.CalThroughDate.SelectedDate = DateTime.Today;
}
else
{
// if the starting date is past the through date
if (this.StartDate > this.ThroughDate)
{
throw new Exception("dates are bogus");
}
}
}
/**
* "Get" (accessors) methods for start and through dates
*
*/
public DateTime StartDate
{
get
{
return CalStartDate.SelectedDate;
}
}
public DateTime ThroughDate
{
get
{
return CalThroughDate.SelectedDate;
}
}
}
我應該在哪裏拋出異常,並且它應該是在default.aspx.cs頁面或當前頁面(CalendarRange.ascx.cs)?
我認爲你根本不會拋出異常。您有數據驗證問題,可以使用工作流程進行處理。這不是一個你無法從中恢復的例外事件。 – Crowcoder