javascript timer - asp.net-2.0

I am developing an online exam application using asp.net. In the start exam page I have created a javascript countdown timer.
How can I move to the next page automatically after the timer reaches 00?
Here is my code:
long timerStartValue = 1000 ;
private int TimerInterval
{
get
{
int o =(int) ViewState["timerInterval"];
if(o==0)
{
return (o);
}
return 50 ;
}
set
{
ViewState["timerInterval"] = value;
}
}
protected void Page_PreInit(object sender,EventArgs e)
{
string timerVal = Request.Form["timerData"];
if(! String.IsNullOrEmpty(timerVal))
{
timerVal = timerVal.Replace(",", String.Empty) ;
this.timerStartValue = long.Parse(timerVal);
}
}
protected void Page_Load(object sender, EventArgs e)
{
if(! IsPostBack)
{
this.timerStartValue = 10000; //3599000;//14400000;
this.TimerInterval = 500;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
this.timerStartValue = 3599000;
}
protected void Page_PreRender(object sender, EventArgs e)
{
System.Text.StringBuilder bldr=new System.Text.StringBuilder();
bldr.AppendFormat("var Timer = new myTimer({0},{1},'{2}','timerData');", this.timerStartValue, this.TimerInterval, this.lblTimerCount.ClientID);
bldr.Append("Timer.go()");
ClientScript.RegisterStartupScript(this.GetType(), "TimerScript", bldr.ToString(), true);
ClientScript.RegisterHiddenField("timerData", timerStartValue.ToString());
}
Thanks in advance,
sangita

It sounds like when you click the "Next" button, you are loading an entirely new page. This of course changes all the content and resets all the javascript. You can't maintain state across pages without a bit of work.
The solution to this could be to save the timer state when the next button is pressed, and pass it to the next stage. You could do this by saving the timer state to a hidden form input and submitting it along with the Next button.
The other option would be to load your questions via AJAX. Instead of moving to a new page every time the next button is clicked, you could simply replace the question portion of the page with a new question, and leave the timer intact. This is probably the solution I would use.

Are u reloading the entire page when clicking on the next button ? That may leads to realod the java script file also.So the variable values will reset.May be you can think about showing the questions /answers via Ajax.You need not reload the entire page when showing the next question.the part when you show the quiz will only be updated.so you can maintain the global variables in your java script too. Check the below link to know about partial page updating using jQuery.
http://www.west-wind.com/presentations/jquery/jquerypart2.aspx
Hope this helps.

You can put the timer in an iframe if you can't get rid of the postback.

You need a way to persist information between pages, and there's really only one possibility: To make it part of the next page request.
Now, this could be subdivided into 2 categories:
1) As part of the url: http://www.example.com/page?timer=123;
2) As part of the headers;
And number 2 opens new possibilities:
a) As part of POST data;
b) As a client-side cookie only;
c) As a cookie tied to information on the server;
Number 1, 2a and 2b can be manipulated by the user. So what you can do is store some value in a cookie, a hash for example or a database row ID, that you'll use to fetch information on the server.
tl;dr? Use a asp "Session object". It lets you keep things on the server-side and users will have no idea what they are.

Related

Crystal Reports Web Viewer Issue with Pagination and Optional Parameters

I've been searching for a couple of days now and am running into an issue no matter what I've tried. The problem is that I seem to have come across with the perfect storm and I can't get all 3 things working at the same time.
Pagination
Optional Parameters
Parameter Dialog Prompt
So this first method is what I've been using and everything works except it won't Navigate past past 2 (And I've very aware of why navigation doesn't work)
// ##################################################################################################################################################
// METHOD 1: Everything works correctly except you can't go past page 2
protected void Page_Load(object sender, EventArgs e)
{
CrystalReportViewer1.ReportSource = Session["myReportDoc"] as CrystalDecisions.CrystalReports.Engine.ReportDocument;
if (CrystalReportViewer1.ReportSource == null)
{
//Generate the Report Document
Handlers.ReportHandler myReportHandler = new Handlers.ReportHandler();
CrystalDecisions.CrystalReports.Engine.ReportDocument myReportDocument = myReportHandler.GenerateReport("AlarmStatusReport");
Session["myReportDoc"] = myReportDocument; //This is were we save it off for next time
CrystalReportViewer1.ReportSource = myReportDocument;
}
}
So knowing that the common fix is to not use Page Load but use Page_Init instead. This fixes the Navigation... until I open a report that has optional parameters. With those, every time I try to navigate to the next page, instead of it working, the Parameter box re-appears and now requires at least 1 of my Optional Parameters to be filled out. (Each "next Page" reduces the prompt by 1 Optional). But, because I'm being forced to change the Parameters, it "refreshes" the report and I'm back on Page 1.
// ##################################################################################################################################################
// METHOD 2: Works, but not for any report that has Optional Parameters. They become "Required" and keep popping up instead of navigating to the next page
protected void Page_Init(object sender, EventArgs e)
{
CrystalReportViewer1.ReportSource = Session["myReportDoc"] as CrystalDecisions.CrystalReports.Engine.ReportDocument;
if (CrystalReportViewer1.ReportSource == null)
{
//Generate the Report Document
Handlers.ReportHandler myReportHandler = new Handlers.ReportHandler();
CrystalDecisions.CrystalReports.Engine.ReportDocument myReportDocument = myReportHandler.GenerateReport("AlarmStatusReport");
Session["myReportDoc"] = myReportDocument; //This is were we save it off for next time
CrystalReportViewer1.ReportSource = myReportDocument;
}
}
Now, I got real excited, because I got a bit clever and fixed both those issues, by trapping the Navigation and keeping track of the Page myself. EVERYTHING WORKS NOW!!! until I go to the Parameter Dialog prompt and it was totally jacked up.
// ##################################################################################################################################################
// METHOD 3: Everything works correctly except the Prompt Box doesn't Format correcly due to the addition of the added Event Handers
protected void Page_Load(object sender, EventArgs e)
{
CrystalReportViewer1.ReportSource = Session["myReportDoc"] as CrystalDecisions.CrystalReports.Engine.ReportDocument;
if (CrystalReportViewer1.ReportSource == null)
{
//Generate the Report Document
Handlers.ReportHandler myReportHandler = new Handlers.ReportHandler();
CrystalDecisions.CrystalReports.Engine.ReportDocument myReportDocument = myReportHandler.GenerateReport("AlarmStatusReport");
Session["myReportDoc"] = myReportDocument; //This is were we save it off for next time
CrystalReportViewer1.ReportSource = myReportDocument;
//Init our Manual Page Counter to 1
HiddenFieldPageNumber.Value = "1";
}
CrystalReportViewer1.Navigate += CrystalReportViewer1_Navigate; //Simply Adding this event, EVEN IF IT HAS NO CODE, Breaks the style and formating of the Parameter Prompt box.
CrystalReportViewer1.PreRender += CrystalReportViewer1_PreRender;
}
private void CrystalReportViewer1_Navigate(object source, CrystalDecisions.Web.NavigateEventArgs e)
{
//This prevents this event from Incrementing the Page again when the PreRender Event
//below re-sets which page to show.
if (_SkipPageIncrement == true)
{
return;
}
//Whenever the Navigation is used, this Event fires. Here is the problem, there is nothing that actually tells
//us if the user clicked on Previous or Next (or GotoPage for that Matter). So we have to do some guessing here
if (e.CurrentPageNumber == 1 && e.NewPageNumber == 2)
{
//If they pressed "NEXT" we will always get Current = 1 and New = 2 due to the Pagination starting over on the PostBack
//So we INCREMENT our real Page Number Value.
HiddenFieldPageNumber.Value = (Convert.ToInt32(HiddenFieldPageNumber.Value) + 1).ToString();
}
else if (e.CurrentPageNumber == 1 && e.NewPageNumber == 1)
{
//If they pressed "PREV" we will always get Current = 1 and New = 1 due to the Pagination starting over on the PostBack
//So we DECREMENT our real Page Number Value.
HiddenFieldPageNumber.Value = (Convert.ToInt32(HiddenFieldPageNumber.Value) - 1).ToString();
}
}
private void CrystalReportViewer1_PreRender(object sender, EventArgs e)
{
//The Viewer has a method that allows us to set the page number. This PreRender Event is the only
//Event I could find that works. It comes AFTER the Navigate, but before the reports is rendered.
_SkipPageIncrement = true; //The ShowNthPage re-triggers the Navigation, so this prevents it from running again.
CrystalReportViewer1.ShowNthPage(Convert.ToInt32(HiddenFieldPageNumber.Value));
}
As commented above, the moment I add the OnNavigation Event, even if I comment out all the actual code inside, my Prompt box goes from looking like this...
To this (my page as a dark background and you can see that now shows, plus the "OK" button is all jacked up.
I just don't get why trapping the Navigation Event breaks the Prompt box even when the event is not firing (on that first load).
Side note: I'm using VS 2019 with CR 13.0.3500.0
So thanks to the help of a teammate that is more adept on CSS as I am, I have resolved the issue "good enough". So for anyone who wants to use the LOAD event, (Or has to like me), but then loses the ability to use the navigation and wants to use my method, the band-aid for the Crystal Reports Parameter prompt is to simply override their Styling in you Site.css with this...
/*---------------------- Custom CSS for Report Prompt Buttons ----------------------*/
.pePromptButton {
padding-bottom:4.3px;
}
td.pePromptButton {
display: inherit;
}
img {
vertical-align:top;
}

How do I Redirect a new Tab with Query String in asp.net

How to open new tab window with passing ID using Query String.
I tried all the possible ways,i.e Onclientclick and others. but not.help me.
Thank you in advance
if (e.CommandName == "Items")
{
int ID = Convert.ToInt32(e.CommandArgument.ToString());
Response.Redirect("Add_Items.aspx?TestID=" + ID);
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
int id = Convert.ToInt32(Request.QueryString["TestID"]);
lblTestID.Text = id.ToString();
}
The server-side code can't instruct the browser to open a new tab, that's not how redirects work. All a redirect does is tell the browser to navigate to an address.
To open a new tab, you'd need to use client-side code. Generally this could be as simple as:
window.open(address, '_blank');
However, keep in mind that browsers make no standard guarantee to open things in tabs vs. new windows. That's entirely up to the browser's settings and capabilities. (I think that in some browsers it might make a difference whether this code is executed directly or in a click event.)

Mixing sessions in a ASP.NET application

I am facing a problem of mixed information between sessions in my application. The problem
occurs when a user loads a page and it populates a session (Session ["Whatever"]) ... so far so good. The problem starts to happen when that same user opens the same screen on another browser tab for example. He carries other information for the same session that is used on the first screen it opened. (Here is a small example)
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
if (Request.QueryString["id"] != null)
{
if (Request.QueryString["id"] != "")
Session["Any"] = Request.QueryString["id"].ToString(); //<-Here it changes the session for the first tab
}
}
}
I wonder if there is a better way to do this. Has some way to create a unique identifier for each tab opened by the user? Or some unique id in order to concatenate the name of the session? So sessions would be only by open flap.
Thanked.

How is this explained with ASP.NET page life cycle?

I have the following code, which simply inserts a record into the database, based on some text fields and a dropdown. The dropdown gets bound in the Page Load Event.
protected void btnAdd_Click(object sender, EventArgs e)
{
try
{
Personeel p = new Personeel();
p.achternaam = txtNaam.Text;
p.naam = txtVoornaam.Text;
p.fk_afdeling_id = Convert.ToInt16(cmbAfdeling.SelectedValue);
BLLpersoneel BLLp = new BLLpersoneel();
BLLp.insert(p);
lblFeedback.Text = "Done and done!";
rptPersoneel.DataBind();
}
catch (Exception err)
{
lblFeedback.Text = err.Message;
}
}
protected void Page_Load(object sender, EventArgs e)
{
if(Page.IsPostBack == false)
{
BLLafdeling BLLa = new BLLafdeling();
cmbAfdeling.DataSource = BLLa.selectAll();
cmbAfdeling.DataTextField = "naam";
cmbAfdeling.DataValueField = "afdeling_id";
cmbAfdeling.DataBind();
}
}
My question is about IsPostBack.
On first load, the page has no PostBack, so it will bind the data to the dropdown "cmbAfdeling".
Then, when submitting the form, there IS a postback, so we don't reach the code inside the if statement. To me, that would mean that ASP.NET would NOT bind the data to the combo box.
However, the data is still there after submitting (and thus having a postback).
How is this explained?
This is due to the ViewState. The data in the ComboBox is stored in the ViewState and is sent back & forth during postback.
This might be worth reading to understand what is happening: http://msdn.microsoft.com/en-us/library/ms972976.aspx
It's explained by a concept called viewstate:
If you examine the code produced by your asp, you will find some hidden fields, one of which is the "viewstate". The viewstate saves the important values of your asp in order to be able to populate the elements every time the pages gets loaded, even if it's after a postback.
The data is maintained during postback, as you don't clear the data during postback or on load it will persist.

Maintaining GridView current page index after navigating away from Gridview page

I have a GridView on ASP.NET web form which I have bound to a data source and set it to have 10 records per page.
I also have a hyper link column on the GridView, such that a user can navigate to another page (details page) from the list. On the details page, they have "Back" button to return to the GridView page
Edit
Just to clarify the query
I am looking for sample code snippet on the Server Side on how to specify the page index to set the GridView after data binding. The idea is to ensure the user navigates to the same page index they were on.
The three basic options at your disposal: query string, session, cookie. They each have their drawbacks and pluses:
Using the query string will require you to format all links leading to the page with the gridview to have the proper information in the query string (which could end up being more than just a page number).
Using a session would work if you're sure that each browser instance will want to go to the same gridview, otherwise you'll have to tag your session variable with some id key that is uniquely identifiable to each gridview page in question. This could result in the session management of a lot of variables that may be completely undesirable as most of them will have to expire by timeout only.
Using a cookie would require something similar where cookie data is stored in a key/data matrix (optimized hash table might work for this). It would not be recommended to have a separate cookie name for each gridview page you're tracking, but rather have a cookie with a common name that holds the data for all gridview pages being tracked and inside that have the key/value structure.
Edit: A small code snippet on setting the page index.
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
try
{
if(HttpContext.Current.Request["myGVPageId"] != null])
{
myGridview.PageIndex = Convert.ToInt32(HttpContext.Current.Request["myGVPageId"]);
}
}
catch(Exception ex)
{
// log it
}
}
}
I'm more of a fan of the Session approach, personally. Simply save your page index as a session variable, and, if this Session variable isn't null on page load, use it to fire your "OnPageIndexChanging" method, like so:
Set your current page number whenever the page number changes:
protected void GridViewIndexChanging(object sender, GridViewPageEventArgs e)
{
myGridView.PageIndex = e.NewPageIndex;
Session["pageNumber"] = e.NewPageIndex;
//whatever your page index changing does...
}
Then, on Page_Load do something like:
if (!IsPostBack)
{
if (Session["pageNumber"] != null)
{
GridViewIndexChanged(myGridView, new GridViewPageEventArgs((int)Session["pageNumber"]));
}
}
you can ues the Page Index Change Event of Gridview and Find out the Current Page Index for e:g
yourGridId.PageIndex=e.NewPageIndex;
ViewState["GridPageIndex"]=e.NewPageIndex;
on PageLoad Get the Viewstate Value
string pIndex=string.Empty;
pIndex=Convert.toInt32(ViewState["GridPageIndex"]);
if(!string.Empty(pIndex))
{
yourGridId.PageIndex =pIndex;
}
you must use query string and is recommended, otherwise you can use session object but don't use that for this as you may have grid view opening in different pages so use query string .
gridView1.CurrentPageIndex = (Request["pageNo"] != null) ? Request["pageNo"] as int : 0;
gridView1.DataSource = myDataSet;
gridView1.DataBind();
you can update your link on GridView_DataBound event

Resources