Answered by:
relative url in hyperlinkfield

Question
-
User996453919 posted
Hi
I am using a grid view inside a web user control. The gridview contains a hyperlinkfield.
I use this control in 3 separate directories within the application.Directory structure
appRoot
Controls -> this folder contains web user control
folder1
UserView.aspx ->page uses control
Process.aspx
folder2
Default.aspx ->page uses control
Process.aspx
folder3
New.aspx -> page uses control
Process.aspx
How do I get relative url(s) for the hyperlink fields?
If I try to built
Case 1:DataNavigateUrlFormatString="~/Process.aspx?commandid={0}", it takes the approot for relative url, So the url is http://approot/Process.aspx?commandid=5GO53DS3BB.
Case 2:DataNavigateUrlFormatString="Process.aspx?commandid={0}", it takes control folder for relative url, So the url is http://approot/Controls/Process.aspx?commandid=5GO53DS3BBIn any case, the url I require is
http://approot/folder1/Process.aspx?commandid=5GO53DS3BB, or http://approot/folder2/Process.aspx?commandid=5GO53DS3BB or http://approot/folder3/Process.aspx?commandid=5GO53DS3BB when invoked from the respective folder.
Thursday, June 26, 2008 9:02 AM
Answers
-
User1564875471 posted
Server.MapPath will never works here, it will returns the physical path of the mapped file !
Please remove the HyperLinkField , and instead use a HyperLink with a TemplateField ( since you want to resolve the Process.aspx with respect to the page location , use the page.ResolveUrl instead of the User Control ResolveUrl )
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink NavigateUrl='<%# Eval("Id", Page.ResolveUrl("Process.aspx") & "?CmdName={0}")%>'
Text="Details" />
</ItemTemplate>
</asp:TemplateField>- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Saturday, June 28, 2008 5:12 AM
All replies
-
User-999963490 posted
Could you state your issue another way? I don't understand your problem
Thursday, June 26, 2008 9:20 AM -
User537870505 posted
You can use Request.Path, which returns the virtual path of the currently executing aspx (including page name). But you have to use them from code-behind as in: GridView1.DataNavigateUrlFormatString = Request.Path + "?commandID={0}";Thursday, June 26, 2008 9:30 AM -
User-2005691517 posted
Try using ResolveUrl
http://msdn.microsoft.com/en-us/library/system.web.ui.control.resolveurl.aspx
Thursday, June 26, 2008 9:33 AM -
User996453919 posted
Ok drocco, I will explain again.
I have a web user control (let us call it ProcessControl.ascx). ProcessControl.ascx contains a gridview. The grid view has a databound hyperlink field which call Process.aspx.
The ProcessControl.ascx is used in 3 aspx pages(UserView.aspx,Default.aspx,New.aspx) within different folder.Let us go to the directory stucture
appRoot
Controls -> this folder contains web user control
ProcessControl.ascx
folder1
UserView.aspx ->page uses control
Process.aspx
folder2
Default.aspx ->page uses control
Process.aspx
folder3
New.aspx -> page uses control
Process.aspx
When I use ProcessControl.ascx in UserView.aspx in "folder1", the hyperlink field should point to Process.aspx inside folder1.
When I use ProcessControl.ascx in Default.aspx in "folder2", the hyperlink field should point to Process.aspx inside folder2.
When I use ProcessControl.ascx in New.aspx in "folder3", the hyperlink field should point to Process.aspx inside folder3.In a single sentence the the hyperlink should take relative url of the aspx page which uses the control.
Hope I am clear.
Thursday, June 26, 2008 9:41 AM -
User-999963490 posted
I had a feeling that is what you meant but wasn't sure
For something like this I would use Server.MapPath
For example a link to your page you could = Server.MapPath("/folder1/Process.aspx") that will take you from the root, to "folder1" to that web page inside folder1
Do the same for the others
Thursday, June 26, 2008 9:50 AM -
User996453919 posted
I am aware of server.mappath, but this grid view is in a control, which is reused across pages as I mentioned in my post. So I cant hard code folder names.
Thursday, June 26, 2008 9:57 AM -
User996453919 posted
ResolveUrl looks like it can get the work done. I havnt checked is out in my code yet.
But I am looking for something which can be used declaratively. As these urls are in grid view. If not I have to change for each row data.
Thursday, June 26, 2008 10:00 AM -
User996453919 posted
Anyway I can use ResolveUrl with asp:Hyperlinkflied tag?
I tried this:
<asp:HyperLinkField DataNavigateUrlFields="Id" NavigateUrl="<%ResolveClientUrl("~/Process.aspx")%>" HeaderText="Process" DataNavigateUrlFormatString="?commandid={0}" DataTextField="CmdName" />But gives error
Thursday, June 26, 2008 10:37 AM -
User996453919 posted
Still havnt got this working.
Anyone has any ideas???
Saturday, June 28, 2008 1:45 AM -
User-1179452826 posted
VB.Net
<asp:HyperLinkField DataNavigateUrlFields="Id"
NavigateUrl="<%# Eval("Process", ResolveClientUrl("~/Process.aspx") & "?CmdName={0}")%>"
HeaderText="Process" DataTextField="CmdName" />
C#:
<asp:HyperLinkField DataNavigateUrlFields="Id"
NavigateUrl="<%# Eval("Process", ResolveClientUrl("~/Process.aspx") + "?CmdName={0}")%>"
HeaderText="Process" DataTextField="CmdName" />
Saturday, June 28, 2008 3:39 AM -
User-1179452826 posted
Sorry..the last post was made for templatefield with asp:Hyperlink.
You can use this:
<asp:HyperLinkField DataNavigateUrlFields="FirstName"
DataNavigateUrlFormatString="?id={0}" DataTextField="FirstName"
NavigateUrl="<%# ResolveClientUrl("~/Default.aspx") %>" />Just replace firstname with your column name and id with your query parameter name.
The reasaon NavigateUrl="<%# .. works and not "<%=.." is that they are parsed at different stages. <%= is not allowed in a server controls tag. <%# is if the control is for gridview or any other databound control.
Saturday, June 28, 2008 3:54 AM -
User996453919 posted
Hi HeartattacK
ResolveClientUrl does not work. I tried to get it to work the other day but no success.
Have you used this in before successfully?
Saturday, June 28, 2008 4:03 AM -
User-1179452826 posted
Why don't you just do this:
<asp:HyperLinkField DataNavigateUrlFields="FirstName"
DataNavigateUrlFormatString="Process.aspx?cmdid={0}" DataTextField="FirstName"
/>This will link to the Process.aspx int he same folder as the UserView.aspx page. Change firstname to your db column name.
Saturday, June 28, 2008 4:41 AM -
User1564875471 posted
Server.MapPath will never works here, it will returns the physical path of the mapped file !
Please remove the HyperLinkField , and instead use a HyperLink with a TemplateField ( since you want to resolve the Process.aspx with respect to the page location , use the page.ResolveUrl instead of the User Control ResolveUrl )
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink NavigateUrl='<%# Eval("Id", Page.ResolveUrl("Process.aspx") & "?CmdName={0}")%>'
Text="Details" />
</ItemTemplate>
</asp:TemplateField>- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Saturday, June 28, 2008 5:12 AM -
User996453919 posted
Have you read my first post completely?
DataNavigateUrlFormatString="Process.aspx... is already tried , the url result is also give in the first post.
Saturday, June 28, 2008 5:20 AM