ASP.NET - Current name of page from web user control? - asp.net

I need to find out name of location where web user control is. Somethnig like HttpContext.Current.Request.Url.ToString(), but I get only page for this web user control.

Request.Url.Segments will give you a string array. The last item is the page

You should try the Request.Url.LocalPath property
string fileNameFromLocalPath = Path.GetFileName(Request.Url.LocalPath);

This code helps:
string filename = Path.GetFileName(Request.Url.AbsolutePath);

If you ask for Page.getType.name, you will get the name of the master, the aspx page.
if you want the name of the ascx control you are working on, use
me.GetType.Name.ToString
if your control is in a directory MyDir and the name of your ascx is test.ascx then the result will be
"ASP.MyDir_test_ascx"

You can also use (VB.Net):
Dim pageName as String = Page.GetType().Name
which replaces the .extension with an underscore
So from Default.aspx you would be returned Default_aspx
You can also use:
Dim pageName as String = CType(HttpContext.Current.CurrentHandler, Page).GetType().Name
Which will produce the same results as described above.

Related

What is this redirection called and how can it be setup for an ASP based site?

Does this redirection method have a specific name, and how do I set it up for an ASP based site?
http://www.example.com/?URL=www.redirecteddomain.com
Ok, in that case, it not really the web server, but simply your code that can do this.
so, if you web page was:
http://localhost/MyJumpPage.aspx?URL=www.google.com
So, in your code, all you have to do is grab that 1st parameter, and then run code to jump/navigate to that page.
EG:
string strURL = "";
strURL = Request.QueryString("URL");
Response.Redirect("http://" + strURL);
So, the code behind a button, or even on page load can simply pull the query value from the url string, and then jump to that URL.

Link to a different page in asp.net

I have the following code which should go to a particular company page from the request page. In the website folder Company page is under a folder called Companies and the request page is under Requests folder.
Dim strUrl As String = "/Companies/Details.aspx?Company_ID=" & .Company_id
litlCompany.Text = "" & .Company.Name & ""
The Url should be built as,
http://localhost/Companies/Details.aspx?Company_ID=222
But it comes as,
http://localhost/Requests/Companies/Details.aspx?Company_ID=222
Does anyone knows why?
I'm assuming you are currently on http://localhost/Requests/something.aspx? If so, that's because you forgot the tilde root specifier:
~/Companies/Details.aspx
Which should fix your problem.
You can also try
Page.ResolveClientUrl("~/Companies/Details.aspx?Company_ID=" + Company_id)
By the name of your control it appears that you are using a Literal.
Try changing this to a HyperLink, then you won't need to put the HTML (<a href=...) in the text property, you will be able to use hypCompany.NavigateUrl = strUrl and ASP.Net will generate it for you. This is the neatest way to do it anyhow.
Also add in the tilde to go to the root :-)
E.G.
Dim strUrl As String = "~/Companies/Details.aspx?Company_ID=" & .Company_id
hypCompany.NavigateUrl = strUrl
Am assuming that this link is built in the request.aspx page.
I think the a href is being rendered to the current httpcontext. Have you tried creating the string as :
Dim strUrl As String = "~/Companies/Details.aspx?Company_ID=" & .Company_id
I think this will work if the Companies folder is in the root. The tilda should ensure it looks from the root down.

ASP.net link to other page with passing parameter

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" );

how to make html page by link name

I am building a page for produce name "product.aspx"
I have display some products in this page with hyperlink.
Now I want, when I click my product (such as pen) then it automatically makes an html page name pen.html with some information.
When I click clock then it automatically makes an html page name clock.html with some information.
How can I do this?
I am using asp.net C# 3.5.
You can use a Literal to crete your html page
in frontend ypu can add Literal
so in backend code you can create a html page by adding text to the literal
eg.
var productdata = datasource;
ltrProduct.text ="<head><title>"+productdata.title+"</title></head><body></body></html>";
Rather than actually creating a physical file for these products, its best to have a Web Form called something like "Product.aspx" which you pass a querystring to like:
/Product.aspx?product=Pen
This can then be URL Rewritten to something like:
/Product/Pen.aspx
You can then use this query string to return data like:
Dim cmd as new sqlcommand("SELECT Name, Price FROM Products WHERE FileName=#FileName", Conn)
cmd.CommandType = CommandType.StoredProcedure
cmd.parameters.AddWithValue("#FileName", Request.QueryString("product"))

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