Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB.NET 1.0 > VB.NET 2002/2003 Basics
|
VB.NET 2002/2003 Basics For coders who are new to Visual Basic, working in .NET versions 2002 or 2003 (1.0 and 1.1).
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB.NET 2002/2003 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 24th, 2005, 02:17 PM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default How to obtain a Web Form’s URL

In page One.aspx I have
Code:
    . . .
    Server.Transfer("Two.aspx", True)
    . . .
    In web form Two’s page load I call a user-defined function (MakeLinks()) to generate a dynamically-sized set of links to put across the top of the page for Two.aspx. (There might be 3 links, there might be 15: Go To Page: [u]01</u> [u]02</u> 03 [u]04</u> [u]05</u> The current page [03 in this case] is represented, but is not a link.)

I use some data from Session, and some from the Request to create these links. I need these links to have Two.aspx as the target of the href.
Code:
    Private Sub Page_Load(ByVal sender As System.Object, _
                          ByVal e As System.EventArgs _
                         ) Handles MyBase.Load

        LinksDiv.InnerHtml = MakeLinks(Session, Request)

    End Sub
In VB6 I would have used URLFor(Two) to obtain the reference to Two.aspx.
In .NET I have not been able to find any way to accomplish this.

The .URL property of the Request is One.aspx, since that is who received the actual request...

I don’t want to hard-code it, because I want the app to be capable of being moved without it becoming broken...

How can this be done?
 
Old June 25th, 2005, 03:28 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

While it would be a very nice feature to have in .NET it seems to make sense that there is no method that will generate a URL for you to a page. Consider that the pages are just ASPX files that can be named anything and can live anywhere in your application structure. They can be derived from any page-derived class in the accessible namespaces (the frame supplied Page class, or any class that you create in your application assemblies that was derived from the system's page class). Therefore, there is no reliable way to say "give me the URL for X".

Something I have started doing to solve two problems at once is to create a shared/static "GetUrl" method on my pages. This method provides the root-relative URL to the page that owns the code-behind class as well as specifying required parameters so you don't have to guess at them or encounter problems later on.

Here's an example:

Public Class ItemAttributes
   Inherits System.Web.UI.Page
   ...
   Public Shared Function GetUrl(itemID As Integer, page As Page) As String
      Return page.ResolveUrl("~/ItemAttributes.aspx?itemID=" & itemID)
   End Function
End Class

This way, if I move this page to another physical directory location I can just change the page name that is resolved in the GetUrl method of the page. Also, if in the future I decide that I need to require another querystring argument on the page's URL I can add it as an argument of the method and force a design/compile-time break that shows me ALL the locations that call it instead of a runtime break that has to be discovered in testing or by a customer (which we don't want).

-Peter
 
Old June 27th, 2005, 01:22 PM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

What is the meaning of the tilde in the relative path, and what are you using itemID for?

Because this function is Shared, any process that has ItemAttributes.aspx in scope can get results from this function?
What would be the syntax to cause any given page’s GetURL() to respond (as over against getting a different page’s GetURL() to respond)? Would it just be s = ItemAtributes.GetURL("...")?
 
Old June 27th, 2005, 02:22 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

The ~ is used as a "home" location placeholder (ala *NIX style). If you use the ~ in a URL on a server control, .NET will replace it with the application root (i.e. "/myWebApplication") so you don't have to hard code server-root-relative URLs for resources. This is typically how you would set up a url for an image used in a user control. You never know where in the file hierarchy of you application the user control will be used (on "index.aspx" or on "folder/folder/folder/another.aspx") so you can't use a pure relative image URL ("images/button.gif") because it will likely break.

The method in the code-behind class is shared so that it's visible by any page in the application despite the class that it lives in not being instantiated. This is so any other page can ask for the URL to the desired page.

For example, we have a page called "itemList.aspx" which shows a list of many items. For each item, we have a hyperlink that will get you to the ItemAttributes page, therefore we call ItemAttributes' GetUrl() method so we can dynamically get the URL to it. In addition, we are told what parameters are required (itemID) by means of the method signature.

-Peter
 
Old June 27th, 2005, 06:53 PM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

When I have:
Code:
Public Class wfv_view
    Inherits System.Web.UI.Page

 __________________________________
[ Web Form Designer Generated Code ]
 Â¯Â¯Â¯Â¯Â¯Â¯Â¯Â¯Â¯Â¯Â¯Â¯Â¯Â¯Â¯Â¯Â¯Â¯Â¯Â¯Â¯Â¯Â¯Â¯Â¯Â¯Â¯Â¯Â¯Â¯Â¯

    Private Sub Page_Load( . . . ) Handles MyBase.Load

        . . .

    End Sub

    Public Shared Function OwnURL() As String
        Return Page.ResolveURL("~/wfv_view.aspx")
    End Function


End Class
the statement Page.ResolveURL(" . . . ") has a squiggle under Page, and hovering shows “Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class.”

What am I missing?
Page seems to only have .Equals() and .ReferenceEquals() as available methods, and no properties at all.

As near as I can figure from help, the Page object is not a Shared object, and as such cannot be called from a Shared Method.

Finally, help only lists ResolveURL as part of ASP.NET Mobile controls or under:
Code:
Title                                   | Location
Control.ResolveUrl Method (System.Web.UI)   |   .NET framework Class Library
 
Old June 27th, 2005, 09:59 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Quote:
quote:Originally posted by planoie
 Public Class ItemAttributes
   Inherits System.Web.UI.Page
   ...
   Public Shared Function GetUrl(itemID As Integer, page As Page) As String
      Return page.ResolveUrl("~/ItemAttributes.aspx?itemID=" & itemID)
   End Function
End Class
I should have described it a bit more. You need to pass a page instance to the method to get a page context.

The ResolveUrl() method does exist. It is possible that it only shows up when you have "Hide Advanced Members" unchecked in Visual Studio preferences.

-Peter





Similar Threads
Thread Thread Starter Forum Replies Last Post
Using web request and response to post data to URl cgmenon .NET Framework 1.x 0 July 13th, 2006 10:39 AM
url of actual web page VB6 Perseus Beginning VB 6 0 July 30th, 2005 04:38 AM
Print an web page without URL address rekha_jsr Servlets 1 September 16th, 2004 05:15 AM
Hashing of URL while posting web pages debsoft General .NET 1 April 22nd, 2004 09:20 AM
Web service URL yaronkl VB How-To 0 January 26th, 2004 01:29 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.