2014年7月13日 星期日

[RESOLVED] Timer/Stopwatch and DetailsView


Rundown of the page:


My page has a Stopwatch, two timers, a ModalPopup, DetailsView, and Gridview. I have one timer that is used for the stopwatch function. The other timer is used to time out (ModalPopup) the page after the stopwatch has started.


The DetailsView is always on Insert mode. The Gridview is just to display what has been inserted from the DetailsView.




The function of the timer is to see how long it takes to insert all the items in the DetailsView. For some reason, when the stopwatch is running, I can't really insert the items. The focus is taken off the textbox or dropdownlist. This defeats the purpose
of the stopwatch though. I'm not sure what's causing this though.



































Dry
Freeze
Chill




Dry
Freeze
Chill






















Parcel Post
Priority
Other
ByPass
AF Priority Mail
AF General




Parcel Post
Priority
Other
ByPass
AF Priority Mail
AF General









































































































































   









































using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Diagnostics;

public partial class PackageManagement_Packing2 : System.Web.UI.Page
{
public static Stopwatch sw;
public static Stopwatch logout;

protected void Page_Load(object sender, EventArgs e)
{
lblError.Visible = false;
mpeTimesUp.Hide();


if (Session["OrderID"] == null || !string.IsNullOrEmpty(Session["Shipped"] as string) || !string.IsNullOrEmpty(Session["Cancel"] as string) )
{
Response.Redirect("~/PackageManagement/Packing1.aspx");
}

else if (Session["OrderID"] != null)
{
lblDisplayOrder.Text = Session["OrderID"].ToString();
}


if (!IsPostBack)
{
sw = new Stopwatch();
logout = new Stopwatch();


}

}
protected void btnStart_Click(object sender, EventArgs e)
{
if (btnStart.Text == "Start")
{
tm1.Enabled = true;
sw.Start();
btnStart.Text = "Stop";
btnReset.Enabled = false;



}
else
{
tm1.Enabled = false;
sw.Stop();
btnStart.Text = "Start";
btnReset.Enabled = true;
}

}
protected void btnReset_Click(object sender, EventArgs e)
{
sw.Reset();
lblTimer.Text = "00 : 00";
}

protected void tm1_Tick(object sender, EventArgs e)
{
long sec = sw.Elapsed.Seconds;
long min = sw.Elapsed.Minutes;
long hrs = sw.Elapsed.Hours;



if (hrs < 10)
lblTimer.Text = "0" + hrs;
else
lblTimer.Text = hrs.ToString();

lblTimer.Text += " : ";


if (min < 60)
{
if (min < 10)
lblTimer.Text = "0" + min;
else
lblTimer.Text = min.ToString();

lblTimer.Text += " : ";

if (sec < 10)
lblTimer.Text += "0" + sec;
else
lblTimer.Text += sec.ToString();
}
else
{
sw.Stop();
}

}

protected void dvPacking_DataBound(object sender, EventArgs e)
{
if (dvPacking.CurrentMode == DetailsViewMode.Insert)
{
TextBox txtOrderID = dvPacking.FindControl("txtOrderID") as TextBox;
txtOrderID.Text = Session["OrderID"].ToString();



DropDownList Adjustments = dvPacking.FindControl("ddlAdjustments") as DropDownList;

if (Adjustments != null)
{
Adjustments.DataTextField = "AdjustmentType";
Adjustments.DataValueField = "AdjustmentID";
Adjustments.DataSource = "dsAdjustment";

gvPacking.DataBind();
}
}
}

protected void gvPacking_DataBound(object sender, EventArgs e)
{

}
protected void btnContinue_Click(object sender, EventArgs e)
{
if (tm1.Enabled == false)
{

Session["Timer"] = lblTimer.Text;

if (Session["Timer"] != null)
{
lblError.Visible = false;

Session["Order"] = Session["OrderID"];

Response.Redirect("~/PackageManagement/Packing3.aspx");
}
else
{
lblError.Visible = true;
}
}
else if (tm1.Enabled == true)
{
lblError.Visible = true;

}
}

protected void tiLogout_Tick(object sender, EventArgs e)
{
if (lblTimer.Text != "00 : 00")
{
tm1.Enabled = false;
sw.Stop();
btnStart.Text = "Start";
btnReset.Enabled = true;

tiLogout.Enabled = false;
mpeTimesUp.Show();
}

}

protected void OkButton_Click(object sender, EventArgs e)
{
mpeTimesUp.Hide();
}
}









This is the code that worked:





沒有留言:

張貼留言