asp.net hyperlink - click to make the POST request - c#

<asp:HyperLink ID="hlBanner" Target="_blank" style="padding-left:10px;" runat="server" ImageUrl="banner.png" />
I want to send some infomation such as FName, LName & Email into the POST request to another page ProcessInfo.aspx. This processing page pulls the values from the posted form like Request.Form["FName"]. I have to use only the POST technique, because i cannot make the changes to the ProcessInfo.aspx.
I cannot use the Querystring parameters to pass the info. I was hoping to use the WebRequest class to make the redirection to the second page.
How can i build the navigateURL property for making the POST request ?? Pls suggest. I am open to change the control also.

If you want to use Request.Form Collection here is a good solution.
http://www.c-sharpcorner.com/UploadFile/desaijm/ASP.NetPostURL11282005005516AM/ASP.NetPostURL.aspx

Use the Server.Transfer to call ProcessInfo.aspx.
If you have the inputs FName, LName & Email on your current page, you will receive the values you ProcessInfo page too.

i found the easier way and is already present on stackOverflow.com
https://stackoverflow.com/a/6440159/86023

Related

Redirecting to a page hiding query string parameters asp.net

I am working for a project which has web garden scenario and cannot keep any data in session/inmemory.
The asp.net page opens from Appian(bpm tool) and we pass id through query string.
Now, client is asking me to hide the query string parameter after reading it. But, in that case say, landing page is http://a.aspx?id='123' and after reading that value we have to redirect to b.aspx without exposing the id(query string).
Please suggest me a suitable way to achieve this. I am not really getting any idea for this.
you can add key/value pairs to the header, it won't be visible in the querystring.
HttpContext.Current.Response.Headers.Add( key, value);
and
string headerVal = HttpContext.Current.Request.Headers[key];
You can use session variables on the server side or http Post instead of GET.
Session["id"] = id;
And on load of b retrieve it.
To use Post you can use a hidden field and a form.
//You can set it like this
<form name='IdForm' action='b.aspx' method='post'>>
<asp:HiddenField id="WhateverId" runat="server" value='<%= Request.QueryString["whateverID"] %>' />
</form>
On redirect use javascript to post
function Redirect() {
document.forms["IdForm"].submit();
}
You must use this js script wherever the redirection happens.
And finally on b.aspx code behind
HttpContext.Current.Request.Form["WhateverId"]

Access web control values

I have two pages in ASP.NET 3.5 and I need to access/read the web controls values from the first page but on the second page. The second page is being displayed with a single link, there is not a post event or something like that.
I guess I should use ViewState but it looks so complicated for this task so please let me know a better way to achieve this.
P.S I'm using C# and Visual Studio 2010
If I understand correctly you have two .aspx pages and you want one page to share information with the other page. Does the first page link to the second page?
I ask because there are a couple of approaches you could take. You could add parameters to a query string in the link to the second page with the information you are trying to send. You could also use the session to temporarily store the information.
For example:
<asp:HyperLink NavigateUrl="www.<yoursite>.com/firstpage.aspx?eggs=1&bacon=yum" Text="Awesome Site" runat="server" />
In the second page you would have this in the codebehind in the Page_Load
string eggs = Request.QueryString["eggs"];
string bacon = Request.QueryString["bacon"];
Now you have the value from page one available in page two.
Another approach might be to use the Session like so:
Page one:
Session["bacon"] = "Yum";
Page two:
string bacon = (string)Session["bacon"];
However, I would advise against overusing session to pass information between pages.
Quick & "Dirty": A session variable which holds the info to pass.
On the first page:
Session["ValueToPassToOtherControl"] = "The value";
On the second page:
var value = Session["ValueToPassToOtherControl"];
Elegant: You need to manage your state in any way (via a static manager whose function is to store and retrieve that info, but that will be also variables). Problem is HTTP is stateless. So you need to bypass this limitation via some kind of storing and retrieving of the data.
You suggested the use of ViewState but forget it, ViewState is the technique used by an ASP.NET Web page to persist changes to the state of a Web Form across postbacks which isn't what's happening on your scenario.
There is a better way that using the QueryString jugglery and Session values.
You could just use the previous page property that is set during cross page posting.
Use an asp link button:
<asp:LinkButton runat="server" id="myLink"
NavigateUrl="~/Page2.aspx"
target="_blank" Text="Go to page 2"></asp:LinkButton>
Then on Page2.aspx.cs:
Get the values from the Page.PreviousPage as follows:
TextBox txtUser = (TextBox)Page.PreviousPage.FindControl("txtUser");
TextBox txtSomeValue = (TextBox)Page.PreviousPage.FindControl("txtSomeValue");
Use these as you require in your second page.

asp.net redirect to another location than set in navigateURL

in my asp.net website I have menu control and there are those items:
<asp:menuitem ... navigateURL="~/Admin/admin.aspx">
<asp:menuitem ... navigateURL="~/Admin/search.aspx">
I want force the asp.net think (or display in web browser adress bar), that I am on admin.aspx, but actually be redirected to search.aspx. Is it somehow possible ?
Thanks.
Doing that with the markup you showed, no, it's not possible. You'll need to either use an <iframe> setting the src property via javascript or you do it from code-behind using Server.Transfer as opposed to Response.Redirect.
URL rewriting sounds like it's what you are after:
http://weblogs.asp.net/scottgu/archive/2009/10/13/url-routing-with-asp-net-4-web-forms-vs-2010-and-net-4-0-series.aspx
Failing that you might be able to build some custom logic into your page and use Server.Transfer to change page, without changing the URL.
You can setup customized routing if you would like, but that will still display "search.aspx" in the address bar. If you want to fake the admin.aspx as search.aspx, then add your search controls in to the admin.aspx page and hide the admin controls. If you are trying to "spoof" admin.aspx, like you would an e-mail address or something, then I would direct to not StackOverflow.

Change url on server side

When user clicked on A tag ( <a href='showSomething.aspx?id=11&key=fixed'>Bring Something</a> ) showSomethingPage.aspx will be shown with URL parameters. But I want to change URL from showSomething.aspx?id=11&key=fixed to showSomething.aspx?key=fixed in despite of shown the thing with id 11.
Try saving the ID in the session state and then using Response.Redirect.
Use a redirect:
C# code (put this in Page_Load or something):
Response.Redirect("showSomething.aspx?&key=fixed");
If you just want to hide the URL from the status bar your link, use the onclick event to open the URL:
a href="javascript:void(0)" onclick="window.location='showSomething.aspx?id=11&key=fixed'"
Hope it helps you. ;)
You can save the id in the session and then redirect to showSomething.aspx?key=fixed. then showSomething.aspx?key=fixed will read the session and file the id=11 and show it.

How to remove query string parameters from the url?

Assume I have the link http://www.somesite.com/file.aspx?a=1&b=2
And now I want to remove all the parameters, so it becomes:
http://www.somesite.com/file.aspx
Or I may want to remove only 1 of the parameters such as
http://www.somesite.com/file.aspx?b=2
Is there a way to do the above in C#? What is happening is that I am coming from another page with a parameter called edit in the url, but when the page does a post back, the edit parameter is still there, so it still thinks it is in edit mode. Example:
User A goes to page one.aspx and clicks on an edit link. They are taken to two.aspx?edit=true. During page load, it sees the the query string parameter edit is not null and it puts the contents in edit mode. Once the user is done editing, the page is refreshed, but the url is still two.aspx?edit=true and keeps the contents in edit mode, when in fact it should be two.aspx
Request.Querystring is read-only collection - You cannot modify that.
If you need to remove or change the param in querystring only way out is to trigger a new GET request with updated querystring - This means you will have to do Response.Redirect with updated URL. This will cause you lose the viewstate of the current page.
Use the PostBackUrl property, for example:
<asp:Button ID="DoneEditingButton" runat="server" Text="Done editing" PostBackUrl="~/two.aspx" />
When you are done with the edit you are doing a post back so just define the action to post to two.aspx instead of just posting back to itself that way it will drop off the get parameters.
How about checking Page.IsPostBack to see if the current request is a postback or not?
if you have only string, you can use:
strinULR.Split('?').First();
or
strinULR.Split('?').FirstOrDefault();
Try something like this.
if (url.Contains("?"))
url = url.Substring(0, url.IndexOf("?"));
In this example, I'm checking if the url even contains a query string, and if it does, subtracting getting the "left part" of the string prior to the ?.
Late but you can do this to remove query string from URL without another GET Request.
http://www.codeproject.com/Tips/177679/Removing-Deleting-Querystring-in-ASP-NET

Categories