How to refresh just a specific portion of an HTML page? - c#

I have one HTML file containing several <div> elements. I want to refresh just part of the page using either JavaScript or C#. Can someone help?
I am trying to do it this way:
document.location.reload(document.getElementById("contentdiv"));
It reloads the whole page. I wish to reload contentdiv. If contentdiv is at the middle of the page then it should load only that part.
Thank you.

You could move the contents of everything you want reloaded into an external file, and either use the <iframe> tag and only refresh that frame, or you could use JavaScript and refresh the div with Ajax.
Ajax isn't that simple to explain in a short answer, but you can find plenty of information on it here: http://www.w3schools.com/Ajax/ajax_example.asp or if you use a framework like jQuery ajax is much easier.
iFrames can be implemented (on mypage.html, for example) like so: <iframe src='mypagecontent.html'></iframe> and in mypagecontent.html you could use <script type='text/javascript'>window.location.reload();</script> to refresh the frame.
Not sure if this is what you're looking for, but hope it helps somewhat.

What ASP.NET Framework are you using? If you are using Web Forms, look into UpdatePanel

Related

Timers from ASP.NET to MVC Razor

I'm moving an app from ASP.NET/C# to MVC and I have some Timers <asp:Timer>
I'm trying to find exactly the same tool for MVC (Html5 or DevExpress) but seems like there is not or at least my intellisense doesn't show it.
Anyone knows about it and can post the squema of it?
You will need to use Javascript yourself.
Take a look at window.setInterval() or one of the many jQuery timers.
Another option, that doesn't involve javascript is the HTML META tag.
example:
<meta http-equiv="refresh" content="30">
content is specified in seconds. META elements live within the HEAD element.

Execute C# code while using .load jquery

Again, I've looked around to try to find some posts on this and there are many but none that address my specific question (that I could find).
What I am looking : I have a projects and it loads different pages (.aspx) in an iframe dynamically. Now I am trying to remove iframe and add a div and load aspx pages inside that div, using this :
$("#containerDiv").load("test/default.aspx", function () {
});
it loads aspx page easily but I am unable to execute C# code which is written in default.aspx.cs may be its not a good practice but I want to know is there any solution of my problem.
$.load will not your c# code why because it load only the contents of your aspx page not of aspx.cs.
Use user controls instead.
If need help how to implement user control refer this link.

How to make Google crawlable AJAX pagination in .NET

I have searched a lot and have found enough about it but I am unable to apply it.
I have an Ajaxhandler to request as:
..test.ashx?pagenumber=1
And I want to make it crawlable.
The way you'd have to do it is to render a plain HTML navigation based pager. And then on-load (using javascript ... jquery would be good) convert all of the pager links to ajax. That way, when googlebot queries the page, it will be able to navigate all the links as they were originally rendered.

Should I load javascript in an IFrame embedded in an ASP.NET page?

Is the best approach to loading javascript files to put them in an IFrame and then embed the same in asp.net pages? I have read somewhere that this will help to boost page-loading times.
Best is to put script references at the bottom of the page. This ensures that all content is loaded before the scripts. Don't use the iFrame unless needed.
When looking at page load times, it depends on where your code is stored. If it is in the html then it will be reloaded each time the page is loaded. If you move it out to a js file, it will be loaded the first time and cached after that so then you shouldnt have a problem with affecting the load times.
You can use jQuery's $(document).ready to make sure that all the elements are loaded before running any code.
Don't go for the iframe approach. Put the scripts as far away at the bottom of your html as possible and your css as high as possible. Also try to go for unobtrusive javascript if possible. jQuery's great at this so you surely want to take a look into this. The benefit of jQuery is that it's also put on CDN servers which also speeds up performance.
I found these guidelines to be a great help when I was upgrading the performance of one of my former projects at a client: Best Practices for Speeding Up Your Web Site.
Some great tools are also Firebug in combination with YSlow.

Opening an external page inside our page

I used to implement this above title by using iframe but now I dont want to use it any more I have some plans in my mind I need to implement them by opening an external page inside our asp.net page without using any iframe I have only simple aspx page with div tage and panel and some other serverside componants, I just want to know how I can do it without iframe ? I don't want to design new complex control but I am looking for some methods can do that for me.
I have to mention that I need to control area which is loaded by external site as the same as iframe but the difference is that iframe can not handled by ajax even you put iframe inside the update panel your page has refresh and postback while you are changing the src value programmatically (in c# code) so we have to design some others methods what is the solution ?
I thought I can make request an get some html and show into div but I couldn't to implement it.
You could
Make a WebRequest on the server-side and then set the div's text to HTML returned
You could make an invisible iFrame to make the request and then use JavaScript to grab the HTML from the iFrame and put it in a DIV. (EDIT: Comment suggests this won't work)
You can't generally make calls (like XmlHttpRequest) to external websites because of cross-site scripting issues.
Your direct request, "opening an external page inside our asp.net page without using any iframe" is not possible, by design.
You mention AJAX. You can use AJAX to load your page, remove the headers (or do that serverside) and replace the <body> tag with a <div> tag (or do that server side too). This way, you can place the contents of your page anywhere you like. As a container, I suggest you use a block level element, a <div> would suffice.
The only (!) problem here is: cross-site requests like this are not honored by browsers. You can solve this server-side by loading the page from elsewhere using WebRequest or similar means.
Depends on where you'd like to merge the data. If you'd like to merge the data on the client browser, your only other option besides frames is to use Javascript/Ajax.
You can do a jQuery.ajax() on page load and use the html() method on a div to populate it with the textual result of that AJAX call.
Try to use as little of the WebForms control hierarchy and life-cycle as possible. It sounds like your problem can be fixed with AJAX if you don't mind the second request on page load.
If you would like to merge the content on the server side ( rarely the right thing to do ) you can use System.Net.HttpWebRequest to get and merge the data before returning it to the browser.
there's no substitute for an iframe in your situation. you're not going to be able to make ajax requests to the other site due to security concerns. you could retrieve the contents of a single page server side and render it to the client but none of the functionality will be included, since the content is now running in the context of your own site.

Categories