URL is not rendering correctly in C# - 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.

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>

Access custom attribute and value in c# code behind

I want to access custom attribute in the code behind, how can I?
e.g. my server control
<asp:HyperLink ID="id_1" runat="server" Target="_blank" NavigateUrl="xyz" Style="color:red"></HyperLink>
'Style' is not an Attribute of HyperLink as a result it is not rendered in the HTML, so I want to access the Style value in c# code behind and once I access it i can dom something like below which works.
id_1.Attributes.Add("Style","color:red");
Any help is appreciated. If you guys have any other suggestion, that is appreciated as well.
string value = id_1.Attributes["Style"].ToString();

Embedded code (<%= %>) in button control

I'm working on a project where I'm using the <%= getString("key")%> to dynamically get the appropriate text.
this works great when i use it in simple p tags, but i can't find a way to do this with controls like Button/Label etc.
Is there any way, other than calling
Mybutton.Text = getstring("key");
to dynamically add the text?
The idea is that getString retrieves af language code, and depending on that code gets a string in the appropiate language.
I've been looking around, but all i come across is using the embedded code tags directly in the aspx pages, which wont cut it for buttontext.
If you can use DataBinding instead of the <%= operator, you can use:
<asp:Button ID="MyButton" Text='<%# getstring("key") %>' />
This is a good explanation of why <%= won't work in this context.

Convert url in asp?

I have Web-App. Somewhere in my page I used following code to create a Hyperlink.
<%= Eval("text") %>
as you see this code must be work but there is a little problem. content of NavigateUrl is something like this url.
"~/account/login.aspx"
How I must resolve that URL?
Update : I must say I cant Change value of NavigateUrl cuz that load from Xml-Datasource. I must change that in UI something like:
Eval( Resolveurl("NavigateUrl") )
You can try using Control.ResolveUrl.
Typically you would use Page.ResolveUrl to resolve a path relative to the current page, and this.ResolveUrl to resolve a path relative to the current control (UserControl or Page).
In your case, the tilde is relative to the application root, so either will do.
In response to your comment, you want to use something like:
ResolveUrl( (string)Eval("NavigateUrl"))

Asp Hyperlink and Website link?

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

Categories

Resources