HtmlAnchor control not opening in new window - c#

I have the following code to dynamically create an HtmlAnchor and assign its HRef property. When I right click the link and do "save target as" it is downloading the pdf, but when clicking the link is not opening in a new tab.
HtmlAnchor htmlanc = new HtmlAnchor();
htmlanc.HRef = "file:\\arts\Shared\Let";
htmlanc.Title = "Letter Link";
htmlanc.InnerText = "file:\\arts\Shared\Let";
htmlanc.Target = "_blank";
pnlLet.Controls.Add(htmlanc);
when i click on the link it is generation an error which in the below image.
And the path is in a network folder which is shared. Is the error generating because my application cannot access the path. I checked with the networking guys but they say that the application have full access to the network shared folder, but i doubt it.

I wanted this to be a comment but it seems you are struggling to understand our help so I wanted to expand it here.
I have copied your code sample and put on Page_Load of a blank WebForms solution.
protected void Page_Load(object sender, EventArgs e)
{
var htmlanc = new HtmlAnchor();
htmlanc.HRef = "http://stackoverflow.com/questions/29281667/htmlanchor-control-not-opening-in-new-window";
htmlanc.Title = "Open Question";
htmlanc.InnerText = "Open Question";
htmlanc.Target = "_blank";
Controls.Add(htmlanc);
}
When running it you can right-click on the link:
Then you can see all of the attributes and see the target="_blank":
When I click this link it correctly shows in a new tab. If you follow the above steps can you check your link has the correct target set and expand on what happens when you click it? Depending on your browser it may be set to download based on mime-type rather than opening a new tab etc.

Related

Linking words to windows Explorer c#

Question: How can I make a list of link buttons so that when a user clicks on a link it opens it up in windows explorer.
So I have a rich text box that contains a list of all folder names and I have a list of all the folder paths. I want to be able to click on the folder name and have it linked using the path to the correct folder in windows explorer.
LinkLabel link = new LinkLabel();
link.Text = transfer2;
//link.Text = "something";
link.Name = dirName;
link.LinkClicked += new LinkLabelLinkClickedEventHandler(this.link_LinkClicked);
LinkLabel.Link data = new LinkLabel.Link();
data.LinkData = #"C:\";
link.Links.Add(data);
link.AutoSize = true;
link.Location =
this.Display_Rich_Text_Box.GetPositionFromCharIndex(this.Display_Rich_Text_Box.TextLength);
this.Display_Rich_Text_Box.Controls.Add(link);
this.Display_Rich_Text_Box.AppendText(link.Text + " ");
this.Display_Rich_Text_Box.SelectionStart = this.Display_Rich_Text_Box.TextLength;
I started with this code. I am using a foreach statement to get the folder names and path. I tried to change the name of link so it will appear to the user that they are clicking on folder A, but when you click on folder A it uses the path to open the windows explorer where the folder is from.
Any ideas or help would be greatly appreciated.
Update
I changed the code a little so now it will display, but I cant scroll down. It appears to be only on the surface as I ran something in the rich textbox and it was scrollable while the link stayed on the surface.
I also added a picture so you can see what the problem is. I scrolled a little bit so it would b easy to see.
LinkLabel link = new LinkLabel();
link.Text = dirName;
//link.Text = "something";
link.Name = transfer2;
//link.LinkClicked += new LinkLabelLinkClickedEventHandler(this.link_LinkClicked);
LinkLabel.Link data = new LinkLabel.Link();
data.LinkData = #"C:\";
link.Links.Add(data);
link.AutoSize = true;
link.Location =
this.Display_Rich_Text_Box.GetPositionFromCharIndex(this.Display_Rich_Text_Box.TextLength);
this.Display_Rich_Text_Box.Controls.Add(link);
this.Display_Rich_Text_Box.AppendText(link.Text + "\n");
this.Display_Rich_Text_Box.SelectionStart = this.Display_Rich_Text_Box.TextLength;
Update: I am trying to make essentially a list of hyperlinks so I don't think I can use linklabel as I think it is in a fixed position.
For the first part of your problem, opening Explorer, you can do this on the click event for each item in the list (or the click event of the whole list area, as I describe later):
System.Diagnostics.Process.Start("explorer.exe", "\"" + path + "\"");
(the quote/slash thing is to make sure paths with spaces work)
For the UI bits, I've never even seen LinkLabels before, so I don't know how you got on the road to that, lol! I'm not sure whether you're using WinForms or WPF but in either you'd generally want to use something like a ListBox (or a custom control that behaves/looks precisely how you want, but I'd guess you aren't ready for that). In WPF you could set the ListBox's ItemsSource to your data and DisplayMemberPath to whatever property the text comes from (if it is only strings then just don't set DisplayMemberPath). You'd then set up an event for clicking on the ListBox and respond to that by checking what item is selected and running the code above to open Explorer.
If you want to get your UI working with minimal changes, try replacing the LinkLabels with Buttons (you can style them to look like links if you want, at least in WPF) since those work the same way.

How build link to local file in richtextbox

Hello i builded application with log that sow me what i am doing(where i saving files).
Sow my goal here is build links to all my new files that i created and open them by click
This my code
StreamWriter sw = new StreamWriter(Path.Combine(filepath), false, Encoding.GetEncoding("Windows-1255"));
sw.Write(Encoding.GetEncoding("Windows-1255").GetString(byteArray1255));
sw.Close();
rtx_Log.Text+= filepath;
here i created some file and i just want to show the pass in richtextbox and open it by click.
If RichTextBox.DetectUrls is set to true, the control will automatically detect links from the protocol and create a link.
"My File: file://c:/MyFile.txt" will display the file:// part as a link.
The RichTextBox.LinkClicked event is fired when the user clicks a link - and you can act on it as needed.
private void RichTextBox1OnLinkClicked(object sender, LinkClickedEventArgs e)
{
var filePath = new Uri(e.LinkText).AbsolutePath;
}

Redirect webpage to same tab in asp.net

I'm using a asp.net hyperlink control to open a web URL when a user click the hyperlink.
What I want to do is, say user click the hyperlink, so it should open new tab (not a new window).
if the user clicks the link again, it should not open a different tab. It should redirect user to the same tab which opened last time.
Update: here i found
Popup = window.open(URL, "LoginWindow");
This will redirect user to same window when the link is clicked. I'm using this functionality in popup windows. This works perfectly fine with Chrome & Firefox but not in IE.
It always open a new window rather than redirecting to the already opened one. Any Idea to solve this?
Regards
You can also try target="MyWindow" so it always open in this new window/tab. This way we can avoid opening new window/tab for every anchor link we click on.
The attribute target="_blank" is your only option. Much will depend on users' browser specific settings, you cannot change. In modern browsers this will open a new tab, in others a new window.
After opening in a new tab (new tab vs new window is a browser option, not an HTML option, see Open link in new tab or window) with
target="_blank"
you need to set the hyperlink target to self either when generating your the page in c# or in JavaScript
target="_self"
See http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.hyperlink.target.aspx
int i=1;
if(i==1)
{
Hyperlink1.target="_blank";
}
else
{
Hyperlink1.target="_self";
}
Try this code.Hope this will work for you
if (ViewState["hasvalue"].ToString() == "Clicked")
{
HtmlPage.Window.Navigate(new Uri("form2.aspx"), "_self");
}
else // First Time it will be opened in New TAB
{
Hyperlink1.target="_blank";
Hyperlink1.NavigateUrl="form2.aspx";
}
// Assign this value to session
ViewState["hasvalue"] = "Clicked";
}

How to set the Selected item in the tree view on the Left side of CHM file

I have a CHM helpfile for my WPF Application. My CHM file contains "htm" files for each page of my application. I want to open the help file for the corresponding page when the user presses F1 on that page. Right now I am able to locate the page and open that page by using the following code:
Help.ShowHelp(this, helpfile, keywordText);
where keywordText contains the URL of my htm file for the selected page.
But the problem is, that the panel on the left side (contents tab in a tree view) is not expanded to the page that opened in the right window. The panel on the left side always remains the same.
How Can I expand the tree view on the left side to the selected page?
Have a look at the code and the small GUI (snap attached). The topics are refreshed after the users button click (e.g. Help Topic1).
Please note the help file created with Microsoft HTMLHelp Workshop may need a auto sync parameter.
private void btnHelpTopic1_Click(object sender, EventArgs e)
{
// sHTMLHelpFileName_ShowWithNavigationPane = "CHM-example_ShowWithNavigationPane.chm"
// This is a HelpViewer Window with navigation pane for show case only
// created with Microsoft HTMLHelp Workshop
helpProvider1.HelpNamespace = Application.StartupPath + #"\" + sHTMLHelpFileName_ShowWithNavigationPane;
Help.ShowHelp(this, helpProvider1.HelpNamespace, #"/Garden/tree.htm");
}
private void btnHelpTopic2_Click(object sender, EventArgs e)
{
helpProvider1.HelpNamespace = Application.StartupPath + #"\" + sHTMLHelpFileName_ShowWithNavigationPane;
Help.ShowHelp(this, helpProvider1.HelpNamespace, #"/Garden/flowers.htm");
}
For download I provide a C# VS2008 Project including the code above and the help files with different help viewer windows (different CHM files for show case only).
Thanks.
The Problem is in the URL.
Before that I gave a URL like this
"Help.CHM::/html/MyHelp.htm"
And when I removed "Help.CHM::/" from the URL and Everything Worked Well..

create hyperlink from text

I am attempting to create a hyperlink from an existing url that I would like to 'share' with others. What I mean to say is that I am creating a 'share page' option for my phone app and I pass the current url via querystring to my SharePage.xaml, whereby the user may select an option to share the current url that the webbrowser control is on. For instance, in my SharePage.xaml.cs my code is as follows:
SharePage.xaml.cs
string urlToShare;
public SharePage()
{
InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
//base.OnNavigatedTo(e);
NavigationContext.QueryString.TryGetValue("curUrl", out urlToShare);
}
private void SocialNetworks_Click(object sender, RoutedEventArgs e)
{
ShareLinkTask shareLinkTask = new ShareLinkTask();
Uri shareUrl = new Uri(urlToShare);
shareLinkTask.Title = "Shared Link!";
shareLinkTask.LinkUri = shareUrl;
shareLinkTask.Message = "Check out this link!";
shareLinkTask.Show();
}
As of now this works although the LinkUri part of the message shows up as plain text instead of a hyperlink (which is what I would like to give as an option). The purpose would be to simply facilitate more efficient, quicker navigation to a url so that the user does not have to copy and paste the url into a web browser manually (something I've found annoying on the Windows Phone). Is there any way to do this in code behind in my SocialNetworks_Click event? Any code help or suggestions would be greatly appreciated, I have never messed with the Hyperlink option in C# as I am new to the language (and cannot find anything online about doing this in code behind if thats possible). Thanks in advance!
I think you're confused about what the ShareLinkTask is supposed to do.
This isn't meant to be displayed as a link in your app, or even in the task UI.
On the "Post a link" page this will be just text (and not tappable).
When the link appears in Twitter or Facebook or LinkedIn or whatever else you're sharing to then it will be a valid link that can be tapped/clicked.

Categories