I have 2 page in my application:
First is:"A.aspx" and Second is:"B.aspx"
A.aspx has one button with event as below:
protected void Button6_Click(object sender, EventArgs e)
{
HttpContext.Current.Items["Name"] = "University";
Server.Transfer("B.aspx");
}
In B.aspx :it just writes session data from A.aspx
protected void Page_Load(object sender, EventArgs e)
{
Response.Write(Context.Items["Name"].ToString());alues
}
The issue is when it redirects to "B.aspx" and since i have used server.transfer
the content of "b.aspx" is shown with url as "A.aspx" in address bar which is inconsistent for users.
I want consistent url,means along with content of "B.aspx" ,url should also reflect as "B.aspx".
I tried using Response.Redirect but HttpContext does not work with Response.Redirect as context is getting lost while traversing.
Since i have to implement it in a banking application,So i cannot use querystring ,sessions instead of HttpContext.
So,is there is way to use Response.Redirect with HttpContext or can i manipulate url on page load of B.aspx so as to achieve consistent URL.
Or there is any other way to achieve this.
Related
I'm doing a project for school and i have this page with some search results.
When i go back to the search results page, the browser asks to "Confirm Form Re-submission" and i would like to avoid this.
I haven't done nothing so far to solve this issue besides searching the web for a solution and not had not found one.
Browser message:
Confirm Form Re-submission
This web page requires data that you entered earlier in order to be properly displayed. You can send this data again, but by doing so you will repeat any action this page previously performed.
Press the reload button to resubmit the data needed to load the page.
ERR_CACHE_MISS
The problem lies in that you are doing a post request, and if the person making use of your web page refreshes it, it does what it is supposed to do resubmit the post request.
Hence, there is no way of preventing this issue from happening, what you could rather do is changing the way you obtain the values from a post request to get request.
Hi. Found the solution for my problem.
So what i did was, stored the value in session, redirect to a loop page and back, and just preform the search on form load. Now i can go back and forth with no problem. Here is the code i used:
Search page:
protected void btnSearch_Click(object sender, EventArgs e)
{
Session["searchValue"] = txtSearchValue.Text;
Response.Redirect("loopPage.aspx");
}
Code on LoopPage.aspx:
protected void Page_Load(object sender, EventArgs e)
{
Response.Redirect("searchPage.aspx");
}
search page again:
protected void Page_Load(object sender, EventArgs e)
{
if (Session["searchValue"] != null)
{
...preform search and create objects to present results...
}
}
Here a button that does a postback
<asp:LinkButton runat="server" ID="btnBestPrice" OnClick="btnSearchBestPrice_Click">Search Best Price</asp:LinkButton>
Assume that this button is clicked on page
http://localhost:47207/Default?ClusterId=131
Now after the postback is completed, the page is still
http://localhost:47207/Default?ClusterId=131
However, after the postback, i want to make URL to be
http://localhost:47207/Default
Is that possible?
If i make a redirect, the postback event would be loss. I still want to process the postback event perfectly fine. So if somehow i can set the postback url to be raw url of the page on the client side or?
asp.net 4.5 web forms c#
I assume your postback is displaying some information to user, but have to change the URL without interrupt the process.
First of all you have to understand that if you changed the URL in the server side, the browser will treat it as a new page and make a new request. Response.Redirect is basically telling the browser it is time to move onto another page. So you cannot change the URL while remaining in the same request. (while Server.Transfer is remaining at the same URL but different page which is not what you want)
So I have 2 solutions for you, the following one make sense to me but there is still redirecting the page:
protected void Page_Load(object sender, EventArgs e) {
if (Session["ClusterId"] != null) {
try {
int ClusterId = int.Parse(Session["ClusterId"]);
// Code here
} catch { }
Session.Remove("ClusterId");
return;
}
}
protected void btnSearchBestPrice_Click(object sender, EventArgs e) {
int ClusterId = int.Parse(Request["ClusterId"]);
Session.Add("ClusterId", ClusterId.ToString());
Response.Redirect("~/Default");
}
Here is another solution that does all the actions in your btnSearchBestPrice_Click event without Redirect and Session, and bind a JavaScript page ready event, call history.pushState and also wipe out the unnecessary parameters in the action attribute of your form element.
i have webapplication. In my master page i have few link buttons which are as below
<asp:LinkButton ID="link1" runat="server" OnClick="linkAge_Click">Age</asp:LinkButton>
<asp:LinkButton ID="link2" runat="server" OnClick="linkName_Click">Name</asp:LinkButton>
In my Visual studio i have seperate folder called Age and Name and under those there are default.aspx.
on click event of link button i have this code
protected void linkAge_Click(object sender, EventArgs e)
{
Response.Redirect("/Age/");
}
protected void linkName_Click(object sender, EventArgs e)
{
Response.Redirect("/Name/");
}
In IIS i added Application called "Test" and then added all code inside it
When i browse i get to master page as http://localhost:80/Test
When i click on link "Age" the url Changes to http://localhost:80/Age
I expected it to be http://localhost:80/Test/Age
What is wrong that i am doing? Can i achieve this without using any code changes.
Try to use http://localhost/Test/ (with slash in the end) link in to Your browser.
You need to add a tilde (~) to get URLs relative to application root:
Response.Redirect("~/Age/");
BTW: Why do you need to have the redirect behind a post-back? You could use <asp:HyperLink> instead
Like we do this in mvc to get path from root - Url.Content("~\Action\")
In web form -
protected void linkAge_Click(object sender, EventArgs e)
{
Response.Redirect(Server.MapPath("~/Age/"));
}
You got to think like this i think.
Since you're using only the /Age/ path, with nothing to indicate that you're using a relative path. For code managed by IIS, you'd use ~/Age/Whatever to indicate that you intend to go to Age relative to Test.
There is a requirement in one of my project, where I need to change Master Page during run time.
I mean I need to apply check and on the basis of that check particular master page can be called to my native aspx page.
Please help me out for same.
Thanks in advance :)
For example:
void Page_PreInit(Object sender, EventArgs e)
{
this.MasterPageFile = "~/NewMaster.master";
}
Apply your conditionals as required. From here.
Yes. Set the MasterPageFile property only during the PreInit page event—that is, before the runtime begins working on the request (since the rendering of the page with the master page occurs prior to the Init event)
protected void Page_PreInit(object sender, EventArgs e)
{
MasterPageFile = "simple2.master";
}
If you try to set the MasterPageFile property in Init or Load event handlers, an exception is raised.
Yes it is possible, Implement like below
Dynamically Loading Master Pages in ASP.NET 2.0
To acheive this we need to write code in Page_PreInit before page gets render.
Put below code into your code behind:
if (Session["userType"] == "Admin") //check the user type
this.Page.MasterPageFile = "~/Admin.master";
else
this.Page.MasterPageFile = "~/User.master";
Hope this helps.
I am new in asp.net, My problem is facing logout problem in myappication using master page and window authentication. After i signout from my application if i use browser back button it again goes back to signed page after that i click any control then only it is goes back to the logout stage but i want to dont show the logged page unnessarily.
i am using href,document.location.replace(page),response.write("mypage.aspx") these technique for naviagation purpose and i am using session in all pages.
Note: I am using login and logout in master page top itself...so if i check the session for null redirect to home page which is also a content page then i am not getting the home page it self to login because infinite looping occurs...
when i am searching i got some coding to clear the cache but i a am facing problem that is after i logged and navigate to some of pages then i click browser backbutton without signout it is showing page is expired Click refresh to get the data back....
Finally, I need solution like google signout i.e: after signout from google page then if we use back it shows only the home page. and please tell which event is fired while clicking the browser backbutton if yes how to validate session and redirect to logout page.
Please help me i am facing these problem in one-week....
Thanks in advance to everyone..
Easiest way to resolve this problem is to disable the cache of the page.
This should help you out.
Login Page
protected void Page_Load(object sender, EventArgs e)
{
Session["imp"] = "0";
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
Session["imp"] = "1";
Response.Redirect("AdminHome.aspx");
}
Log Out Page
protected void Page_Load(object sender, EventArgs e)
{
Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1));
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetNoStore();
if (Session["imp"].ToString() == "1")
{ }
else
{
Response.Redirect("HomePage.aspx");
}
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
Session["imp"] = "0";
Session.Abandon();
Response.Clear();
Response.Redirect("HomePage.aspx");
}
You could consider in the Masterpage Page_Load to check for login credentials (however they are implemented in your solution) and if they are not present, to Response.Redirect() to the login or home page.
Edit: I'm not sure whether the OnLoad event is raised when using the back button. This approach may not work.