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