asp.net
c#
Our webpage currently contains a rather large web app which causes a lengthy delay when attemping to navigate to it. I'm currently implementing a WCF web service to utilize ajax but the delay is a concern to my employer so he wanted a quick and dirty fix in the mean time.
I would like to have the empty page load and then use a timer to load the content. This will cut down on perceived page load time but i'm unsure how to accomplish it.
Any help would be much appreciated
Shawn
Some code to get you started:
In the asp.net page:
<asp:Timer ID="Timer1" OnTick="Timer1_Tick" runat="server" Interval="1">
</asp:Timer>
<asp:UpdatePanel ID="updatePanel1" runat="server" UpdateMode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
</Triggers>
<ContentTemplate>
.... your stuff here
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdateProgress ID="UpdateProgress1" runat="server" DisplayAfter="100">
<ProgressTemplate>
Please wait...
</ProgressTemplate>
</asp:UpdateProgress>
In the code behind:
protected void Timer1_Tick(object sender, EventArgs e)
{
this.Timer1.Enabled = false;
StartLongRunningTask();
}
Instead of a timer, you could flush the Response with Response.Flush().
Related
I have Used Asp UpdatePanel On Master Page To stop postback. But now it's Create problem on other pages, such as data fetching in gridview on button click.
<asp:UpdatePanel runat="server" ID="updatePanle1">
<ContentTemplate>
<asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</ContentTemplate>
</asp:UpdatePanel>
try this code.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GridviewBind();
}
}
<asp:UpdatePanel ID="updatepnl" runat="server">
<ContentTemplate>
---Put Your Grid View Code---
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="btnSubmit" />
</Triggers>
</asp:UpdatePanel>
You could try adding Triggers in your UpdatePanel desgin code here to stop the other events causing postback including button clicks and others. Try something like this :
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Button1" EventName="clickEvent" />
</Triggers>
You can also try using PostBackTrigger instead of AsyncPostBackTrigger here incase if it doesnt work out as expected.
<Triggers>
<asp:PostBackTrigger ControlID="Button1" EventName="clickEvent" />
</Triggers>
Hope this helps.
We don't normally use the update panel just to prevent the whole asp.net site from re-loading page / posting back. Client controls (jquery) would still be the best fit. But if you're really curious you might want to grind time learning the lessons in this MSDN link https://msdn.microsoft.com/en-us/library/bb398864%28v=vs.140%29.aspx
I use the below code to auto refresh the page every 60 seconds via the AJAX tools in VS2010. Works perfectly.
<asp:MultiView ID="MultiView1" runat="server">
<asp:View ID="View1" runat="server">
<asp:UpdatePanel ID="UpdatePanel1" runat="server" ViewStateMode="Enabled" UpdateMode="Conditional">
<ContentTemplate>
ASP.NET/HTML Code
<p>
<asp:Button ID="Button2" runat="server" Text="Click here" OnClick="Button2_Click" /> to disable the pages automatic refresh.</p>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
</Triggers>
</asp:UpdatePanel>
<asp:Timer ID="Timer1" runat="server" Interval="60000">
</asp:Timer>
</asp:View>
<asp:View ID="View2" runat="server">
etc.
</asp:MultiView>
I want to include a button on the asp.net page to cancel the auto refresh.
I tried to include the below but when I clicked the button, it didn't work. The below is the Code Behind for an OnClick event for a Button. The asp.net code is in the above code.
protected void Button2_Click(object sender, EventArgs e)
{
Timer1.Interval = 0;
}
Where am I going wrong? Is this even a way to do this or do I need to go another route in order to allow the user to cancel the auto page refresh?
Thanks to PeterJ I have found the solution. I modified the code and since I clicked it the page has not refreshed. The issue was with my code behind for the button OnClick event. I had:
Timer1.Interval = 0;
When I should have had:
Timer1.Enabled = false;
I would like to set the Timer Interval to be 5000-10000 ms randomly using aspx only (without the C# code behind).
The following example raises the error message:
Cannot create an object of type 'System.Int32' from its string representation '<%= Random.Next(5000,10000) %>' for the 'Interval' property.
How do I correct the Interval variable to get a random interval calls?
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:Timer ID="Timer1" runat="server" Interval="<%= Random.Next(5000,10000) %>">
</asp:Timer>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
</Triggers>
<ContentTemplate>
<%= DateTime.Now.ToString() %>
</ContentTemplate>
</asp:UpdatePanel>
I tried to Convert.ToInt32, but it doesn't work.
Is it even possible?
For some inspiration, I managed to work this out with some C# code behind this way:
aspx :
<asp:Timer ID="Timer1" runat="server" Interval="5000" OnTick="Timer1_Tick">
C# :
protected void Timer1_Tick(object sender, EventArgs e)
{
Random random = new Random();
Timer1.Interval = random.Next(5000, 10000);
}
but again, I would like to get rid of the C# code.
I hope it helps.
The <%= %> tag will always return a string, as it's an equivalent to Response.Write(" ").
Your alternative is code-behind (offering a good explanation of why you don't want to use code-behind might get some more interest from potential respondents). Or, use the <%# %> tag instead. Although, this will require the control be data-bound, which is probably an even less elegant solution than just using the code-behind.
I have an asp:UpdatePanel with an asp:Timer. These are in a Master/Content Page. The code is below:
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Timer ID="Timer1" runat="server" Interval="5000" OnTick="Timer1_Tick"></asp:Timer>
</ContentTemplate>
</asp:UpdatePanel>
But when the timer fires, I get the folowing error:
Microsoft JScript runtime error: Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed. Common causes for this error are when the response is modified by calls to Response.Write(), response filters, HttpModules, or server trace is enabled.
Details: Error parsing near '
This works in a standalone web form, but not in a content page with a master page.
Can anyone explain a fix for this?
Thanks in advance for any help!!
Is there a specifc reason why you have the Timer control in the UpdatePanel?
Every time I have needed to use a Timer control to cause an UpdatePanel refresh, I have set it up like the following and it works fine with MasterPages:
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
</Triggers>
<ContentTemplate>
<!-- your content here, no timer -->
</ContentTemplate>
</asp:UpdatePanel>
<asp:Timer ID="Timer1" runat="server" Interval="5000" OnTick="Timer1_Tick">
</asp:Timer>
Use the Trigger to cause the UpdatePanel to refresh from the Tick event. You only want to embed content in your UpdatePanel if possible.
I use UpdatePanel with DataList element inside. I want to update the contents from DB every 10 secunds. I noticed that updating occures only after the postback. I did the code like
<asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
</Triggers>
<ContentTemplate>
<asp:DataList ID="lstComputers" runat="server" DataKeyField="ComputerID" DataSourceID="ComputersDataSource"
OnItemDataBound="lstComputers_ItemDataBound" OnItemCommand="lstComputers_ItemCommand">
<HeaderTemplate>
// Header data
</HeaderTemplate>
<ItemTemplate>
// Item template
</ItemTemplate>
</asp:DataList>
<asp:UpdateProgress ID="UpdateProgress2" runat="server">
<ProgressTemplate>
<img border="0" src="images/loading.gif" />
</ProgressTemplate>
</asp:UpdateProgress>
</ContentTemplate>
</asp:UpdatePanel>
In code behind i tryed to use RaisePostBackEvent method but got Stack overflow exception...
protected void Timer1_Tick(object sender, EventArgs e)
{
this.RaisePostBackEvent(Timer1, "");
}
Remember that all your code-behind is executed on the server only. Therefore, if the Timer1_Tick() method is running, then your Timer is raising a PostBack.
The reason you get a StackOverflowException running that method is because it simply calls itself, infinitely. You need to place your update code in that method, not call it again recursively.
Take a look at the setTimeout() and setInterval() javascript functions. This all needs to happen on the client, not the server side.
protected void Timer1_Tick(object sender, EventArgs e)
{
lstComputers.DataBind();
}
Solved the problem with data reloading