I am looking for a way to change page url (change one of the url key's value) when the user refreshes the page. I am doing this to reflect changes on the contents of the page. I have tried several methods but they all have a loop problem. Below is one of the methods:
In my html page:
<body onLoad="CheckPageLoad();">
<input type="hidden" name="trial" id="trial" value="hello" />
</body>
In the page.asp page: (inside a script tag)
function CheckPageLoad() {
if ($("#trial").val() == "hello") {
// This is a fresh page load
alert('Load');
$("#trial").val("hi");
} else {
// This is a page refresh
window.location = url;
alert('Refresh');
}
}
Issues:
Every time the page is refreshed, the Load alert is always displayed. I thought the load is done once. It looks like every time the page is refreshed, it reloads so the value always remains to be hello.
I tried using the script tag only and the new url is opened but keeps looping. (This is the main problem)
Any suggestion to update the above method or use another method to do the refresh is appreciated. Below are other methods I also tried:
window.onbeforeunload = unloadPage();
function unloadPage() {
//do something
//works but loops
}
Thanks
I put the following code into a basic page and it demonstrates why you are getting the described behaviour:
<html>
<head>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript">
function CheckPageLoad() {
if ($("#trial").val() == "hello") {
// This is a fresh page load
alert('Load');
$("#trial").val("hi");
} else {
// This is a page refresh
alert('Refresh');
window.location = window.location + '?id=1';
}
}
</script>
</head>
<body onLoad="CheckPageLoad();">
<input type="hidden" name="trial" id="trial" value="hello" />
</body>
</html>
When you set the window.location it changes the url (well duh!) which therefore means you are loading a new page. When the page is refreshed you get a "Refresh" alert before a "Load". This demonstrates that the page is posting back first, but the onload event causes the page to navigate to another location, therefore loading it again. It is my understanding that updating anything in the URL ultimately is a new request/newly loaded page.
You might find the following useful regarding the viewstate in asp (seeing as you suggested you are using an asp page): http://msdn.microsoft.com/en-us/library/ms972976.aspx
My suggestion would be to completely remove the window.location = url; part of your code in favour of looking at the data which was posted back instead. This means that you aren't unnecessarily loading the page twice on postback and allows you to access the data from any updated inputs.
Related
I have an aspx page. I've added a ScriptManager to it, and set EnablePageMethods=true, and created a static method marked as [WebMethod] on the server-side.
I have always worked with WebMethods, and I've never seen this error before.
On javascript, PageMethods is accessible. But when I call my method, the Page_Load method is fired, instead of the WebMethod.
I've searched and found other people had this issue as well. But no answers.... Any ideas?
HTML:
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true"></asp:ScriptManager>
JS:
PageMethods.Test()
C#:
[WebMethod]
public static void Test()
{
}
So after founding the culprit of why controls in my update panel was calling post back twice I found it was because of a page method being called.
After finding LcSalazar solution I did disabled Friendly URLs and everything was working. But I find the Friendly URLs to be more clean so I found a solution .
On your master page add the following .
<script type="text/javascript">
$(function () {
try {
if (PageMethods.get_path().indexOf('.aspx') == -1)
PageMethods.set_path(PageMethods.get_path() + '.aspx');
} catch (e) {
}
})
</script>
Once the page methods path includes the .aspx the page_load doesn't fire again.
Alternatively look into using ASP.Net Web Api . Better practice as well when it comes to reusable methods instead of declaring the methods in you Base Page or every page you are trying to use it .
I discovered that the problem on my case is that I'm using friendly URL's. Since PageMethods references the server-side page by its address, there you have the issue. It's been discussed here, on CodePlex: http://aspnetfriendlyurls.codeplex.com/workitem/3.
Apparently there are workarounds for this, but I ended up making a manual ajax call to a generic handler (.ashx).
When you submit a form that has runat server it processes the full .net lifecycle. Which fires the Page_Load of your .cs class. If you want to make it strictly want to fire the webmethod I suggest using an ajax call to your webmethod.
Disable friendly urls. Comment this out in App_Start\RouteConfig.cs
routes.EnableFriendlyUrls(settings);
In my default web page i have some pop-up Iframe when a user click something (using JS).
How can i prevent users from go directly to the link : WWW.mydomain.com/Iframe.aspx
and see the full page but still give them the access whenever they click the Iframe button from the default page.
default - default.aspx
Iframe - Iframe.aspx.
Thanks.
There is a technique called Frame killer used by web applications to prevent their web pages from being displayed within a frame. In your case, it's a bit opposite, but we can borrow a similar idea.
If you display your Iframe.aspx as a popup when you click a button. You could check for the window.opener in your Iframe.aspx throw error if window.opener is empty. Like this:
<script type="text/javascript">
if(!window.opener) {
throw new Error();
}
</script>
If your Iframe.aspx is embedded in an iframe of the popup. You could check it further using window.parent.opener
<script type="text/javascript">
if(!window.parent.opener) {
throw new Error();
}
</script>
Note that this technique does have limitations as pointed out in the link above.
I am using ASP.Net AJAX UpdatePanel to load the right part of the page.
As that part will take some time to load, I would like to load it after the other parts of the page is loaded.
I can use either normal AJAX or ASP.Net AJAX but I chose to use the latter as I want to try it out.
I found out that my UpdatePanel is always loaded.
I want it to be loaded only after the page is ready.
Some says to use the timer, some says to use some javascript.
But I still can't get it done.
So, these are my 2 obstacles, to stop loading when the page starts and to start loading when the page is ready
why don't you enable and disable on page events? for example. try in Page_PreLoad event, set the update panel's enable property to False. While in Page_LoadComplete event, set it back to enabled = true
UpdatePanels use Ajax to update, not on first load.
If you want it to load quickly on first load, avoid any expensive processing, database calls on load. Put them in an
if(IsPostBack)
{
//your long processing
}
Now use an AsyncPostBackTrigger to make your updatePanel postback.
try with this code ( Conditional Mode + Update Method)
<asp:UpdatePanel ID="YourIdPanel" UpdateMode="Conditional" runat="server">
//In order to force loading
YourIdPanel.Update();
<script type="text/javascript">
var currentItemID=$('#<%= labcurrentItemID.ClientID %>').html();
if (currentItemID != null) {
$(document).ready(function () {
$('#main_container').load('/Custom/Going%20Places/PopularRelatedArticle.aspx?currentID=' + currentItemID);
});
}
else {
$(document).ready(function () {
$('#main_container').load('/Custom/Going%20Places/PopularRelatedArticle.aspx?currentID={7F0811A7-D484-4675-8A23-0AEB235B9B5F}');
});
}
</script>
I want to create a button in visual studio that refreshes the entire browser page. Currently, I have a web part that is deployed onto sharepoint 2010. I would like to add a button to refreshes the page dynamically so that my web part content changes. It is because, my web part has a random code which changes information every 10 seconds, but to view the changes the page has to be refreshed constantly. I want to just add a button to my web part to hit refresh. Is it possible?
There's a few ways, but all use JavaScript on the button. Here's my favourite:
<input type=button value="Refresh" onClick="history.go()">
The "type=button" prevents the button from causing a postback. onclick="history.go() says to go to the last item in the history list, i.e. the current page.
use this one
input type="button" value="Reload Page" onClick="window.location.reload()
you can try this..... If you want to refresh the page in button click , you can set this into button click....
<html>
<head>
<script type="text/JavaScript">
<!--
function timedRefresh(timeoutPeriod) {
setTimeout("location.reload(true);",timeoutPeriod);
}
// -->
</script>
</head>
<body onload="JavaScript:timedRefresh(5000);">
<p>This page will refresh every 5 seconds. This is because we're using the 'onload' event to call our function. We are passing in the value '5000', which equals 5 seconds.</p>
<p>But hey, try not to annoy your users too much with unnecessary page refreshes every few seconds!</p>
</body>
</html>
I have 6 thumbnail images as asp:imagebutton instances. These are treated as triggers for asp:updatepanel control on the page which contains an asp:image control.
When the user clicks on the thumbnail, the image in the asp:updatepanel's image control changes to the clicked thumbnail image.
The users are also allowed to again enlarge the image by clicking on the enlarge button (this runs the lightbox function). This works fine.
Question
The problem is that the enlargement works when the page loads, however when the user select a thumbnail and then tries. The method (lightbox) does not work.
I have had similar problems with javascript functions and the asp:updatepanel. Has anyone else faced similar issues? If so, how do I solve this issue?
A $ function (which is a DOM ready function) will be only be executed when the page loads for the first time.
Any further AJAX call (which is a partial loading/rendering) will not fire DOM ready function. Which is the reason, you were not able to get it working.
In you case, this function binds your anchor link (button) with the lighbox behavior the first time the page loads. so, it works. The next time when you refresh the update panel (which is a partial render) the button is not bound to lightbox again. Unless this binding is achieved it will not show up.
Normally a script control is used in such cases to fire the event every time the control is loaded.
If you are not bothered here about segregating related objects, the solution like pageLoad is still fine.
Following javascript was defalut one, it work fine without asp:UpdatePanel
<script type="text/javascript">
$(function()
{
$('#gallery a').lightBox();
});
</script>
but it is not working with asp:UpdatePanel so there is the need to call function on page load and it works fine.
<script type="text/javascript">
function pageLoad(sender, args)
{
$('#gallery a').lightBox();
}
</script>
Thanks guys for helping.
Solved:
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<script src="../slimbox-2.05/slimbox2.min.js" type="text/javascript"></script>
<script type="text/javascript">
function pageLoad() {
jQuery(function ($) {
$("a[rel^='lightbox']").slimbox({/* Put custom options here */
}, null, function (el) {
return (this == el) || ((this.rel.length > 8) && (this.rel == el.rel));
});
});
}
</script>