Can I use Internet explorer's "find" to find a number (and go to the first result) in a webpage after I open a new IE window through ASP.NET?
Edit : Maybe I should clarify, I am opening a page on a site that is not mine, I cannot embed and run javascript on it...
Is this even possible?
Thanks
Roey
Not ASP or IE-specific: most sites use highlighting only, which you could also do on the client side using jQuery, like with the Text Highlight plugin.
You would then be missing the "Next" and "Previous" buttons, but I guess someone solved that problem already as well...
EDIT: As you clarified that the content is from some other site: this cannot be done unless you show the content from within your own URL (which is probably not accepted by the owner of the other site). Click for example a Google cache result (for which the content is served from a Google URL) which does do highlighting, while clicking a normal search result (which is served from the site's web server) doesn't do it. That's why Google offers a toolbar that allows for highlighting after all, and that's why people use bookmarklets.
One thing you can do is have your server request the page itself, and then modify the markup with something like jQuery as Arjan mentioned.
With ASP.NET, do something like:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.siteiwanttofindnumberon.com/pagetoopen.html");
request.Headers = new WebHeaderCollection();
//set up headers as necessary
request.Method = "GET";
//retrieve the response
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
b = new List<byte>();
while (b.Count < request.ContentLength)
b.Add((byte)response.GetResponseStream().ReadByte());
Now you have a List that represents the response stream, as though you'd sent the response yourself with a telnet client or a web browser. You can do as you please with this - for instance, injecting jQuery code to do highlighting for you.
Personally, I would manually scan this list for the information I want and wrap it in a span to highlight it. I would also try to put an in-page anchor at that point and redirect the target of the request to that anchor, thereby forcing the browser to scroll down to the highlighted text. Again, jQuery or another Javascript framework could accomplish this for you as well.
Finally, you'll want to find a way to render this stream to the client. I'm not sure off the top of my head if you can do this in a new window. You may need to manually construct an iFrame-modal-popup type thing, or use an HttpHandler.
Even if you had JavaScript access I don't think you could access the Ctrl-F functionality in a browser. Now that you don't even have JS, I guess the answer is no can do...
No, there's no JS-accessible mechanism to do this. There used to be a proprietary IE API for this called NavigateAndFind() but it was removed in IE7 or IE6SP2.
http://msdn.microsoft.com/en-us/library/ms536641(VS.85).aspx
Related
I'm used to PHP and I wanted to make a totally empty asp.net page "Hello World" (in the source code) I.E. not "<html><p>Hello World</p></html>" I want to totally strip it all out (and possibly manipulate the headers) so the view source is Hello World.
This is because I am writing an asp.net throughput environment (basically a triangulation server, to facilitate network security) that effectively redirects so I want to have no outputs that I don't accurately control.
Incidentally this also includes response headers as well. Full Control basically.
This is what I have so far
Dynamically, the browser do create "html", "body" etc... basic tags. It's the DOM, created by the browser from what it received.
To explore what you actually received from the server, you can inspect the source. Or better, use developer tools in your browser to look at the network part (to allow you to explore headers as well).
To manipulate what you send, you have to manipulate the context.Response object.
You can add all the headers you want from there.
from more info, see for example :
https://msdn.microsoft.com/en-us/library/system.web.httpresponse(v=vs.110).aspx
To add headers, see for example this question :
How to add Headers in HTTPContext Response in ASP.NET MVC 3?
example:
HttpContext.Current.Response.AppendHeader("Content-Length", contentLength);
What you use is the DOM explorer not the source code. Right click on the page and select View source. I bet the tags aren't there.
Headers can be displayed in the network tab. Just click on the page request in the network tab and you will see details like headers.
I am attempting to get the resulting web page content so I can extract the display text. I have attempted the code below but it gets me the source html and not the resulting html.
string urlPath = "http://www.cbsnews.com/news/jamar-clark-protests-follow-decision-not-to-file-charges-in-minneapolis-police-shooting/";
WebClient client = new WebClient();
string str = client.DownloadString(urlPath);
Compare the text in the str variable with the html in the Developer Tools in the Chrome browser and you will get different results.
Any recommendations will be appreciated.
I'm assuming you mean that you want the article text. If so you will need to follow a different course of action. The page you refer to is loaded with client script that injects loads of content into the base HTML document. This is done by executing the client-side script. You will need to parse the DOM after the script is executed to get the content you're interested in.
As others have pointed out, an actual web browser will parse the downloaded HTML and execute javascript against it, potentially altering its content. While you could try to do that parsing yourself, the easiest route is to ask a real web browser to do it for you and then grab the results.
The easiest solution specifically in C# would be to use the WebBrowser Control from Windows Forms, which essentially exposes IE to your program, allowing you to control it. Use the Navigate method to load the URL in question, then use the Document property to navigate the DOM. You can, at that point, get the outerHTML to get the final content of the DOM as HTML.
If you're not writing a Windows program and are interested more in headless operation, have a look at PhantomJS. It's a headless Webkit browser that is scriptable from javascript and would give you similar capability, although not in C#.
I have seen this on some survey websites. What is the C# code they use on the client side to keep the URL same, but when clicking the "Next" button, the same aspx page is maintained
without having any query string;
without any change even a character in the url; and
the grid, the data , the content, the questions keep changing?
Can anyone give a code-wise example how to achieve this?
My main query is how is this done in code-behind to change data of page and maintain same url.
Nothing simpler that a session, maintainted at the server side. Store a "current question number" in session, increment it at each succesfull postback and you have what you ask about.
Another possibility - a cookie which contains "current question number".
Both cookie and session are invisible in the query string of course.
"change data of page and maintain same url." Answer is Server.Transfer.
This method will preserve url.
The Next button may submit a form using the HTTP POST method. The form data may contain the session, question and response data. The site uses that to build a new response. Unlike a GET, a POST does not incorporate data into the URL.
Developers will typically accomplish this task by using AJAX. The basic premise behind it is that only a certain portion of the page (e.g. a grid or content area) will make a server call and retrieve the results (using Javascript). The effect achieved is that there has not been a full post back, which is why you don't see the URL or parameters changing.
It is possible to do this using jQuery, pure Javascript, or Microsoft's UpdatePanel.
oleksii's comment has some good links as well:
That's the AJAX magic. There are many JQuery plugings for this, for
example this one with a live demo. You can also program it easily
using JQuery Get or Post or any other wrapper that use XmlHttpRequest
object.
I have a bunch of parameters that I need to pass onto a second page via request headers. At first I tried via JS but I found out that that's impossible (please correct me if I'm wrong here).
So now I'm trying to do it in the code-behind (via C#). I want to write a bunch of custom request headers and call Response.Redirect or something similar to redirect user to the new page.
Is this possible? If so what methods do I have to use?
Edit: unfortunately using QS parameters is not an option here as it's out of my control.
Use a Server.Transfer("somepage.aspx?parameter1=value");
There is no client redirect then.
You can try setting the headers and do a Server.Transfer - I believe that will work to - up to you, but using the querystring is a bit more readable to me and doesn't show up in the clients browser.
you need to look at state in .net their are various ways to achive state.. in a stateless environment.
i would put it in the session object on page one.. read it on page 2...
create a session object on code behind page 1
read from session object on page 2.
or if you read the msdn state documenation on request paramters this will show you the options avliable.
JS dont worry about doing tricky stuff with it.. mostly trickey is wrong.
On a button click event I am required to POST to a page on an external website and redirect there. I get how to do this using a GET method
Reponse.Redirect("www.somesite.com?my=params&as=aget")
But how can I do this on as POST?
I don't want to post the entire form as this button event is called within a repeater
Depends.
If you want to post the exact input of a form you have on your site (that is, you just replicate a form the other site has), then just set the form's action to the URL you want to post to and the browser will do everything for you.
If however you want to post some values you generate on the server (perhaps based on the input from your form), I'm afraid it's not possible. You can't redirect using a POST. Redirect is GET by it's nature.
BUT you might be able to fake it by doing a POST (using something like System.Net.WebClient) and then a redirect (it depends on how the other site handles the GET - it might display the same thing that it did on the POST, or not).
One more option (for the second case) would be to to do an AJAX call to your server, which will compute the required values, then do the POST to the other server from Javascript.
You can build up the request using WebClient, adding the appropriate headers.
My inner forms don't contain the runat="server" attribute so I can do what I want. I do get this problem though ASP.Net First inner form in Server Form doesn’t POST.
Jquery is life saving in this situation. Used for one of my project and works like a charm. Give it a try : Peter Finch - Using Javascript to POST data between pages