所以我有常规的按钮点击事件,它已经检查了同一个俱乐部的数据,音高,时间和等级。日期存在,如果没有,则插入新记录。
我现在也有一些错误处理来检查时间格式是否格式正确或超过2小时。
是否可以根据这些错误句柄的结果停止执行按钮单击事件,如果它们放在从按钮单击事件调用的单独方法中?或者是在按钮点击事件中将所有if嵌套在一起的情况?
这是我的btnBook代码:
protected void btnBook_Click(object sender, EventArgs e)
{
DateTime();
if (txtBookingName.Text != "" & txtUserID.Text != "" & txtUserName.Text != "" & ddlClub.SelectedValue != "Select Club" & ddlPitches.SelectedValue != "Select Pitch" & end.Value != "" & start.Value != "" & txtDescription.Text != "")
{
//Helped by forum post to check if timeslot is already booked (www.forums.asp.net/t/2050551.aspx?Validation+to+Check+Duplicate+Records+)
using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString))
{
//Open your connection
conn.Open();
//Your Select Query
//Change this as per your design and provide the primary key field here
string selectString = "SELECT COUNT(*) FROM Booking WHERE ClubID = @ClubID And PitchID = @PitchID And StartTime = @StartTime And EndTime = @EndTime";
//Create Command object
SqlCommand myCommand = new SqlCommand(selectString, conn);
//Pass your parameter here
myCommand.Parameters.AddWithValue("@ClubID", ddlClub.SelectedItem.Value);
myCommand.Parameters.AddWithValue("@PitchID", ddlPitches.SelectedItem.Value);
myCommand.Parameters.AddWithValue("@StartTime", Convert.ToDateTime(start.Value));
myCommand.Parameters.AddWithValue("@EndTime", Convert.ToDateTime(end.Value));
// Get the Result query
var idExists = (Int32)myCommand.ExecuteScalar() > 0;
//Check if record exists in table
if (!idExists)
{
//Insert the Record
using (SqlCommand cmd = conn.CreateCommand())
{
SqlCommand cmd1 = new SqlCommand("Insert into Booking (BookingName, UserID, ClubID, PitchID, StartTime, EndTime, Description) values(@BookingName, @UserID, @ClubID, @PitchID, @StartTime, @EndTime, @Description)", conn);
cmd1.Parameters.Add("@BookingName", SqlDbType.NVarChar).Value = txtBookingName.Text;
cmd1.Parameters.Add("@UserID", SqlDbType.Int).Value = Convert.ToInt16(txtUserID.Text);
cmd1.Parameters.Add("@ClubID", SqlDbType.Int).Value = Convert.ToInt16(ddlClub.SelectedItem.Value);
cmd1.Parameters.Add("@PitchID", SqlDbType.Int).Value = Convert.ToInt16(ddlPitches.SelectedItem.Value);
cmd1.Parameters.Add("@StartTime", SqlDbType.DateTime).Value = Convert.ToDateTime(start.Value);
cmd1.Parameters.Add("@EndTime", SqlDbType.DateTime).Value = Convert.ToDateTime(end.Value);
cmd1.Parameters.Add("@Description", SqlDbType.NVarChar).Value = txtDescription.Text;
cmd1.ExecuteNonQuery();
conn.Close();
MsgBox("Your booking has been placed", this.Page, this);
}
}
else
{
MsgBox("The time slot for your selected Club & Pitch has already been booked. Please choose another", this.Page, this);
}
}
}
else
{
MsgBox("Please fill in all the required information", this.Page, this);
}
}
以下是我希望用于对错误检查进行分组的方法:
public void DateTime()
{
//Helped by tutorial (www.asp-net-example.blogspot.ie/2009/01/aspnet-date-time-example-how-to-get_2137.html)
DateTime startTime = Convert.ToDateTime(start.Value);
DateTime endTime = Convert.ToDateTime(end.Value);
TimeSpan difference = endTime.Subtract(startTime);
double totalHours = difference.TotalHours;
lblBookMessage.Text += "Total Hours Difference: " + totalHours;
if (totalHours > 2)
{
MsgBox("You cannot book a slot for more than 2 hours at a time", this.Page, this);
}
//Forum post to verify datetime format is correct (www.forums.asp.net/t/2067633.aspx?validate+textbox+on+button+click)
bool flagStart = true;
string strStart = start.Value.Trim();
if (!string.IsNullOrEmpty(strStart))
{
//validate the format dd/mm/yyyy HH:mm
if (!Regex.IsMatch(strStart, "^([1-9]|([012][0-9])|(3[01]))\\/([0]{0,1}[1-9]|1[012])\\/(201)\\d [012]{0,1}[0-9]:[0-0][0-0]$"))
{
//Don't Meet the requirements
flagStart = false;
MsgBox("Please enter a valid date and an on the hour time", this.Page, this);
}
}
lblBookMessage.Text = flagStart.ToString();
bool flagEnd = true;
string strEnd = end.Value.Trim();
if (!string.IsNullOrEmpty(strEnd))
{
//validate the format dd/mm/yyyy HH:mm
if (!Regex.IsMatch(strEnd, "^([1-9]|([012][0-9])|(3[01]))\\/([0]{0,1}[1-9]|1[012])\\/(201)\\d [012]{0,1}[0-9]:[0-0][0-0]$"))
{
//Don't Meet the requirements
flagEnd = false;
MsgBox("Please enter a valid date and an on the hour time", this.Page, this);
}
}
lblBookMessage.Text = flagEnd.ToString();
}
答案 0 :(得分:0)
您需要重构代码。当您在按钮单击处理程序中看到如此多行代码时,它看起来很麻烦。将您的逻辑划分为两种方法。如果可能,将它们移动到业务逻辑类。 根据提供的信息。您可以检查日期和数据的验证,分为两种返回bool的方法。你可以检查按钮点击事件,如..
protected void btnBook_Click(object sender,EventArgs e) {
string outmessage = string.empty;
if(ValidateDate(starttime,endtime,out outmessage) && ValidateData(txtBookingName.Text,txtUserID.Text, txtUserName.Text, ddlClub.SelectedValue, ddlPitches.SelectedValue , end.Value , start.Value ,txtDescription.Text, out outmessage))
{
// all valid
lblMessage.Text=outmessage;
}
else
{
//something is wrong.
MessageBox.Show(outmessage);
}
}
我希望我能正确理解你的问题,希望我的回答也能帮到你。