I've got a login button with he following code.
protected void prv_Click(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
if (!HttpContext.Current.Request.IsSecureConnection)
{
string postbackUrl = HttpContext.Current.Request.Url.AbsoluteUri.Replace("http", "https");
Response.Redirect(postbackUrl);
}
}
login_box.Visible = true;
}
The problem is, if the user is browsing using http and clicks on login link which fires off the prv_Click, the site redirects you to https which is correct, but the login_box which is standard div set to visible false and run at server never gets set to true. The user has to click on the login link again which then expands it.
Any help would be appreciated.
Don't set the visibility on the button click, set it on page load. Something like this should work:
login_box.Visible = HttpContext.Current.Request.IsSecureConnection;
Note that it shouldn't be in an IsPostBack. This will always ensure that it's only visible when you have a secure connection. If it needs to be hidden for any other reasons, then you'll need to modify the expression accordingly.
Related
So I read up a lot about how to properly use Response.Redirect without causing some errors.
Response.Redirect(url, false);
Context.ApplicationInstance.CompleteRequest();
Situation: A user logs in the site and visits a page that lists forms. The user sits on that page for an hour doing nothing (which would log them out). The user clicks a button to edit a title (which requires a postback). This results in the user object being null since it logged him out of the session.
What I have done: In the page load, check if the user object is null and if it is, redirect to the login page. That is what should happen right?
Problem: The button event is STILL firing. Is it because I have the following line of code?
Context.ApplicationInstance.CompleteRequest();
How can I stop the event from being firing when I simply want to redirect to the login page?
I know I can provide true in the redirect code, but that will cause an error too right?
Response.Redirect(url, true);
What I currently do (which is not the best way I know): In the button event, I again check to see if the user object is null. If it is not null, proceed with the code to edit the title (recording who edited it). This is a bad way of handling this.
What I have seen: Wrap all events with Response.IsRequestBeingRedirected. But if I have several pages and lots of button events, this can get a bit annoying. Is this the only way to handle this situation?
Example:
protected void Page_Load(object sender, EventArgs e)
{
Account account = Session["Account"] as Account;
if (account == null)
{
Response.Redirect("WebForm1.aspx?NewSession=true", false);
Context.ApplicationInstance.CompleteRequest();
return;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Account account = Session["Account"] as Account;
Label1.Text = account.userID.ToString();
}
Again, if I am already on the page, and the session expired, and I click Button1, the Button1_Click method is still being called. What I provided will give me a "NullReferenceException". Page_Load IS being called, and it is making the redirect. However, Button1_Click is still being called even with the return statement in Page_Load.
Thanks!
You are correct about Response.Redirect(url, true). It is kind of expensive because it throws ThreadAbortException causing the current thread to terminate which is not what you want most of the times.
There is even a KB exists on this topic KB312629.
And yes, with your code it is not possible to prevent events from firing for the current IHttpHandler (which Page class implements) using built-in classes/methods.
So I would suggest to create base page class and add a bool field indicating about request completion request plus method that will call ApplicationInstance.CompleteRequest and will set the flag to true. Also you'll need to override RaisePostBackevent method to take control over controls (not Page) events invocation on page.
public abstract class BasePage : Page
{
private bool shouldNotRaiseEvents;
protected void CompleteRequest()
{
shouldNotRaiseEvents = true;
Context.ApplicationInstance.CompleteRequest();
}
protected override void RaisePostBackEvent(IPostBackEventHandler sourceControl, string eventArgument)
{
if (shouldNotRaiseEvents)
{
return;
}
base.RaisePostBackEvent(sourceControl, eventArgument);
}
}
Your updated code would be:
Response.Redirect(url, false);
CompleteRequest();
Also please note that if you have something inside LoadComplete, PreRender, SaveViewState, Render, Unload event handlers and you don't want this code to execute after redirect call you'll need to override that methods also.
I've been racking my brain over this for a while, its been a while since I had to do this and know its possible I've done it in another project in the past that I don't have a backup of to refer to.
I have a Login View on a page inside the login view is 2 panels one panel with a login control (to login) and one panel with a createuserwizard (to register) and a second button to click to register.
I'm trying to hide the panel with the login control and show the panel with the register control via a button click but all I end up with is a null reference exception.
this is what I have currently.
protected void Register_Click(object sender, EventArgs e)
{
FindControl("LoginView1").FindControl("LoginPanel").Visible = false;
FindControl("LoginView1").FindControl("RegPanel").Visible = true;
}
I appreciate any help thanks.
I figured out what the problem was so I'll leave the question here for anyone who may have the same problem and stumble across this
I was so used to working with controls from a master page but within a page that's inside the master page you don't need the first findcontrol its simply:
protected void Register_Click(object sender, EventArgs e)
{
LoginView1.FindControl("LoginPanel").Visible = false;
LoginView1.FindControl("RegPanel").Visible = true;
}
I have a ListBox with a "list of servers" that has AutoPostBack enabled and an SelectedIndexChanged event attached to it:
protected void lbServerList_SelectedIndexChanged(object sender, EventArgs e)
{
if ( lbServerList.SelectedValue.ToString() != "")
{
Response.Redirect("detail.aspx?Server=" + lbServerList.SelectedValue.ToString());
}
}
Then I have a textbox to add a "server" with a button "btnServertoAdd" (to execute the addition)
protected void btnServertoAdd_Click(object sender, EventArgs e)
{
Response.Redirect("add.aspx?Server=" + tbServertoAdd.Text);
}
Scenario: If I select an item from the ListBox it will go to detail.aspx showing the server specs: Awesome.
Now, If I click back (browser button) and then type something in the TextBox and click btnServerToAdd it will still go to detail.aspx and not to add.aspx as it should....
How can I fix this?
Let me know if more code is needed.
This is occurring because when you click the button, the selected server is also different from the original value (as stored in the view state). Both events are fired, but evidently the SelectedIndexChanged event is fired first and the Redirect skips the rest of the processing.
I can't think of how to not make the SelectedIndexChanged event fire the second time around, so instead what you could do is, instead of Redirecting in the events themselves:
Have a pair of bool member variables in your page class.
Set one to true in each event handler.
In the page OnLoadComplete event, check each and redirect as necessary:
If both are true, redirect to add.aspx.
If one is true, redirect to the corresponding page.
Otherwise don't redirect at all.
I have the below code
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//do something
}
else
{
// do something else
}
}
protected void Button1_Click(object sender, EventArgs e)
{
//do something
}
}
The point is that a post back happens if I press F5/refresh button or a button click. How will I prevent the code from doing any action if F5/refresh button is clicked?
I have checked Detect F5 being pressed and Refresh but the solution of mine will be different as I need to do this in C# code.
Thanks
You are trying to capture something on the client - so it must be client side script (as discussed in the link).
It's not a postback in ASP.Net terms - your page is simply being requested again (GET). You cannot stop this - its just like going to some other page on your web site and clicking back through some navigation.
If you are saying you want to prevent some type of server side code you have from being run (more than x times) then you can think about sessions or cookies and read them in before you run whatever process. A simplistic sample:
visit page 1 - set session or cookie that identifies page 1 process was run
visit page 2 - set session or cookie that identifies page 2 process was run
return to page 1 - check for existence of session or cookie variable, and if exists, don't run page 1 process.
Another option, if viable is to use ASP.Net caching.
I want to set focus to an element (a button) after the user clicks on Submit, but the focus is not on a new page it is in the same page. What is happening is that when they click submit i am evaluating a few conditions.. if one of them is met I send them back to the same page (or do not re-direct) in other terms but it still postsback so, when it postsback I again after the submit, i want to set the focus to this item.. how can i do this?
So you want to set the focus of the page to a WebControl on the page? i.e. a <asp:Button>?
You can use ControlId.Focus()
EDIT
If you are talking about the Scroll position after postback, then you could try this
<%# Page MaintainScrollPositionOnPostback="true" %>
Now I have never used it before or tried it, so not sure if it works.
Despite your question doesn't seem too clear to me, I am assuming that you want to set focus to the asp.net control, so you can do
protected void Page_Load(object sender, EventArgs e)
{
if(Page.IsPostBack)
{
if(vartemp === vartemp2) //assuming that you want to set focus when specific condition meets
myButton1.Focus();
}
}
Use a hiddenfield and set its value with the controlId of the control you are validating before making the postback. and in the pageload event you can check the value of the hidden field and focus that control.
Or you can use validationcontrols for validating.