ASP.NET (C#) event to happen before printing
I have a web form in ASP.Net with C# code behind. It's a simple thing, and I'm pretty new so I'm kind of stuck.
In the source code of the web form I have a button called "print" that looks like this:
<asp:Button ID="btnPrint" runat="server" onclientclick="window.print();" Text="Print" />
No problem. In the code behind I have this:
protected void btnPrint_Click(object sender, EventArgs e)
{
//get current Date/Time
string dateTime = DateTime.Now.ToLongDateString() + ", at " + DateTime.Now.ToShortTimeString();
//set it to labelDate
lblDat开发者_Go百科e.Text = "Requested on " + dateTime;
}
So the problem is that when I hit the print button, the form prints before the code executes and stamps the label (lblDate.Text).
Soooo... my noob question is how to get that date/time stamp to process before the form prints?
Thanks for your advice.
Mark
My first instinct would be to ditch the server side event, and populate the time stamp with javascript before the print call.
Try something like this:
protected void btnPrint_Click(object sender, EventArgs e)
{
//get current Date/Time
string dateTime = DateTime.Now.ToLongDateString() + ", at " + DateTime.Now.ToShortTimeString();
//set it to labelDate
lblDate.Text = "Requested on " + dateTime;
ScriptManager.RegisterStartupScript(this, this.GetType(), "key", "window.print();", true);
}
Instead of calling a windows.print(), call a function that adds the Requested datetime. That will do the work.
精彩评论