Refresh page button using C# - c#

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>

Related

Calling Page Click event via User Control

I am working on a project in which I am binding Cart:
As shown in image I am populating my cart. Using User Control. And in User control I have populated that using One simple aspx form that is minicart. So now as you can see there is one button Checkout in That page. Which is populated through User Control but actaully its inside my minicart form. So now if I am trying to redirect using that Checkout link button. But that is not calling my click event rather its just postback the page.
Basically I want to call aspx page's click event through user control in my another aspx page.
I have tried to call that click event through many ways. But still page is only doing postback. I just want respose.redirect to my checkout page.
Even I have tried by using <a> also:
<a id="lnkcheckout" runat="server" href="javascript:return false;"
onclick="checkout_onclick">Checkout</a>
But not succeed..
Don't go for a server side for only redirecting you can take one achor tag also
<div align="right" style="padding-top: 10px;"> <a id="lnkcheckout" href="checkout1">Checkout</a> </div>
And it will work :) I hope this will help you
Try to use event.preventDefault() or use return false like ,
jQuery(function(){
jQuery(document).on('click','.morebutton', function(e){
e.preventDefault();
alert('ok');// you code in place of alert;
});
});

How to update page url on page refresh?

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.

Refreshing Datagrid to the Parent page on click from the IFRAME page

I have a ASP.net page which has a Datagrid to display all the Records of a Table. On Click of an Add Button pops up an IFRAME with different .ASPX page to be saved as a new record. I would like to add the new record to the Datagrid on the parent page from the IFRAME.
Please suggest how can I refresh the Datagrid alone without refreshing the entire page.
Note: The Save button is in IFRAME in an another page and the Datagrid is in another Page.
Thanks
If you just want to refresh Datagrid only , you may consider Ajax . Here is the link
Or You may also consider a java script to refresh the grid, here is the code to give you an idea
protected void grid_view_refesh()
{
grid_view.Bind();
}
Button:
<button id="btn_refresh" onclick="btn_refresh_click();">Refresh</button>
javascript
<script type="text/javascript" language="javascript">
function grid_view_refesh()
{
// you code to refresh grid
}
Hope it will help
EDITED
You can try this code on your popup windows ( Child window)
<script type="text/javascript">
function CloseWindow() {
window.close();
window.opener.location.reload();
}
<input type="button" value="Close Window" onclick="javascript: return CloseWindow();" />

Prevent left navigation menu to be refreshed with Response.Redirect()

I have my left navigation menu is on my master page and and i am using update panel also, my problem is whenever I call to Response.Redirect() the entire page is getting refreshed and the left navigation menu's status is getting flushed out every time. I need to maintain the left navigation menu's status.
If you don't want to use framesets, or build an Ajax driven website, the standard way to handle this is to implement a sitemap for your website so that the left hand nav knows what state to display when it loads each page.
Most probably you will need to run a javascript/jQuery code on page load.
you will run this code to get the current page name/title/anything that you can use to differentiate between your pages, then you will use this value to update the left menu item that points to this page.
Two ways to do that.
1.use frame
<html>
......
<body>
<frame>left menu</frame>
<frame>right</frame>
</body>
</html>
redirect right frame.
2.use ajax and div/frame
<html>
......
<body>
<div>left menu</div>
<div>right</div>
</body>
</html>
you can use ajax(try jquery) to update right div.

Want to show a message box using javascript

Hi everyone I have a web form in which I am having a button on clicking which data back up is being taken, I used the following javascript :
<script language="javascript" type="text/javascript">
function showPleaseWait() {
document.getElementById('PleaseWait').style.display = 'block';
}
</script>
<asp:Button ID="btnTakebackup" runat="server" Text="Take Backup" Enabled="true"
onMouseDown="showPleaseWait()" CausesValidation="false" />
<div id="PleaseWait" style="display: none;">"Please Wait Backup in Progress.."</div>
Hi I am using a button to take a back up.
Now I want to show a message in btnTakebackup_Click() event, whether Back up was successful or not.
I used Response.Write("<script>alert('abcd');</script>"); in btnTakebackup_Click() event.
But the problem is that I want to show the page also, which is not showing instead white background is showing.
Thanks in advance...
To show a message box alert should be able to write out a new script to the response stream:
var script =
"<script type=\"javascript\">" +
"alert(\"Backup in progress, don't go!\");" +
"</script>"
Response.Write(script);
However much this is distasteful, I suppose it is sometimes "necessary".
You can add client side event handlers to ASP controls:
How to: Add Client Script Events to ASP.NET Web Server Controls
Cheers.
Do you really want it to be an alert? (You should know that they lock up the whole browser not just the tab your page is on), do your users really need to acknowledge the backup success by clicking ok or just be informed of it?...
I suggest you have a div on the page that says "Backup successful". The visibility of which can be set by a boolean property BackUpSuccess which you can set to true in the code behind you mention.
<div id="backUpSuccess" <%=BackUpSuccess ? "" : "style='display:none;'"%>>
Backup was successfull
</div>
...you can style the div as you like in your .css file to get attention.
If you really do want an alert you could run some JavaScript on page load to check the content of a hidden input that you set server side in similar fashion...but running javascript on page load is tricky...unless your using jQuery and then you will know it's very easy.
From your question, I understood that after clicking on the button, the data back up is happening, but the alert is not displaying as soon as you clicked the button.This is because you are calling the JavaScript in the button click event which will be fired only after all the code in the button click is executed.I suggest you to add a JavaScript function in the .aspx source page it self and call the JavaScript function as shown below:
<script ...>
function xyz()
{
alert('Please Wait');
}
</script>
and in button declaration
<asp:button id='btn_submit' runat="server" OnClientClick="return xyz();" />

Categories