My code looks like:
protected void Page_Load(object sender, EventArgs e)
{
if(IsPostback)
{
if(/* User is not authenticated to perform any actions on this page */)
{
Response.Redirect(/* Error Page */);
}
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
/* do some stuff */
}
I would have guessed that if I submit the button to do a postback and I was not authenticated, then in the Page_Load the redirect would happen and would send back an HTTP 302. However, it appears that it runs the Response.Redirect line of code, keeps processing past it in Page_Load, and then even runs btnSubmit_Click afterwards. The response is not the error page.
I can put a return call after the Response.Redirect to prevent further processing in that function, but the btnSubmit_Click function will still get run.
Why would it work this way?
To prevent more code from processing, use this:
Response.Redirect(url, true);
The second parameter will end further processing instead.
Related
whenever I am clicking the logout button it is going to the logout page but Timer1_Tick function is not working and the execution is being stopped in the logout page.It is not going to GuestUser_HOME.aspx page.
The code in the logout page is here.
public partial class WebForm24 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1));
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetNoStore();
}
protected void Timer1_Tick(object sender, EventArgs e)
{
//string redirectUrl = FormsAuthentication.LoginUrl + "?
ReturnUrl=index2.html";
FormsAuthentication.SignOut();
Session["email"] = null;
Response.Redirect("GuestUser_HOME.aspx");
}
}
Are you sure your timer had started !?
check IsEnabled property before runtime to find out it`s enable or not.
but a better solution is like below ,
first :
Create a function for implement sign out operation.
second :
use this function in right place , for example in Click Event of button or Page Load event of sign out page.
third :
if you want implement this operation in a TimerTickEvent , you must stop timer after complete log out operation , and check if user logged in run SignOut operation not e
Is there a way to intercept asp.net ajax webmethods(aspx page static methods)?
i want to intercept before request reaching the method and also after sending the response.
use in global.asax file
protected void Application_BeginRequest(Object sender, EventArgs e)
{
//before method colling
string ss = HttpContext.Current.Request.Url.ToString();//use if it is equal to your page webmethod url i.e. domain.com/dfault.aspx/GetMessage;
if(ss=="http://domain.com/dfault.aspx/GetMessage")
{
do your work here
}
}
protected void Application_EndRequest(Object sender, EventArgs e)
{
//after method colling
string ss = HttpContext.Current.Request.Url.ToString();// use if it is equal to your page webmethod i.e. domain.com/dfault.aspx/GetMessage;;
if(ss=="http://domain.com/dfault.aspx/GetMessage")
{
do your work here
}
}
you can try fiddler or HTTtamper.
Sometime ago, I asked about "how to send an alert and reload the page" and I found the answer Here
But now I need to send this alert/ message of confirmation like New Registration Successfully done AND redirect to a different page.
And I have no idea how... The way I used to do, will only reload the currently page, and if I make a Response.Redirect("PageName.aspx"); it will NOT show the alert.
What could I do?
UPDATE
Now, I found this window.location.href = 'pagina.aspx'; But I don't know if it is a good practice or how to use this, on codebehind, this method does not exists...
You want to load script from code behind.
Page without Ajax
protected void Submit_Click(object sender, EventArgs e)
{
// Do something
Page.ClientScript.RegisterStartupScript(this.GetType(), "redirect script",
"alert('New registration successfully done!'); location.href='pagina.aspx';",
true);
}
Page with Ajax
protected void Submit_Click(object sender, EventArgs e)
{
// Do something
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "redirect script",
"alert('New registration successfully done!'); location.href='pagina.aspx';",
true);
}
I have this weird thing going on in my application.
At the moment, I have 'Trace="true" on my aspx directive and all Response.Redirect() codes are working fine. But as soon as I remove this directive, all the Response.Redirect go to the Home page of the site.
Any help will be appreaciated.
Here's the code:
protected void SearchSubmit_Click(object sender, EventArgs e) {
Response.Redirect( "~/?view=Search+results&search=" + Server.UrlEncode(SearchText.Text), true);
}
protected void AdvSearchSubmit_Click(object sender, EventArgs e) {
string filtertype = ""; if (rbFilter.SelectedValue != "") filtertype = "&f=" + rbFilter.SelectedValue; Response.Redirect("~/?view=Search+results&search=" + HttpUtility.UrlEncode(advSearchText.Text) );
}
I'll bet an exception is occuring and you're being redirected to the home page via your application_error method in Global.asax.
Since response.redirect() aborts the thread this is the likely outcome.
I need to insert a bit of Javascript into the process when a Web Form is submitted, but after the client side validation takes place.
RegisterOnSubmitStatement seems to place the javascript before the validation.
Anyone know how to get it to render after?
Solution found:
In a web control, I put something like this:
protected override OnInit(EventArgs e) {
Page.SaveStateComplete += new EventHandler(RegisterSaveStuff);
base.OnInit(e);
}
void RegisterSaveStuff(object sender, EventArgs e) {
Page.ClientScript.RegisterOnSubmitStatement(typeof(Page), "name", "JS code here");
}
that´s right, the RegisterOnSubmitStatement DO NOT WORK in the init function.
It should be called after in the page lige cycle.
I thing the right place therefor is:
"PreRenderComplete"
protected override OnInit(EventArgs e)
{
Page.PreRenderComplete+= new EventHandler(Page_PreRenderComplete);
base.OnInit(e);
}
void Page_PreRenderComplete(object sender, EventArgs e)
{
Page.ClientScript.RegisterOnSubmitStatement(typeof(Page), "name", "JS code here");
}
After some research online and playing around with it, I figured out that you can do it by hooking into the Page's SaveStateComplete event. I guess the validation submit statement is registered during the PreRender event, so if you register it after that (in the SaveStateComplete event) you can get it afterward.
You do have to re-register it, but that's not a big deal because I'm not relying on ViewState for my JS.