So I have a Hyperlink called lnkTwitter:
And I'm trying to set the url in the code behind:
lnkTwitter.NavigateUrl = string.Format("http://www.twitter.com/home?status={0}", Server.UrlEncode("I'm Steven"));
When I do that and hover over the link, the url displays correctly in the status bar as "http://www.twitter.com/home?status=I'm+Steven", but the actual url, if I click on the link or look at the link's properties, is "http://www.twitter.com/home?status=I%27m+Steven".
For some reason, this only happens in Firefox; in IE, I am taken to the correct url.
Have you tried to view source code?
If source is ok, then there's no troubles with your code.
Firefox just likes to unescape the urls that it shows. While this can be confusing it should not cause your code or the sites you link to (twitter, in this case) any problems.
If you follow the link and then copy the url and paste it into Notepad or something then you should get the escaped form that was actually used instead of the unescaped form that was displayed.
Uri.EscapeDataString, Uri.EscapeUriString, HttpUtility.UrlEncode and HttpUtility.UrlPathEncode are available to use in C# out of the box, they can not convert all the characters exactly the same way as JavaScript escape function does.
Solution: Use JScript.Net's own implementation. Simply reference the Microsoft.JScript.dll and use the Microsoft.JScript.GlobalObject.escape() method to encode your url.
Related
I'm trying to read lines from a website and then, copy it into my textBox2.
textBox1 will have a website's URL like http://example.com.
When I click on button1 I'd like to read HTML content from the above URL, and please that info textBox2.
Should I use HtmlAgilityPack?
How can this be done?
2 =================================================
So, for example, i copy this link into my "textBox1"
http://www.mineshaftersquared.com/server/DareCraft#
So, is there a way to make app copy everything from:
//*[#id="Plugins"]
to
//*[#id="rightInfo"]/section[2]/div/div[1]/table[2]
/this is XPath
/It don't have to in XPath, but this was the only way I could show.
You don't install HtmlAgilityPack, you reference if from your project.
Read about WebClient class (OpenRead method), you probably want to use it to get the pages.
Here is a tutorial you might want to start with:
http://www.codeproject.com/Articles/33798/HTTP-GET-with-NET-WebClient
I have some text which is loaded from a resource file. Ordinarily, to get dynamic content I would use:
string.Format(GetLocalResourceObject("SomeText"), PhoneNumberAsString)
I want to do the same with a link, only the link needs to be application relative as I have URLs like mysite.com/page.aspx and mysite.com/fr/page.aspx.
I normally use an <asp:HyperLink /> tag to create the links as I can then just put a squiggle at the start NavigateUrl="~/page.aspx". However, I don't know of a way to get a dynamic HyperLink to appear as a string without adding it as a control to something.
Simply writing ToString() outputs System.Web.UI.WebControls.HyperLink..
How do I get a link out of a resource file and make it into a hyperlink using ASP.NET Webforms?
UPDATE
With some help from the answers I now have the following code on my page:
<p><%= string.Format(GetGlobalResourceObject("Resource", "MoreThan1000Users").ToString(), ResolveUrl("~/contact-us.aspx")) %></p>
and in my resource file I have:
If you would like more than 1000 users please call our sales team.
Does this seem like good practice or is there another way to achieve what I'm doing? I don't know if I should be happy or not that there is HTML inside the resource file.
Since you haven't posted code, I'm guessing somewhere you have a HyperLink WebControl object that you're hitting ToString() on. If that's the case, you can access the URL associated with it using its myHyperLinkControl.NavigateUrl property.
If you're storing the link in your resource with a squiggle/tilde (which is good) then you can replace the squiggle with your application location. If you have a control/page, then you can easily call its ResolveURL method (which takes the tilde and automatically replaces it) There's some existing solutions if you don't have a control/page reference with your context, then there's some discussion to do that here: ResolveUrl without an ASP.NET Page
I guess this is what you want:
Server.MapPath("~/page.aspx")
That will work inside your aspx and your code-behind.
I am experimenting with URL Rewriting. The first time it displays with the correct url. After perfroming any event handling, the form posts back, and then it has wrong url.
Like the page url is http://devweb.tsgdomain.com/nphnewdemo/Enewsletter/68 and when i click url button then it gives wrong url http://devweb.tsgdomain.com/nphnewdemo/Enewsletter/popup.aspx?name=dev-test-please-review-it insted of this url http://devweb.tsgdomain.com/nphnewdemo/popup.aspx?name=dev-test-please-review-it.
Please suggest some answers to why this is happening (and how I can make this work correctly).
This is happens because the form is rendering using the actual URL (and not what user see).
If you like to change that you can be rewriting the form attribute making a global handler of the form.
Here is a full solution that is tested and working.
http://www.koders.com/csharp/fid39B3A4A2AD871AA78E7E5D8643A076EF4352CDF9.aspx
In the aboce code the line that make the change is the
value = Context.Request.RawUrl;
This might be a ridiculously easy question, but it has me stumped. I have a web form where I'm trying to create a hyperlink in the code behind to a file server share, e.g. file://myServer/Shared/, but when the page is rendered, the link doesn't include the server name, i.e. file:///Shared/. I don't know why this happens. Any help or insight is appreciated.
UPDATE:
Sure, here is the snippet where the link is being set.
//The link is embedded in a table
HyperLink link = (HyperLink)e.Row.Cells[1].Controls[0];
link.NavigateUrl = #"file://myServer/Shared/";
As a test, I assigned the link to a string value and the link prints the expected url.
string foo = link.NavigateUrl;
//Displays this
"file://myServer/Shared/"
I don't know why this doesn't appear when the link is rendered in the final page.
UPDATE 2:
Ok, so I know I have to set the absolute path in the code-behind, I thought that's what I was doing, but it still won't render correctly.
UPDATE 3:
I followed pjacobs suggestion about setting the test property and it was actually a step in the right direction. I have the following:
link.Text = "link text";
Now the link gets rendered as follows: file:///myServer/Shared. I'm almost there except it gives the extra '/' in front of the server name. I'll keep playing with it, this seems like it should be so simple, I don't understand why ASP.Net renders the URL differently.
You have to set the Text property of the HyperLink... link.Text = "whatever"
Are the resources inside the project? If so:
you need to use ResolveUrl to resolve the "web location" of the resource.
http://msdn.microsoft.com/en-us/library/system.web.ui.control.resolveurl.aspx
if you're using an asp.net control you shouldn't need to use the resolve url, but you need to refer to the location of the file relative to the path of the project.
If not:
Did you give the proper read account to ASP.NET process?
Use a virtual directory?
http://www.dotnetspider.com/tutorials/AspNet-Tutorial-86.aspx
Assume I have the link http://www.somesite.com/file.aspx?a=1&b=2
And now I want to remove all the parameters, so it becomes:
http://www.somesite.com/file.aspx
Or I may want to remove only 1 of the parameters such as
http://www.somesite.com/file.aspx?b=2
Is there a way to do the above in C#? What is happening is that I am coming from another page with a parameter called edit in the url, but when the page does a post back, the edit parameter is still there, so it still thinks it is in edit mode. Example:
User A goes to page one.aspx and clicks on an edit link. They are taken to two.aspx?edit=true. During page load, it sees the the query string parameter edit is not null and it puts the contents in edit mode. Once the user is done editing, the page is refreshed, but the url is still two.aspx?edit=true and keeps the contents in edit mode, when in fact it should be two.aspx
Request.Querystring is read-only collection - You cannot modify that.
If you need to remove or change the param in querystring only way out is to trigger a new GET request with updated querystring - This means you will have to do Response.Redirect with updated URL. This will cause you lose the viewstate of the current page.
Use the PostBackUrl property, for example:
<asp:Button ID="DoneEditingButton" runat="server" Text="Done editing" PostBackUrl="~/two.aspx" />
When you are done with the edit you are doing a post back so just define the action to post to two.aspx instead of just posting back to itself that way it will drop off the get parameters.
How about checking Page.IsPostBack to see if the current request is a postback or not?
if you have only string, you can use:
strinULR.Split('?').First();
or
strinULR.Split('?').FirstOrDefault();
Try something like this.
if (url.Contains("?"))
url = url.Substring(0, url.IndexOf("?"));
In this example, I'm checking if the url even contains a query string, and if it does, subtracting getting the "left part" of the string prior to the ?.
Late but you can do this to remove query string from URL without another GET Request.
http://www.codeproject.com/Tips/177679/Removing-Deleting-Querystring-in-ASP-NET