ASP.Net Hyperlink navigation URL not including server name - c#

This might be a ridiculously easy question, but it has me stumped. I have a web form where I'm trying to create a hyperlink in the code behind to a file server share, e.g. file://myServer/Shared/, but when the page is rendered, the link doesn't include the server name, i.e. file:///Shared/. I don't know why this happens. Any help or insight is appreciated.
UPDATE:
Sure, here is the snippet where the link is being set.
//The link is embedded in a table
HyperLink link = (HyperLink)e.Row.Cells[1].Controls[0];
link.NavigateUrl = #"file://myServer/Shared/";
As a test, I assigned the link to a string value and the link prints the expected url.
string foo = link.NavigateUrl;
//Displays this
"file://myServer/Shared/"
I don't know why this doesn't appear when the link is rendered in the final page.
UPDATE 2:
Ok, so I know I have to set the absolute path in the code-behind, I thought that's what I was doing, but it still won't render correctly.
UPDATE 3:
I followed pjacobs suggestion about setting the test property and it was actually a step in the right direction. I have the following:
link.Text = "link text";
Now the link gets rendered as follows: file:///myServer/Shared. I'm almost there except it gives the extra '/' in front of the server name. I'll keep playing with it, this seems like it should be so simple, I don't understand why ASP.Net renders the URL differently.

You have to set the Text property of the HyperLink... link.Text = "whatever"

Are the resources inside the project? If so:
you need to use ResolveUrl to resolve the "web location" of the resource.
http://msdn.microsoft.com/en-us/library/system.web.ui.control.resolveurl.aspx
if you're using an asp.net control you shouldn't need to use the resolve url, but you need to refer to the location of the file relative to the path of the project.
If not:
Did you give the proper read account to ASP.NET process?
Use a virtual directory?
http://www.dotnetspider.com/tutorials/AspNet-Tutorial-86.aspx

Related

Find and click a link with text - Coded UI

I am trying to create a test that finds a specified link on a web page and clicks it. What I am attempting to do is search for the link by specifying the name/text of the link. Is there a way to create a hyperlink object solely by specifying this?
I can do the following to find the link by specifying the href property like this:
BrowserWindow browser = BrowserWindow.Locate("Window Title");
var hyperlink = new HtmlHyperlink(browser);
hyperlink.SearchProperties.Add(HtmlHyperlink.PropertyNames.Href, "link.com");
Mouse.Click(hyperlink);
But I want to do the same thing by specifying the text/name of the link.
Any help would be appreciated! Thanks
I solved it...
This finds the link based on the text:
hyperlink.SearchProperties.Add(HtmlHyperlink.PropertyNames.InnerText, "text");

Making a file with full path suitable to act as an href in the HTML

I have a piece of code that is like
string.Format("<a href='{0}' class='hidden'>{0}</a>", Path.Combine(filePathPrefix , string.Join("-", new string[] { fOrgName, fCatName, f.filename })))
and meant to return an a tag that a user can click on to get a file. The only problem is that string I'm building for the href does not work as a link. It is something like
c:\users\me\documents\visual studio 2013\Projects\myproj\myproj\Assets\someorg-somecat-somepic.png
which doesn't work as a clickable link, brings to an about:blank page if you click on it. However, if I paste the link into my browser and make the request then it changes to
file:///C:/users/me/documents/visual%20studio%202013/Projects/mypoj/myproj/Assets/someorg-somecat-somepic.png
and brings up the asset properly. That means I need some way getting the link in that form while its on the page. Is this possible? If so, what C# class(es) and method(s) do I need?
Try changing to this:
string.Format("{0}", ...);

How do I create hyperlinks from resource files?

I have some text which is loaded from a resource file. Ordinarily, to get dynamic content I would use:
string.Format(GetLocalResourceObject("SomeText"), PhoneNumberAsString)
I want to do the same with a link, only the link needs to be application relative as I have URLs like mysite.com/page.aspx and mysite.com/fr/page.aspx.
I normally use an <asp:HyperLink /> tag to create the links as I can then just put a squiggle at the start NavigateUrl="~/page.aspx". However, I don't know of a way to get a dynamic HyperLink to appear as a string without adding it as a control to something.
Simply writing ToString() outputs System.Web.UI.WebControls.HyperLink..
How do I get a link out of a resource file and make it into a hyperlink using ASP.NET Webforms?
UPDATE
With some help from the answers I now have the following code on my page:
<p><%= string.Format(GetGlobalResourceObject("Resource", "MoreThan1000Users").ToString(), ResolveUrl("~/contact-us.aspx")) %></p>
and in my resource file I have:
If you would like more than 1000 users please call our sales team.
Does this seem like good practice or is there another way to achieve what I'm doing? I don't know if I should be happy or not that there is HTML inside the resource file.
Since you haven't posted code, I'm guessing somewhere you have a HyperLink WebControl object that you're hitting ToString() on. If that's the case, you can access the URL associated with it using its myHyperLinkControl.NavigateUrl property.
If you're storing the link in your resource with a squiggle/tilde (which is good) then you can replace the squiggle with your application location. If you have a control/page, then you can easily call its ResolveURL method (which takes the tilde and automatically replaces it) There's some existing solutions if you don't have a control/page reference with your context, then there's some discussion to do that here: ResolveUrl without an ASP.NET Page
I guess this is what you want:
Server.MapPath("~/page.aspx")
That will work inside your aspx and your code-behind.

RDLC Report hyperlink are not working in browser

I have a problem with hyperlinks in my rdlc report. I configured a tablix's textbox by applying steps in this tutorial.(It seems very easy though)
It seems to hyperlinks are not working in the reportviewer control (I mean when I look to report in browser) but when I export that report to PDF all these links works as shine.
I tried setting enableHyperlinks option to true.
I tried in different browsers.
Any comment on where could I be wrong is appriciated.
I'm not sure if this relates to your specific problem or not, but I have recently discovered that if you set the action to URL, it must be a full URL, and not a relative one.
For instance, if you are setting the URL as
="MyPage.aspx?myprop=" & Fields!SomeProp.Value
The result will be that no hyperlink is actually added to the field.
However if you had something like
="http://localhost/MyPage.aspx?myprop=" & Fields!SomeProp.Value
it should work just fine, because that is a full URL
This, of course, brings up the problem of not knowing where the application is. For instance, if you set this to localhost and then put this on the production server it would probably fail for most people.
In order to handle that scenario, you will need to add a parameter to pass in the base URL from the web page and then add the rest.
= String.Format( _
"{0}/MyPage.aspx?myprop={1}", _
Parameters!BaseUrl.Value, _
Fields!SomeProp.Value _
)
This works fine for me:
I just add a new parameter in my rdlc as #BaseUrl and Use the same index in TextBox Properties > Action > Go To Url and
String.Format("{0}Pages/PageName.aspx?item{1}",Parameters!BaseUrl.Value,Fields!YourField.Value)
and set Perameter as http://localhost:1268/ or ur url...

navigation menu doesn't redirect to the given value

the problem i encounted concerns redirecting in the navigation menu. I'd like to dynamicly create a navmenu. Depending on what role the user has we get to see the required navigation menu items.
At the moment i use:
if (found)
{
if (admin == true)
{
NavigationMenu.Items.Add(new MenuItem("Agenda", "/AdminPages/Agenda.aspx"));
NavigationMenu.Items.Add(new MenuItem("Add Product", "/AdminPages/ProductToevoegen.aspx"));
}
else if (user == true)
{}
This code I have placed in my Site.master.cs, but I also have a control in my login.aspx.cs code which does a Response.Redirect("~/AdminPages/Agenda.aspx"); to a certain page depending on admin or user once logged in. Now the problem I have is that when I log in, a part works, so it controls the role and adds the required navigation menu items. But when I click for example on the Add Product link it doesn't redirect me to the page. It keeps redirecting me to :
http://localhost:52853/AdminPages/Agenda.aspx
In the url bar it actually shows the url followed with a # when clicked and than redirect to the Agenda page.
Any ideas on how to fix this problem? I tried finding a way to put the navigation links in the login.aspx.cs code aswell but couldn't find the correct way to refer to the NavigationMenu, don't know if that could be off any help. Thank you in advance.
Look at the parameters for creating a new MenuItem.
When only passing 2 parameters, you are filling the text and value parameters. What you want to pass is a navigateUrl parameter. This is only available when passing an imageURL parameter (which can be empty).
Something like the following should fix your problem.
NavigationMenu.Items.Add(new MenuItem("Agenda", "", "", "/AdminPages/Agenda.aspx"));
Hope this helps.
I would suggest using a standard siteMap object and RoleProvider. And read a little bit about security trimming (you can specify on your sitemap nodes which roles can access which sites).
Hm might not be the correct way but i fixed it making another menu in the site.master.aspx file called MenuAdmin for example. Than in the code behind i've set the
NavigationMenu.visible = false;
and
MenuAdmin.visible = true;
Seems to work fine, does what it needs to do for now ;)

Categories