Detecting current page when on root page ASP.NET with VB - asp.net

my current code is below. If you visit the root of my webpage http://evanparsons.net/ you will notice that the current page is not highlighted, however when you visit a page, it works. For testing purposes, I had it return the "myPage" value to see why it isn't working... it still returns index.aspx.
Dim pageName As String = System.IO.Path.GetFileName(System.Web.HttpContext.Current.Request.Url.AbsolutePath)
While DBReader.Read()
_link = _link + "<li><a href='" + (DBReader("source")) + "'"
If ((pageName) = (DBReader("source"))) Then
_link = _link + "class='current' "
End If
_link = _link + pageName
_link = _link + ">-" + (DBReader("name")) + "- </a></li>"
End While
Basically, my navigation comes from a master page that scans my database, and as it cycles through it, I want to add a css class called current.

Have you tried converting them into lower case?
If (pageName.ToLower() = (DBReader("source")).ToLower()) Then

Related

VB.NET 2.0: Where does a URL in code come from?

I have to debug an old VB.NET 2.0 code of someone who has left the company. We have a production system (let us call it http://prod) and a test system (http://test). Both are nearly similiar including a documents repository. When looking at docs in production, all the hyperlinks showing up at the bottom are okay (meaning they say something like http://prod/download.ashx?id={GUID}).
However in test it is the same (http://prod/download.ashx?id={GUID}), even it should be http://test/download.ashx?id={GUID} instead.
After hours of debugging I have found the relevant line of code:
html += "<td><a href='" + HttpContext.Current.Request.Url.AbsoluteUri.Replace(HttpContext.Current.Request.Url.PathAndQuery, "/") + "int/download.ashx?id=" + row.Item(0).ToString() + "' target='_blank' class='" + row.Item(3).ToString() + "'>" + row.Item(1).ToString() + "</a>" + privat + "</td><td>" + row.Item(2).ToString() + "</td>"
Looking at html this shows i.e.
"<table class='table_dataTable'><thead><tr><td>Name</td><td>Jahr</td></tr></thead><tbody><tr><td><a href='http://prod/int/download.ashx?id=4d280886-db88-4b25-98d8-cf95a685d4a4' target='_blank' class='doc'>Document for managers</a></td><td>2014</td>"
So I wonder, where does this come from incorrectly? I may have found the relevant part of coding, but I am not sure, what to do now, respectively if I am right on this?:
Public Class download : Implements IHttpHandler, IReadOnlySessionState
Dim debug As String = ""
Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
Dim fehler As String = ""
Try
' Get the file name from the query string
Dim queryFile As String = context.Request.QueryString("id")
debug += "id=" + queryFile + "<br>"
Any help is appreciated as VB.NET is not my main focus.
You have probably checked this but sometimes the obvious gets overlooked.
Verify the URL in your browser window. Make sure it has not changed to http://prod... while you were navegating.
Verify that your web application is not using frames. The page in question could be loaded in a frame using the prod URL. If this is the case your web.config might have a setting to say where this frame is loaded from or it might simply be hardcoded.
Check for URL Rewrite rules in IIS or your web.config

Set Hyperlink to be open in new window / new tab server side

I wonder if I can get some help, I'm creating hyperlink on the server side for each row in the my grid.
The issue is how can I set it up to open in a new window or new tab when clicking on the hyperlink.
Please help me to modify my code so it can be open in new window/new tab.
Protected Function GenerateReportLink(ByVal inputVal)
Dim output As String = ""
Try
Dim svcs As New SystemServices
' Check for null values
If Not inputVal Is Nothing And Not String.IsNullOrEmpty(inputVal) Then
Dim URL As String = Nothing
URL = (String.Format("https://www.test.com/cgis/{0}/Reports/ShortReport.asp?/SessionID={1}&IncidentID={2}", m_User.CompanyCode, m_SessionID, m_IncidentCaseID.ToString()))
output = "<a href='" + URL + "'>Report</a>"
End If
Catch
End Try
Return output
End Function
Change:
output = "<a href='" + URL + "'>Report</a>"
to:
output = "<a href='" + URL + "' target='_blank'>Report</a>"
Source: W3 Schools
It looks like you might want to set the 'target' attribute of the hyperlink, although going from what you've written so far, I'm not 100% sure where this hyperlink is going to eventually be clicked, most modern browsers, unless the user has explicitly set the behavior as otherwise would open in a new tab I think.
Either way, this might help you http://www.w3schools.com/html/html_links.asp
just add the target='_blank' property to anchor <a> tag under your output varriable.
"<a href='" + URL + "' target='_blank'>Report</a>"

create download link between two asp.net applications

Lets say I have two applications as:
www.test.com/a
www.test.com/b
In site b, I have a page set to download a pdf file. The page is called OpenBook.aspx, when I pass the bookId in the query string, it downloads the relevant file.
Here's the code to download this file from site b, by clicking on a link:
lbl.Text = "Report book {0}
is completed and ready for view. ";
Now this works within site B, but it obviously don't work within A. Because the path is set as www.test.com/a/Processing/OpenReportBook.aspx.....
I tried using the following:
string downloadLocation = HttpContext.Current.Request.Url.Host.ToString() + "/DataCenter/Processing/OpenReportBook.aspx?ReportID=";
Label2.Text = "Report book <a href='" + downloadLocation +
+ 948 +
"'>{0}</a> is completed and ready for view. ";
But still, the path is set relative to site A, when you click it from site A.
Can someone please explain how to resolve this?
Thanks
don't use HttpContext.Current.Request.Url.Host.ToString() .
Instead give the second url in web.config as below
<appSettings>
<add key="downloadurl" value="http://www.test.com/b" />
</appsetings>
And in your .cs file replace the folowing code
string downloadLocation = HttpContext.Current.Request.Url.Host.ToString() + "/DataCenter/Processing/OpenReportBook.aspx?ReportID=";
Label2.Text = "Report book <a href='" + downloadLocation +
+ 948 +
"'>{0}</a> is completed and ready for view. ";
with
string downloadLocation = ConfigurationManager.AppSettings[downloadurl"].ToString() + "/DataCenter/Processing/OpenReportBook.aspx?ReportID=";
Label2.Text = "Report book <a href='" + downloadLocation +
+ 948 +
"'>{0}</a> is completed and ready for view. ";
i hope this may solve your problem.

Getting text between anchor tags

I have a label that I put a hyperlink into with the function:
Private Sub showLink(title As String, cost As String, asin As String)
Label1.Text = (String.Format("<a href=\WebSite3\LinkDisplay.aspx?Title=" + System.Web.HttpUtility.UrlEncode(title) + "&Price=" + System.Web.HttpUtility.UrlEncode(cost) + "&ASIN=" & System.Web.HttpUtility.UrlEncode(asin) + " \>" + title + "/" + cost + "/" + asin + "</a>"))
End Sub
I want to gain access to the the string title + "/" + cost + "/" + asin in order to split it with String.Split("/").
I'm getting an error when I just try Dim fields As String() = Label1.Text.Split("/").
What function should I use to get just the text that is between the <a> tags?
First, instead of using a label, use a HyperLink object. You can set its URL like this:
Private Sub showLink(ByVal title As String, ByVal cost As String, ByVal asin As String)
Dim link As String = System.Web.HttpUtility.UrlEncode(String.Format("WebSite3/LinkDisplay.aspx?Title={0}&Price={1}&ASIN={2}", title, cost, asin))
HyperLink1.NavigateUrl = link
HyperLink1.Text = String.Format("{0}/{1}/{2}", title, cost, asin)
End Sub
Then, you can read out the querystring value of the url like so:
Private Sub GetLinkAttributes()
Dim url As New Uri("http://" & HttpUtility.UrlDecode(HyperLink1.NavigateUrl.ToLower))
Dim fields As NameValueCollection = HttpUtility.ParseQueryString(url.Query)
Debug.Print(String.Format("{0} / {1} / {2}", fields("title"), fields("price"), fields("asin")))
End Sub
You were using backwards slashes in your sample code, that may have thrown the error you were getting (HttpRequestValidationException).
First of all, you should be using a Literal control to do this, and not a label. Or, better yet, use some type of link control.
You haven't added a whole lot of detail since the first time you asked this, but as long as the control is a server control, you can get the text using Label1.Text.
Once you have the text, you still need to parse out the part you want. There's no real trick to this. You need to remove the parts of the text that you aren't interested in.
This last task would be far simpler if you used some type of link control because you could just look at the link text value.

Posting text with link to facebook wall using C# sdk

I am using facebook api for posting to the facebook wall post.THe post is getting displayed in the wall but i am not able to show the link which redirects to the site when it is clicked.
I need to show both the post as well as the link which points the posted post.
Following is my code
string viewDetailsLink = context.Url.GetLeftPart(UriPartial.Authority).ToString();
viewDetailsLink = viewDetailsLink + "/" + "Blog" + "/Index/" + "0/" + blogPost.Id;
viewDetailsLink = "<a href='" + viewDetailsLink + "'/>" + "click here to open" +"</a>";
But the whole thing is displayed as string instead of click here to open.
How do i resolve this?
You're seeing text instead of a link because you're closing our anchor tag prematurely. Try this instead:
viewDetailsLink = "<a href='" + viewDetailsLink + "'>click here to open</a>";
Here is the issue with your code:
viewDetailsLink = "<a href='" + viewDetailsLink + "'/>" + "click here to open" +"</a>";
Shouldn't close tag here ^ ^ No need to concat ^
Your code, fixed and prettied up a bit:
string viewDetailsLink = context.Url.GetLeftPart(UriPartial.Authority).ToString();
viewDetailsLink = String.Format("{0}/Blog/Index/0/{1}", viewDetailsLink, blogPost.Id);
viewDetailsLink = String.Format("<a href='{0}'>click here to open</a>", viewDetailsLink);

Resources