Context rewritepath problem with form C# - c#

i have a problem when i RewritePath in HttpContext,
context.RewritePath(Utility.WebRoot + "List/Add.aspx", false);
It work fine to rewrite the url: http://localhost/List/Add
But when i hit the button it redirect me to http://localhost/List/Add.aspx
Is there a good way to "stop" the redirection to the .aspx page and just leave it on http://localhost/List/Add ?
Thanks for help

There is an issue with the From tag. You have to use a Control Adapter like this one:
http://weblogs.asp.net/scottgu/archive/2007/02/26/tip-trick-url-rewriting-with-asp-net.aspx
Go to the "Handling ASP.NET PostBacks with URL Rewriting" section.

This will help:
http://www.codeproject.com/KB/aspnet/SmartFormControl.aspx
Basically the idea is to create a new Form control (I called my "ActionlessFormControl") that derives from the .Net Form control. The gist of it is that you override the rendering of the attributes and set your own value for the "action" attribute. What I do in mine is I remove the "action" attribute altogether which will post back to the same URL. Meaning, your page will post back to "/List/Add".
The benefit from using the inherited control is that you don't have to "register" each page. This will allow you to have dynamic content/URLS post back correctly.

Related

Custom controller for the layout.cshtml

I have an MVC website which I wanted to implement a globalization/localization. On my _Layout.cshtml, I have a dropdown which have the languages I supported. When a user selected a language on that dropdown, it should automatically post to the server then render the new language.
Is it possible that I create a specific custom controller for the _Layout.cshtml only? If yes, how? If no, is there any possible way or approach I can make?
Thanks in advance!
You should move that to a child action, then call the child action from the layout view.
You can make the form POST to a separate action (in a controller shared with the child action) that sets the cookie / session / DB property, then redirects back to the original URL (via Request.UrlReferrer or from a hidden model-bound field).

Remove GET variables on URL when doing a submit

I have this problem: I have a page with a datagrid with links that redirect to the same page, adding variables to the URL. Once the user chooses some options in the form, it submits, once again, to itself.
My issue is that I must remove all those variables in the URL or they disrupt the submit. Any way to clean the URL while doing the POST?
No, the POST will happen with the current querystring.
You could add the querystring parameters to Session and negate the need to pass this in via the URL.
Solved it. Just need to add a PostBackUrl to the button. Sorry if it was a dumb question.

Response.Redirect disables back button

I am using using Response.Redirect to transfer control to another page. but this disables back navigation in the browser. What is alternative way to achieve this ?
Response.Redirect does not disable the back button by any means. Check it using web-console or put debug point on your page and check again. I think in your case the browser back button is working but your page may be forcing it to redirect again to the second page
You can do a client-side redirect.
Here is code for 5 different ways to do this with JavaScript
I recommend the first one which is to set the window.location.href property in JS.
You should use Server.Transfer() instead.
You must have added a javascript function somewhere in your project ( like MasterPage or parent page ) which is disabling back button and which is bad from user point of view , otherwise it isn't possible for browser's back button to get disabled on its own just because you have used response.redirect in your code -behind .

<form> action when using Server.Transfer()

I am using Server.Transfer() to transfer processing from one page to another. The problem is that the form action in the source of the page, having been transferred, refers to the destination page and not the original page as per the URL in the browser.
Is there a way to make the action of the form reflect the URL in the browser, rather than the actual destination page?
Thanks in advance!
Mark
Not to worry, I've rewritten my routing code using the System.Web.Routing namespace so all the logic is centralised in my global.asax. Works a treat!
Thanks for your help.
Mark
if you give the form an id you might be able to change the action in the attributes property.
<form runat="server" id="form1" action="">
</form>
and then in the code refer to the form like this:
form1.Attributes["action"] = "new action";
changing the action will probably cause postback events intended for the new page to work incorrectly. Your other alternative if your design permits would be to use the Response.Redirect.
Also, you might want to look into PostBackUrl for the buttons on the new page, which will change the page that they post to.
You should investigate if HttpContext.RewritePath might be useful here. Typically in cases like this you would use rewrite path on the PreRender event of the destination page using the URL of the original page. This causes controls that use the current internal path to generate URLs and such to "think" they are still on the original URL at the time they render themselves.

How do I HTTP POST from an ASP.NET button?

Sorry, another super basic ASP.NET question. this so embarrassing.
I am reading the article on How to: Pass values between ASP.NET pages
In the second approach, they suggest hooking up a button and directing the user to another page using POST. I don't know how to do this. How do I HTTP POST?
"When the source page uses the HTTP POST action to navigate to the target page, you can retrieve posted values from the Form collection in the target page."
This is how I am sending the user to the new page:
protected void btnSubmitForPost_Click(object sender, EventArgs e)
{
Response.Redirect("GetViaPost.aspx");
}
EDIT
The final solution:
You can use ASP.NET webforms. Do the following: On the first page, create your controls and a button that sends the user to a new page. Handle the click event from this button. As stated below, use Server.Transfer with endResponse=false, instead of Response.Redirect(). When you use Response.Redirect, your post data is cleared out. I did not need to specify action in the form or anything else.
In ASP.NET when you click a button, you're posting the entire page's fields by default (as it's contained within a gigantic <form /> tag last time I checked. You can access these values after clicking the button like this:
string MyPostedValue = Request.Form["MyFormField"];
*Edit as per your update in your question, change Response.Redirect() to Server.Transfer() like this:
protected void btnSubmitForPost_Click(object sender, EventArgs e)
{
Server.Transfer("GetViaPost.aspx", true);
}
Then in your GetViaPost.aspx's page you can get any form/query string variable you passed from your sending page like this:
string MyPostedValue = Request.Form["MyFormField"];
If I'm reading this right, all of these answers are missing the question...
You're looking at posting from one Asp.Net form to another, and one of the methods is what you want to figure out - doing a normal http post. The book or article probably is already telling you about the Server.Transfer as another option if I'm guessing right.
If I'm getting the question right, then the simplest answer is to not use a standard ASP.Net form (with the runat = server attribute) as the starting point, but to use a simple standard html form to post to an asp.net page
<form action = "targetpage.aspx" method="post">
...some form fields here
<input type = "submit">
</form>
If in the codebehind you wire up to the button click event, then click the button. It's a POSTback that happens.
Any controls that you have runat="server" will be accessible by their id (and any values set on them) in the codebehind.
In terms of posting data to other pages, you have a number of options available to you.
The querystring, sessions, cookies and viewstate.
A basic example (with no error handling) given your updated Response.Redirect might be:
int someId = int.Parse(txtBoxOnThePage.Text);
Response.Redirect(string.Format("GetViaPost.aspx?myId={0}", someId));
Then on the GetViaPost page you could pull that out by:
HttpContext.Current.Request.QueryString["myId"]
http://www.asp.net/learn/ is a surprisingly good source of information and tutorials for this kind of learning.
ASP.NET buttons always perform a POST. You can set which page the button posts to using the PostBackUrl property of the button. If you leave this blank, the button will post back to the same page that is resides on.
Check out this article for more information.

Categories

Resources