NavigateURL/DataNavigateURLFormatString and Session Variable

T

tshad

I gave up on the Page.ResolveURL and just got the Current Path from the
Request.ServerVariables("Path_Info") and put it in a session variable. I
thought I could use a session variable to pass the path in since the other
won't work. But I am getting the same type of error.

<asp:HyperLinkColumn ItemStyle-Width="190" DataTextField="JobTitle"
DataTextFormatString="{0}" DataNavigateUrlField="PositionID"
DataNavigateUrlFormatString='<%= session("PagePath")
%>displayPositionNS.aspx?PositionID={0}'
headertext="Job Title"
ItemStyle-VerticalAlign="Top"
sortexpression="JobTitle"/>

session("PagePath") = /JobSeeker/

The link in the DataGrid:

http://www.stw.com/applicant/<%=%20session("PagePath")%20%>displayPositionNS.aspx?PositionID=442

Not only won't it use the session variable - it still uses the Controls path
(/applicant/) and not the Path I tell it to use.

I also tried it with the <%# %> with the same result.

Is there anyway to use a session variable with the Hyperlink???

Thanks,

Tom
 
G

Guest

Hi there,

First, you cannot use <%=%> for server controls, the only allowed are data
bound <%# %> (in conjunction with a DataBind() method call) and resource <%$
%> expressions. Second, use ~ (tilde) in the URL as it is resolved
automatically by all the controls (by calling ResolveUrl method):

<asp:HyperLinkColumn
ItemStyle-Width="190" DataTextField="JobTitle"
DataTextFormatString="{0}"
DataNavigateUrlField="PositionID"
DataNavigateUrlFormatString="~/displayPositionNS.aspx?PositionID={0}"
HeaderText="Job Title"
ItemStyle-VerticalAlign="Top"
SortExpression="JobTitle"/>


hope this helps
 
T

tshad

Milosz Skalecki said:
Hi there,

First, you cannot use <%=%> for server controls, the only allowed are data
bound <%# %> (in conjunction with a DataBind() method call) and resource
<%$
%> expressions. Second, use ~ (tilde) in the URL as it is resolved
automatically by all the controls (by calling ResolveUrl method):

Actually, that is not the case. Apparently, it doesn't call the ResolveURL
(which sends back the Current Page URL). No matter how you do it, it comes
back with the Controls' URL. Which means if you do it the normal way, you
absolutely cannot call it from different folders. If you call it this way,
you will get the path of the control.

For example:

If you have

http://www.stw.com/jobseeker/displayCompanyJobs.aspx and
/applicant/displayCompanyJobs.ascx (my control loaded
into displayCompanyJobs.aspx)

Where displayCompanyJobs trys to load displayCompanyOverview.aspx - It will
use the path of the Control (www.stw.com/applicant/ and not
www.stw.com/jobSeeker as you would expect and is done by every other
Redirect object or tag)

The ~ really doesn't do anything, as far as I can tell. Here is what will
happen for different ways of calling displayCompanyOverview.

<asp:Hyperlink Text="with ~/ " NavigateUrl="~/displayCompanyOverview.aspx"
runat="server"/><br>

http://www.stw.com/displayCompanyOverview.aspx (where is the applicant
or jobseeker folder????)

<asp:Hyperlink Text="with ~/ and jobseeker "
NavigateUrl="~/jobseeker/displayCompanyOverview.aspx" runat="server"/><br>

http://www.stw.com/JobSeeker/displayCompanyOverview.aspx (Current Page
Path but worthless as it is called from the control and not the page)

<asp:Hyperlink Text="with / " NavigateUrl="/displayCompanyOverview.aspx"
runat="server"/><br>

http://www.stw.com/displayCompanyOverview.aspx

<asp:Hyperlink Text="with no tilde or / "
NavigateUrl="displayCompanyOverview.aspx" runat="server"/><br>

http://www.stw.com/applicant/displayCompanyOverview.aspx (Controls
Path)

If you call ResolveURL as so:
test = Page.ResolveUrl("displayCompanyOverview.aspx")
trace.warn("ResolveURL = " & test)

You will get in the trace:

ResolveURL = /JobSeeker/displayCompanyOverview.aspx

As you see ResolveURL gives you the Current Page Path.

You cannot use Page.ResolveURL in your HyperLink, however. It doesn't
resolve it. And you cannot use ResolveClientURL in .Net 1.1. You get the
error - " not accessible in this context because it is 'Private'"

I also tried to put the path in a session variable and found I couldn't get
the inline call to get the session variable.

These are things you can't do. Here is what you can do and it works well.

I have and found a very good solution - similar to your approach. I am
using the OnPreRender event - which gives me the line that needs to be
handled and I don't have to check to see if the object is a correct type of
object.
Works great.

Here is what I did - for anyone in the same situation.

The ServerVariables("PATH_INFO") in my situation gives me:
/JobSeeker/displayCompanyJobs.aspx So I just take out the
displayCompanyJobs.aspx and I have the Current pages path. As the session
variables - as far as I could tell, you cannot use this
ServerVariables("PATH_INFO") in the Hyperlink object.

The event I created was:

Sub FixHyperLink(s as Object, e as EventArgs)
s.NavigateURL =
request.ServerVariables("PATH_INFO").Substring(0,request.ServerVariables("PATH_INFO").LastIndexOf("/")+1)
& s.NavigateURL
end Sub

Then for all my Hyperlinks I add:

OnPreRender="FixHyperLink"

Now my Hyperlink looks like:

<asp:hyperlink text='<%# Container.DataItem("JobTitle")%>' NavigateUrl='<%#
"displayPositionNew.aspx?PositionID=" & Container.DataItem("PositionID") %>'
OnPreRender="FixHyperLink" runat="server" />

I also changed my HyperLinkColumns to:

<asp:TemplateColumn sortexpression="JobTitle" ItemStyle-Width="250px"
HeaderStyle-Width="250px"
headertext="Job Title" ItemStyle-VerticalAlign="Top"
runat="server">
<ItemTemplate>
<asp:HyperLink ID="JobTitle"
NavigateURL='<%# "displayPositionNew.aspx?PositionID=" &
Container.DataItem("PositionID") %>'
Text='<%# Container.DataItem("JobTitleDesc")%>'
OnPreRender="FixHyperLink"
runat="server"/>
</ItemTemplate>
</asp:TemplateColumn>

There is no event on HyperLinkColumns and the problem with ItemDataBound and
RowDataBound is that you need to check all the controls in the rows to find
any HyperLinkColumn type fields then make the change.

Thanks,

Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top