ASP.net link to other page with passing parameter - asp.net

my question is, how to call another aspx page from same project, with passing parameter, like ID-20?
Thanks.

In the source page, you can pass data with query string
.../target.aspx?Id=20
Then on target page get those values
String s = Request.QueryString["Id"];
Notice that you shouldn't pass any sensitive data by this method

Response.Redirect( "another.aspx?id=20" );

Related

Pass value via URL to ASPX page

I am trying to pass a value to a box on an ASPX page. How can this be done? For example, the following page.
http://irth.digsafelynewyork.com/IRTHOneCall/Ticket/PositiveResponse/PositiveResponse.aspx?ID=
It appears as though it will allow passing the ticket id at the end of the URL, however it will not work.
What you are trying to do is passing a value using the query string. Like in your URL, you are passing a value using query string parameter "ID".
In yoru aspx page, you can read this value using Request object and then assign this value to textbox.
string ticketId = Request.QueryString["ID"].ToString();
Textbox1.Text = ticketId;

Pass parameter from 1 page to another using post method

I have a page "Demo.aspx". I need to set some parameters using post method and redirect the page to "DemoTest.aspx".
Is there any way to set parameters in post method in asp.net? I don't want to set "Querystring" due to security propose.
Also I need server side code for this. I am not able to use "Javascript" or "Jquery" for the same.
Here is some more description for the same.
Right now I am using Response.Redirect("www.ABC.Com/DemoTest.aspx?P1=2"). So the page is simply redirect to the given URL.
Now I don't want to pass that "P1" in "Querystring". Instead of query string I want to use Post method.
Please note that redirection page is not in my own application. So I cant maintain session or "Viewstate".
Thanks in advance.
Use a session variable and response.redirect to the next page.
Session["MyVariable"] = "someThing";
Response.Redirect("DemoTest.aspx");
The value stored in Session variables will be accessible across application.
you can store in session like this :
Session["id"] = "anyID";
To get values On another page you need to write
string id = Convert.ToString(Session["Id"]);
However
By default, in .NET pages post() do the things automatically.
You will need to do sumthing like this:
Server.Transfer("DemoTest.aspx", True)

How do I identify the referrer page in ASP.NET?

In VS2003, I am trying to find out the particular page where the request is coming from. I want to identify the exact aspx page name.
Is there a way to only get the page name or some how strip the page name?
Currently I am using the following instruction...
string referencepage = HttpContext.Current.Request.UrlReferrer.ToString();
and I get the following result...
"http://localhost/MyPage123.aspx?myval1=3333&myval2=4444;
I want to get the result back with out any query string parameters and be able to identify the page MyPage123.aspx accurately...
How do I do that??
Instead of calling .ToString on the Uri, use the AbsolutePath property instead:
string referencepage = HttpContext.Current.Request.UrlReferrer.AbsolutePath;
This should get you "/MyPage123.aspx" in your case.
Edit: Had LocalPath instead of AbsolutePath by mistake
Look at the Segments property of the URI class (which is what HttpContext.Current.Request.UrlReferrer returns).
Something like HttpContext.Current.Request.UrlReferrer.Segments[1] (changing the 1 indexer to get the correct segment you require).

Server.Transferrequest() and getting the current URL

Say in my 'Page_init()' of "a.aspx" i just have 'server.transferrequest("b.aspx").
This works great, displays the content for "b.aspx" and the browserurl still stays at "a.aspx".
Happy days.
However does anyone know how to see this url from my "b.aspx" (the resulting page)?.
The usual request.rawurl and request.url.absoluteuri both return the current page as "b.aspx".
Server.TransferRequest performs an asynchronous execution of the specified URL. This means that your client has no clue of was is going on at the server so from your client perspective it's the same page.
If you need to change the actual page (which is the most common) then use Response.Redirect.
Maybe before you do the transfer you could save the information you need somewhere, then retrieve it when it's needed again.
You can use PreviousPage to get source page that makes server transfer :
string previousPagesUrl = PreviousPage.Request.RawUrl;
EDIT : #maxp, as an answer to your comment, PreviousPage only works for Server.Transfer and cross page postback.
You'll get null for PreviousPage if :
the source page redirects to the destination page.
a link at source page forwards the page to destination page.
NameValueCollection headers = new NameValueCollection();
headers["RawUrl"] = HttpContext.Current.Request.RawUrl;
Server.TransferRequest("b.aspx", true, null, headers);
And then use Headers["RawUrl"] in b.aspx.
Have you tried this method:
public void Transfer(string path, bool preserveForm )
http://msdn.microsoft.com/en-us/library/caxa892w.aspx
I currently got in the same problem, and I found out that Server object has this parameter on transfer method that gives you the posibility to preserve the original request form or not.

Asp.Net System.Web.Routing Find actual .aspx Page

I'm using System.Web.Routing to have some better URL's and have come across a problem. I need to know the actual page that's handling the request.
for example a request comes in as:
/basketball/home
I need to find the page that handles that request, like:
/management/default.aspx
I'm only using the System.Web.Routing and not MVC. I have a handle to the RequestContext that contains some of the route information, but i don't see what i need.
Thanks in advance.
******* UPDATE *******
I was able to use Context.CurrentHandler which give me "ASP.management_default_aspx", not exactly the page but enough to get the page name.
There is actually another simple way to get the actual page:
String vPath = ((System.Web.Routing.PageRouteHandler)Page.RouteData.RouteHandler).VirtualPath
Do not forget to check Page.RouteData.RouteHandler is not null - while you are getting the page w/o ASP.Net routing but directly.
Can you not retrieve this from the current HttpContext object?
Perhaps something like this:
public string GetCurrentPageName()
{
string sPath = System.Web.HttpContext.Current.Request.Url.AbsolutePath;
System.IO.FileInfo oInfo = new System.IO.FileInfo(sPath);
string sRet = oInfo.Name;
return sRet;
}
UPDATE:
Have you tried this article?
How to: Construct a URL from a Route
You should be able to retrieve it back from the Routing table you have constructed.
vhinn terrible's answer worked...
Page.AppRelativeVirtualPath
You just have to remove the initial tilde ("~"), and you're ready to go.
var path = Page.AppRelativeVirtualPath.Replace("~", String.Empty);
I don't know why it was downvoted. Worked for me like a charm.
Try using this code:
Page.AppRelativeVirtualPath
I was able to use Context.CurrentHandler which give me "ASP.management_default_aspx", not exactly the page but enough to get the page name.

Resources