Asp Hyperlink and Website link? - c#

I have a hyperlink in an aspx page, whose value is set in the code-behind. The C# code creates website link and sets the NavigateUrl to the URL. The problem is, when the link is clicked, the site address gets appended to existing website address.
e.g.
www.cnn.com <- main site which has hyperlink.
Let's say the new link is 'www.fox.com', when the link is clicked, I get an error, because now the page address looks something like this:
www.cnn.com/www.fox.com
Is there anyway to just display the link which I set behind the code.
ASPX page:
<asp:Hyperlink ID="ltrWebsite" runat="server"/>
C#:
ltrWebsite.NavigateUrl = "www.fox.com";
ltrWebsite.Text = "www.fox.com";
Thanks.

in markup cannot be a literal, it has to be an hiperLink:
<asp:HyperLink ID="ltrWebsite" runat="server"/>
in code behind do not forget the http:// prefix:
ltrWebsite.NavigateUrl = "http://www.fox.com";
ltrWebsite.Text = "www.fox.com";

Related

send argument of link to another link asp net C#

I have a default page with a gridview showing different categories.
When I enter a category, the name of the category is passed as an argument in the link, like
http://localhost:49442/category.aspx?category=Windows
I have a link with in there for a "New topic". I want to send the "category" parameter further.
I tried an approach but with no results.
The code is
<a id="topicLink" href="default.aspx">New Topic</a>
And the code in C# is
string x = Request.QueryString["category"];
topicLink.Href = "newtopic.aspx?category=" + x;
Error: the name topicLink does not exist in this context
How can I do this ?
Just add runat="server" in your html code like this:
<a id="topicLink" runat="server" href="default.aspx">
New Topic
</a>
Then you can use topicLink to change the href value just as you did. :)
ok, you want to use something like this when the event fires
$("#bttnId").click(function() {
$("a").attr("href", "your url here")
})
this code is based on a click event, in this instance its a button, but if its coming from a page load event from parameters you might want to take a look at purl.js which is a third party js file that will allow you to grab the parameters from a query string and then you can use this code
$(document).ready(function(){
var url = $.url().param('category');
$("a").attr("href", url)
})
you can get more information about purl from here https://github.com/allmarkedup/purl
please also note this is pseudo code and should be enough to give you a direction of where you want to go
I think you are looking for the HyperLink Control
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="default.aspx">New Topic</asp:HyperLink>
You can then alter the NavigateUrl property in code behind.
HyperLink1.NavigateUrl += "?category=" + x;
I think the best way is to create the hyperlink from the begin with the right URL
<a id="topicLink" href="default.aspx?category="+<%Request.QueryString["category"]%>New Topic</a>

How to get asp:hyperlink to display text instead of url

I have a panel that gets a hyperlink added dynamiclly through C# with values from an sql database.
However some of the URLs are really long and quite uneccessary to display.
I have not found any good ways to hide/disable the url showing and replace it with text. I can not use normal <a href> as it handled server side.
EDIT added some of the code.
<asp:HyperLink ID="moduleHyperlink" runat="server"></asp:HyperLink>
now in C#
HyperLink hyp = createHyperlink(btn.link);
moduleHyperlink.Controls.Add(hyp);
This will display for the user the entire btn.link (url string) which might be really long and it looks messy on the webpage. I would rather have a text saying "External Link", which when clicked redirects the user to the url.
You can add Text property to show some valid link title instead of showing the URL.
HyperLink hyp = createHyperlink(btn.link);
hyp.Text = "YourTextForTheLink";
moduleHyperlink.Controls.Add(hyp);`

Hide URL when mouse hover over asp:HyperLink

I have seen javascript:void(0) method used on <a> tags in HTML to hide the target URL of the hyperlinked object. Now I want to do the same on a <asp:HyperLink>, what should I do?
I am doing ASP.NET and here's the markup:
<asp:HyperLink runat="server" ID="hl1">Blah blah blah</asp:HyperLink>
In the codebehind I specified the NavigateUrl for hl1 using HttpUtility.UrlDecode method.
I tried hl1.Attributes[href]="javascript:void(0)"; in coebehind, does not work. Cannot open the NavigateUrl anymore.
You need to store the url you are wanting to navigate to in a hidden field and just set NavigateUrl = "#" in the markup as shown below. This way when user's cursor hovers over the link, the actual navigate URL will never be displayed at bottom of browser.
Then attach a click event handler on client-side for the hyperlink which you do by just setting onclick attribute of the hyperlink to a JavaScript function called navigate. The actual redirection to a new page is done by this navigate function.
In this situation, you will only see the URL of current page suffixed with #. For example, if your current page URL is http://localhost/mysite/view.aspx then it will show http://localhost/mysite/view.aspx# at bottom of browser.
Markup needed
<asp:HyperLink runat="server" ID="hl" NavigateUrl="#"
onclick="navigate();">Some Text</asp:HyperLink>
<asp:HiddenField ID="hdnURL" runat="server" Value="http://www.microsoft.com" />
JavaScript needed
<script type="text/javascript">
function navigate() {
window.location.href = document.getElementById("<%=hdnURL.ClientID%>").value;
}
</script>
Another approach that you can use if you must set the NavigateURL for the hyperlink in code-behind is as below. In this approach, you need to remove the NavigateURL before the content renders and store it in a global variable called linkUrl. The event that fires before the content renders is pageLoad and we will use that event to do this hack.
Global variable in JavaScript must always be declared outside all methods.
Then on clicking the hyperlink, we can obtain the value from the global variable of linkUrl and redirect user to that location.
Note: Keep the markup of the hyperlink the same as in first approach. But remove the hidden field from that markup.
<script type="text/javascript">
function navigate(event) {
window.location.href = linkURL;
}
var linkUrl = null;
function pageLoad() {
var link = document.getElementById("<%=hl.ClientID%>");
linkURL = link.getAttribute("href");
link.setAttribute("href","#");
}
</script>

URL is not rendering correctly in C#

I have Hyperlink in a datalist. HTML is given below:
<asp:HyperLink ID="lnkEntry" runat="server"><%# DataBinder.Eval(Container.DataItem, "Title") %></asp:HyperLink>
i have saved the URL of a Entry in the Database, like "http://test.com/posts/New-test-in-March" And used the following way to Bind it to Hyperlink in Item_databound of Datalist.
lnkEntry.NavigateUrl = objEntry.PermaLink;
But when it renders into the browser the URL get changed to "http%3a//test.com/posts/New-test-in-March"
i have tried to use the URLDecode but it doesn't make any change in output.
lnkEntry.NavigateUrl = Server.UrlDecode(objEntry.PermaLink);
Please help me how can i fix this issue.
you should use HttpUtility, like this:
lnkEntry.NavigateUrl = HttpUtility.UrlEncode(objEntry.PermaLink);
or WebUtility if running recent versions of .net.
Check this thread for more info.

How can I call a csharp variable in .aspx page?

In code behind page I create a variable like this (it belongs to one of the class)
string login_status = "you are not logged in";
I want to show this variable value in my Default.aspx page. What do I do?
You can create a variable of type string with protected access modifier and call it on your web page using:
<%= login_status %>
Drop a label onto your aspx page:
<asp:Label ID="Label1" runat="server"></asp:Label>
then in the codebehind, say:
Label1.Text = login_status;
Add a label control onto the default.aspx page. It will automatically be named Label1.
From your code behind you can place your string into the label.
Label1.Text = login_status
http://haacked.com/archive/2007/02/15/asp.net_tip_-_use_the_label_control_correctly.aspx
you can use asp.Literal control instead of Label.
You should add a control to the page (label, literal) that will display this nicely and set it's value in the code behind.
You can also user runat="server" on any html tag to set it's inner html (etc) in the code behind.
Finally if you want to do it the 'quick' way you can put <%=login_status%> in the markup.
There are multiple ways you can do
First Variables you trying to access should be protected or public
Then you can access these variables like
<% Response.Write(login_status); %>
or
<%= login_status%>
or you can use control like asp control like labels and change its Text property on code behind

Categories

Resources