Restrict a page navigation to one control - c#

I am making an online exam website where a user is taken to a page where there are multiple choice questions. I want to set it up so that once a user navigates to this exam page, he cannot navigate back. The only way he can get out of it is by clicking the submit button and then he cannot access that page again. The questions on the exam page are in an SQL database. I have everything done, the only thing I need help with is restricting the navigation of the page.

It can be done by using Session varaible.If u have 3 pages say a.aspx,b.aspx,c.aspx then create 3 session variables such as
Session["a"]="some value";Session["b"]="some value";Session["c"]="some value";
On the pageload of the a.aspx check if the session contains value or null.If it is null then u can make sure that it is the first time that page is loaded else do what you want to do if user tries to visit a page which is already viewed.
Please try this code
//Code for page a[a.aspx]
protected void Page_Load(object sender, EventArgs e)
{
if(Session["a"]==null)
{
Session["a"]="Some Value";
}
else
{
// do code if user visit the page again.
}
}
Similarly you needs to do for all the pages.
I hope this will solve your problem.

You could use cookies or session to keep a tab on the pages the user has gone through so you can prevent them from going back.

Related

Can you "remember" what link is clicked on a webpage?

I'm working in Visual Studios to create a website for a movie theater (for class) I'm wondering if there's a way to "remember" what link is clicked for a show time? I know I could just create a webpage for each time, but I'd rather track an id, if that's possible!
By clicking the link, they'll go to the right page, but is there any way for me to put into the system what time they clicked? I couldn't find any similar questions here.
you could use sessions to hold the showtime selected. create an LinkButton with an onCommand that creates a session before redirecting them to the seating chart or checkout page.
so say a user is on your movie page and the user selects the 12pm showtime. When the user clicks on the link, it will run what ever is in your code behind. You can create a Session["DateTime"] and set it to the value of the linkbutton. then, redirect to the seating page.
on the seating page, you can check if there's a session. if it finds one, then you can load the seating chart or purchasing page. if it does not find one, you might want to redirect them back to select a time.
HTML example:
<h2>Next Big Movie</h2>
<asp:LinkButton id="lbNBM12pm" runat="server" OnCommand="lb_Click" text="12:00pm"></asp:LinkButton>
Code Behind example:
protected void lb_Click(object sender, EventArgs e)
{
string time = (sender as LinkButton).Text;
Session["TimeSelected"] = time;
Response.Redirect("/nextstep.aspx");
}
and on the next page, you'll read what was passed over in the page load and set the Selected Movie time text:
protected void Page_Load(object sender, EventArgs e)
{
if(Session["TimeSelected"] != null)
{
MovieTime.Text = Session["TimeSelected"].ToString();
}
else
{
//user did not select a time
//possibly redirect them back to the showtimes page.
response.redirect("/previousepage.aspx");
}
}
If I understand it correctly. First, create a table with the following columns:
create table Links
(
id int identity,
link varchar(100),
timeclicked datetime
)
then fire an ajax or a simple HTML POST request to submit what link is clicked in a spefic time.

Losing values after refreshing button asp.net

I have the following project :
It's a page that on Page_Load it fills a TextBox named Email and a TextBox named UserName with a value obtained from asking a database.
Then there is this button, if the email is not null(user is not registered) it will let you register, otherwise it will let you change the email linked to your username.
The thing is, when trying to modify the email, doing an update query, the page preloads, taking the new value placed on Textbox Email the same that is retrieved from the database, making so it will never change.
I've tried to see if it executes the query and it does.
I've tried everything, keeping the variable on a hidden label, creating two different buttons with no luck as when it reloads the code those values are empty again.
I was thinking if I could keep the variable somehow that isn't cookies.
I think You know What is happening.. On every Post back the Page_Load event resetting your Textbox Value
Use IsPostBack to bind the value only on 1st load of page
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//bind dropdown and fill textbox here
TxtName.Text = "Your values";
GetDropdowns();
}
}
I hope this will solve your issue
I totally agree with Kanis XXX, you can use IsPostBack to fill the values only at the start page, and not on other postbacks. In my experience, there are some other advices for your problems:
Using Viewstate, Session state,... to keep your working variable. You can have more detail here: https://kimphuc.wordpress.com/2009/10/18/the-difference-between-viewstate-sessionstate-cookies-and-cache-in-asp-net/
Try to use UpdatePanel, this could be useful in some cases, let you refresh or update data just a part of your page, not the whole page. https://msdn.microsoft.com/en-gb/library/bb398864%28v=vs.100%29.aspx

Windows Phone back button

I need to go back to a particular page when I click on a button. more like custom back button.
I can go exact page, if I know what is the page is,
private void button_click(object sender, RoutedEventArgs e)
{
Frame.Navigate(typeof(Page1));
}
Also I can go back to the previous page by
if(this.Frame.CanGoBack)
{
Frame.GoBack();
}
But how can I jump to the page before the previous page at once?
Any record of page history?
You can use NavigationServices.BackStack to access on an history of your previous pages.
But you have not specific method to go back from 2 pages at once. However, if you would jump to the page before the previous page you have just to called NavigationServices.GoBack() two times.
Moreover, if you would go back to a specific page, you can know its index between backstack and make a loop with the GoBack method.
You can get all pages from BackStack as a List :
var lstAllStackPages = this.Frame.BackStack.ToList();
and make a for loop to get your specific page and navigate to it.
Thank you #CoderDennis, I figured it out. hope it will help to someone else.
I wanted to move back to the Main page if I have come along the Main page,
foreach (PageStackEntry stack in Frame.BackStack)
{
//check if stack contains history of the main page. (Solution name: Helloworld)
if (stack.SourcePageType == typeof(HelloWorld.MainPage))
{
Frame.Navigate(stack.SourcePageType);
break;
}
}

Label's text appeared nothing when is session over from the first page

I am trying to Session over my Label.Text in the first .aspx page to another Label in the second .aspx page. I retrieve my value from the database and place them in the first Label.Text and want to Session this Label (Which i got the text from the database) to another page linked to the first page. I use this method is because i have a detailed products page (My first .aspx page) which consists of many products and when user click a particular product, it's product name have to be displayed in the second page of the .aspx . My problem here is that when i Session over the Label's text on the first page, the value does not pass over to another page.
My first page's .cs code (code behind):
Session["productName"] = productName.Text;
Response.Redirect("products2.aspx");
My second page's .cs code (code behind - I place this code in page load)
if (Session["productName"] != null)
productName.Text = Session["productName"].ToString();
Have i gone wrong anywhere?
Inorder to make the session not to be expired for a long time, you should follow two steps.
Keep a continous eye on the Session Timeout.
Redirect the session when its about to expire.
The Base Page for Detecting Sessions will explain you every thing you need to do, please refer it once.
your code is totally right..
but my question is are you using ispostback in your page load??
if not then use it like this..because of postback sometimes you can't get values.
then put your code if is not postback
page_load()
{
if(ispostback)
{
}
else
{
//put your code here
}
}

Submit a form from code behind

I'm having trouble implementing a functionality on my c#/asp.net app.
I have a form with a RadioButtonList and a submit button.
The RadioButtonList is generated on Page_Load() from a list of objects I retrieve from the database.
I would like to automatically submit the form if there is only 1 object in the list.
I have access to my Form object, to the submit button etc... but I can't seem to find a solution (in the end I'm looking for a kind of form.Submit() ) method.
Does anyone have an idea of how I could do this ?
Thanks in advance !
EDIT >> Here is the code :
.aspx : http://pastebin.com/0E6T7dqH
.aspx.cs : http://pastebin.com/54payZJP
EDIT2 >>>
As it seems there is no way to do what I wanted to do at first, I ended up using a session variable with a response.redirect()
Source :
http://dotnetslackers.com/Community/blogs/haissam/archive/2007/11/26/ways-to-pass-data-between-webforms.aspx
Post happens in the client side. As in Page_Load you are currently executing in the server side, just call the code you want to execute on post.
Edit: For actually going to another aspx
public void Page_Load(object sender, EventArgs e) {
if(!IsPostback && OnlyOneItem) {
Server.Transfer("TheOtherPage.aspx");
}
}
Server.Transfer will maintain the entire request, so your post data will be available.
http://msdn.microsoft.com/en-us/library/system.web.httpserverutility.transfer.aspx
Try something like this
In your Page_Load
if(!IsPostBack)
{
if(check for only one object)
{
//your submit code
}
}
I actually had to do something similar, once. Here is a way you can do it.
Asp.Net buttons have a property called PostBackUrl, and it does exactly what you would expect - it controls where the form will post if you click the button.
You can also use the RegisterStartupScript function to render javascript on the page.
Now, with these two pieces, you can achieve your goal.
if(!IsPostBack)
{
if(results == 1)
{
button.PostBackUrl = "next page's url"
//Register script to click the button using RegisterStartupScript
}
}
Now, having shown you this, I will warn you it may not make for the best user experience. When I did it, it was for a very specific case that had no other solution. The page will actually post back to the user, and they will see the page for a moment before the javascript to click the button takes effect. Additionally, when you set a button's PostBackUrl, that means that when it is clicked, your entire form will be posted to the page specified. The code behind for the current page will not fire at all, so if you have any validation, it won't run.
There's nothing wrong with letting the user click the button to submit the form even if they only have one choice. In my experience, users like to feel like they are in control on the system; they don't like it when pages just do things without their input.
Also, there is not really anything wrong with putting the information the next page needs into the session or even a database table, and using Response.Redirect. It's a fairly common practice and works reliably in most scenarios.

Categories

Resources