Showing posts with label browser. Show all posts
Showing posts with label browser. Show all posts

Wednesday, March 28, 2012

AJAX UpdatePanel Refresh != Browser Refresh

Hey everyone. I've got a problem, I have a page that I want the data and buttons to refresh every 30 seconds. When I click the button, the data is recorded into a database and the button(s) are disabled until the next interval comes up. An interval is 5 mins even though the data will refresh every 30 seconds. That means that when the button is pushed, it should be disabled until the 5 min interval is up.

The problem I'm having is that even though my whole page is in the Update Panel, my buttons DO NOT re-enable after that 5 min interval, but my data labels DO. I have an indicator that changes at each 5 min interval and it is working, but my buttons are still disabled. I can refresh the page and the buttons are enabled...another thing is that after I press the button and it's disabled, I can refresh the page and the buttons still stay disabled as thy should until the next 5 min interval.

Is the entire Page_Load called on the AJAX Update Panel refresh? It doesn't seem like it since my code that enables/disables my buttons is not being called. Any ideas?

What is the full page refresh doing that the AJAX Update panel is not? I would just do a normal page refresh, but that looks bad and I'm also using the collapsable panels and the normal page refresh call would collapse all the panels. That would make the use of them, well, useless.

try call the update function of the updatepanel

<asp:UpdatePanel ID="Test" ...

code behind ... Test.Update()

if not then use javascript to set the buttons back to disabled = false;


The data refreshes fine still, but the buttons don't "reset."

Baseically, I have a function that is called for each button that looks in the database to see if a log has been made for this record. If there is not a log, the button is enabled, if there is a log, the button is disabled until the next interval. For some reason, my functions that check my buttons are not being called when the update panel is updated each 30 seconds. The functionality when the button is clicked works fine, but the buttons never enable again unless I hit the refresh in the browser to refresh the entire page.

I hope someone else has some suggestions as to what might be going on. Thanks!


Ok, here's the answer of why my "AJAX UpdatePanel Refresh != Browser Refresh"...

In my old version of the page, I have the default value of my buttons set to enabled. Each time the page refreshes, the default value is seen. In the AJAX update panel, the default value is not seen and therefore my buttons were not resetting to the default. My functions were assuming these would be enabled unless the log was found.

I hope this helps someone that might have the same problems in the future.

Ajax Update Panel & Opening a new browser window

Please excuse my terminolgy, I am not used to discussing in detail Embarrassed

I have a fairly complicated page, containing various asp controls. One of which is a dynamically populated datagrid. Rows on this datagrid can be clicked and then within the SelectedIndexChanged Event on the grid I am working out several values and passing them as query strings into a URL and am then using response.redirect to open the URL as a new page with the parameters available to it. This works fine.

Response.Redirect("'MyPage.aspx?Sdate=" & sdate & "&Edate=" & edate & "&AreaCode=" & AreaCode & "&Qsdate=" & Qsdate & "&Qedate=" & Qedate & "")

The problem is that the whole page is wrapped in an Ajax Update Panel, for various reasons, and I now need to open the new page (after clicking a row on the datagrid) in a new browser window.

The usual method I think would be -

Response.Write("<script>window.open 'MyPage.aspx?Sdate=" & sdate & "&Edate=" & edate & "&AreaCode=" & AreaCode & "&Qsdate=" & Qsdate & "&Qedate=" & Qedate& "', '_new','width=400, height=200');</script>")

BUT... I gather Response.Write can't be used with Ajax and when I have tryed it, I just aget a parsing error. Can anyone suggest how I can open a new browser window from the SelectedIndexChanged Event, whilst still keeping the datagrid within the Ajax update panel.

You can add a label, and make it initially invisible.// Label1.Visible =false;

Assign the Text property of that label, the java script you want to execute.

And on the SelectedIndexChanged event set the visibility of the Label to true and text to the javascript.

Label1.Text = "<script>window.open 'MyPage.aspx?Sdate=" & sdate &"&Edate=" & edate & "&AreaCode=" & AreaCode &"&Qsdate=" & Qsdate & "&Qedate=" & Qedate& "','_new','width=400, height=200');</script>";

Label1.Visible = true;

But, basically, you could do all of that on the client side. I see no reason why you make a post back, if you just append some values from the client side and concatenate them.


That still won't work with Ajax, I think that might be due to the way the event fires on the datagrid? It is making the lable visible and setting the text to that of the javascript when I debug, but once the page is loaded it just highlights the row selected and nothing else, the label is not even visible.

I don't think this method is right for what I am trying to achieve anyway, as there are variable values in my VB Code behind that need to be passed to the URL.Therefore how can I actually pass values held in vb.net variables into the javascript code?

i.e. - "<script>window.open 'MyPage.aspx?Sdate=" &sdate& "'

sdate is a variable that is used in my vb.net code that holds a date and I need that to be in the URL that is used in the javascript, same as all the other variables above - edate, areacode, etc.

Any other suggestions?


*Bump*


jaYKay:

Response.Write("<script>window.open 'MyPage.aspx?Sdate=" & sdate & "&Edate=" & edate & "&AreaCode=" & AreaCode & "&Qsdate=" & Qsdate & "&Qedate=" & Qedate& "', '_new','width=400, height=200');</script>")

Check what isRegisterStartupScript(Page, Type, String, String, Boolean) for a server side solution, or seeASP.NET AJAX Client Life-Cycle Events. if you need a client side solution.


Did you ever find a solution to this problem? I have the exact same situation and have spent the last 2 days trying to find a way to open a new browser window with dynamic values.

If you or anyone else has a solution to ths problem, could you please post it?

Thanks,

Ken


Hi, I've just coded a way of doing that..it's really ugly one, but this is what I can think of at this moment :)

Here is the web page code:

<%@. Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
<script language="javascript" type="text/javascript">
<!--

function PopUp()
{
var label = document.getElementById('Label1');
if(label!=null)
{
window.open(label.innerText);
}
}

//-->
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
<asp:Label ID="Label1" runat="server" visible="false" style="visibility:hidden" />
</ContentTemplate>
</asp:UpdatePanel>
</form>
<script language="JavaScript" type="text/javascript">
var prm = Sys.WebForms.PageRequestManager.getInstance();

prm.add_endRequest(EndRequest);

function EndRequest(sender, args) {
PopUp();
}
</script>
</body>
</html>

and the code behind (in c#):

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = "http://www.asp.net";
Label1.Visible = true;
}
}

Hope this will help.

Cheers,

Yani


I was not able to get this variation to work, however on another thread I did get an answer that does work and is much cleaner.

Try this

http://forums.asp.net/p/1114996/1730089.aspx#1730089

Thank you very much for your reply.

Ken


I ended up cheating it really. I just used two panels on top of each other and made the first one and its contents disappear and the second one reappear, therefore giving the illusion that it was a new page, only problem now is that the information for the user is at the top of the second panel and the grid they clicked on is at the bottom of the first so they always have to scroll up to the top of the page each time. It works, if not a bit messy, I just need to set the focus to the top of the page, which is not that simple as the page is not refreshing as such and contains data that was parametised by the user.


Hi,

Have you looked at this thread http://forums.asp.net/t/1122462.aspx . I have posted a solution for similar problem in this thread. I hope this should solve your problem instead of doing it the above way. If you have any queries let us know.