Set Hyperlink to be open in new window / new tab server side - asp.net

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>"

Related

double quote in text to create href doesn't work on vb.net

I have a problem to add the href into the Literal control. I spend an hour and didn't work on escape double quot. It shows the text instead of href. Would someone show me how to correct it.
There is my code:
dim id as integer=Cint(txtID.text.tostring())
dim href1 as string
Dim href1 As String = "<a target='_blank' class='body'" & "href='#'"
href = href & " onclick=""openWin('OrderForm/pop.aspx'?cId=" & id & "','',500,270);return false;"">" & Link & "</a>"
Dim ltLink As New Literal
ltLink.Text = href1
pnlLink.Controls.Add(ltLink)
Thanks in advance
Ideally, if you want to create markup at server-side, you want to use server control. It is less error prone than creating them manually.
Dim id As Integer = Convert.ToInt32(txtID.text.tostring())
Dim hyperLink = New HyperLink()
hyperLink.Target = "_blank"
hyperLink.CssClass = "body"
hyperLink.NavigateUrl = "~/OrderForm/pop.aspx?cId=" + id
hyperLink.Text = "Open Window"
hyperLink.Attributes("onclick") = "window.open(this.href, 'targetWindow', 'width=500,height=270');return false;"
pnlLink.Controls.Add(hyperLink)
I don't know what is openWin. I just assume you want to open a new windows. If it is a custom fuction, you can just replace mine with yours.

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

Detecting current page when on root page ASP.NET with VB

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

Server.MapPath in window open in asp.net

i want to open a new pop up window when user click the button.But nw i facing a problem, how can i open a new pop up window based on server.mapPath?
Here is my coding
StringBuilder sb = new StringBuilder();
sb.Append("<script>");
sb.Append("window.open(" + Server.MapPath("~/reportPreview.aspx") + ", '', '');");
sb.Append("</script>");
ClientScript.RegisterStartupScript(this.GetType(),"test", sb.ToString());
But i cant open a new window . Please help :(
window.open expects a URL like "../reportPreview.aspx", but Server.MapPath returns a physical path like "C:\YourApp\reportPreview.aspx". You should call ResolveClientUrl instead. Also, you need to add quotes around the URL:
sb.Append("window.open('" + ResolveClientUrl("~/reportPreview.aspx") + "', '', '');");

Request Function Returns Null in Firefox/Chrome

I have a several form elements that I am dynamically creating with javascript when a user changes a number value in a textbox, by filling the innerHTML of a div like so:
dfinnerhtml = dfinnerhtml + "<div class='field'> ";
dfinnerhtml = dfinnerhtml + "<textarea name='textbox1" + suffix + "' type='text' id='textbox1" + suffix + "' value='' rows='4' cols='20' class='field'></textarea> ";
dfinnerhtml = dfinnerhtml + "</div> ";
Then, in the aspx.vb code behind, when the user clicks the Save button, I run through a series of Requests to try and add the values that are in these form elements to a SQL string:
Dim DFTestValue1 As String
DFTestValue1 = Request("textbox" & c.ToString)
where c is a loop counter to the # of textboxes created (the input value mentioned above that triggers the create)
The problem here is that this code works in Internet Explorer but not in Firefox or Chrome. The Request() value returns null and nothing is saved. I hope I've explained this well enough.
This post has probably got the answer:
http://forums.asp.net/t/1235816.aspx
Try the PreviousPage property:
protected void Page_Load(object sender, EventArgs e)
{
if (PreviousPage != null)
{
TextBox pTextBox = (TextBox)PreviousPage.FindControl("TextBox1");
Response.Write("Previous Page TextBox: " + pTextBox.Text);
}
}
For anyone that is interested to know, the problem was that a stray tag closed a div that preceded the tag. Internet Explorer didn't seem to care and counted items beyond this point as part of the form.

Resources