Slow UpdatePanel in ASP.NET application - asp.net

I was trying to figure out why my UpdatePanel is so SLOW (after Button1's postback) in an ASP.NET application:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="MyNamespace.WebForm1" %>
<%# Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="cc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<cc1:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server"/>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
</ContentTemplate>
</asp:UpdatePanel>
</form>
</body>
</html>
code behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace MyNamespace
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)//this is slow!!
{
Button btn = sender as Button;
btn.Text = DateTime.Now.ToString("hhmmssfff");
}
}
}
I was searching here in StackOverflow but I couldn't find a straight forward answer.
Anyone knows something that might speed this up?
I've seen a faster UpdatePanel button postback when I run my webapplication using http://Junior-PC. But when I use http://localhost is pretty slower.
The only difference between a regular web application and this one is in Web application properties-> Web tab-> Servers section where is marked Use Local IIS Web Server-> Project Url: http://localhost/ and Override application root URL.

If you're using Firefox, don't forget do disable IPv6 in (enter about:config in URL). Except that, code looks fine. What's the speed of regular postbacks? Tried other browsers?

Please try to use classic script manager:
asp:ScriptManager
instead of:
cc1:ToolkitScriptManager.
Morover please unregister AjaxControlToolkit because in this case it is unnecessary. Let us know it helps.

Related

How to solve the error Using the AjaxCallHelper write methods outside of an AjaxCall is not allowed

I've searched this error in all google results, but I can't find a solution.
I have this WebForm:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="LoTest.aspx.cs" Inherits="-.-.-.LoTest" %>
<%# Register TagPrefix="ajax" Namespace="MagicAjax.UI.Controls" Assembly="MagicAjax" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<ajax:AjaxPanel ID="ajaxLG" runat="server" AjaxCallConnection="Synchronous">
<asp:Button ID="btnTest" runat="server" CssClass="button" ToolTip="gin." Text="LogIn" OnClick="btnTest_Click"></asp:Button>
</ajax:AjaxPanel>
</div>
</form>
</body>
</html>
Code Behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MagicAjax.UI;
using MagicAjax;
namespace -.-.-
{
public partial class LoTest : AjaxPage
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnTest_Click(object sender, EventArgs e)
{
AjaxCallHelper.WriteAlert("Test msg");
}
}
}
The web.config:
<httpModules>
<add name="MagicAjaxModule" type="MagicAjax.MagicAjaxModule, MagicAjax"/>
</httpModules>
In the Line “AjaxCallHelper.WriteAlert("Test msg");” throws the error:
Using the AjaxCallHelper write methods outside of an AjaxCall is not allowed.
In IE, just add the site to the compatibility view and that´s all.
Seems like if AjaxMagix.dll works with older elements of the explorer.
In Google Chrome doesn´t work.

Controls of .aspx pages are not accessible in .aspx.cs page

I am converting my existing web site project to web application for that I have created a new web application and the copy the entire source code of web site project in web application project. But now the problem is when I am compiling the project .aspx page controls are not accessible in .aspx.cs page. I am doing this in VS 2012 with target framework 3.5.
Here is the code snippet
HTML Source
<%# Page Language="C#" AutoEventWireup="true" CodeFile="View_Deal_Rights.aspx.cs" Inherits="Modules_View_Deal_Rights"
EnableEventValidation="false" ValidateRequest="false" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>
<%=ConfigurationManager.AppSettings["SystemName"]%></title>
</head>
<body leftmargin="0" topmargin="10" marginwidth="0" marginheight="0" rightmargin="0"
bottommargin="0">
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" AsyncPostBackTimeout="0">
</asp:ScriptManager>
<asp:Label ID="lblok" runat="server"></asp:Label>
<asp:Button ID="btnSave" runat="server" CssClass="button" Text="Save Deal" ValidationGroup="LastSave"/>
</form>
</body>
</html>
Code Behind Source
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using UTOFrameWork.FrameworkClasses;
using System.Collections;
using System.Data;
using System.Web.UI.HtmlControls;
using System.Text;
public partial class Modules_View_Deal_Rights : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
btnSave. // Here Button save is not accessable
lblok. // Here lblok is not accessable
}
}
}
You need to change CodeFile to CodeBehind as you converted your application from web site to web application.
CodeBehind="View_Deal_Rights.aspx.cs"
Once you change then build and you will be able to access.
For more details see following question.
CodeFile vs CodeBehind

(ASP.NET) Button event not firing the first 30 seconds

I'm having a weird issue where a button's click event is not being fired for a while (around 20-30 seconds) and then starts to work as normal. Refresh the page and I have to wait for it to work again.
Please note this: It works when run in debug on the localhost, the behavior described is only found on the server. I'm starting to suspect a server-side issue, but I don't have access to it and would need to pin-point what might be causing this to the people in charge of it (if it's indeed a server-side issue).
Any help to locate the issue is much appreciated! I stripped the page down to the most basics and this is what I have.
ASP:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd (http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd)">
<html xmlns="http://www.w3.org/1999/xhtml (http://www.w3.org/1999/xhtml)">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
</div>
</form>
</body>
</html>
Code-behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("http://www.google.se/ (http://www.google.se/)");
}
}
Config:
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0"/>
</system.web>
</configuration>

Why do I get the result zero when I try to get the width of a DropDownList control in asp.net?

After I click button1, it display 0, why? How can get correct width of a DropDownList control? Thanks!
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="Button" />
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem>Item 1</asp:ListItem>
<asp:ListItem>Item 2</asp:ListItem>
</asp:DropDownList>
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Button1.Text = DropDownList1.Width.Value.ToString();
}
}
It's because the value Width is not set. In html, when the width value is not set, you let the browser choose the size.
Moreover, the asp.net engine only host properties to be able to render the correct Html. If you look at the code of the textbox, you will see that if you put a witdh, it will produces a style="width:yourvalue". ASP.net runs on the server side, so it do not have any idea of the actual size of the control.
If you actually require the client size of the dropdownlist, either specify it explicitly, or use some custom javascript to calculate the actual size of the control, then to pass the calculted size to an hidden field. In the code behind you be able to get the value from the Page.Request.Form collection.
Please clarify your need if you need further explanation.

Setting Session Variable from UpdatePanel

I am using ASP.NET 2.0 AJAX Extensions 1.0 with the version v1.0.20229 of the AJAX Control Toolkit (which to my knowledge is the latest for .NET 2.0/Visual Studio 2005).
My web page (aspx) has a DropDownList control on an UpdatePanel. In the handler for the DropDownList's SelectedIndexChanged event I attempt to set a session variable.
The first time the event is fired, I get a Sys.WebForms.PageRequestManagerParserErrorException: "The message received from the server could not be parsed". If I continue, subsequent SelectedIndexChanged's are handled successfully.
I have stumbled upon a solution whereby if I initialise the session variable in my Page_Load (so the event handler is just setting the value of a session variable that already exists as opposed to creating a new one) the problem goes away.
I'm happy to do this, but I'm curious as to exactly what the underlying cause is. Can anyone explain?
(My suspicion is that setting the session variable receives a response from the server which is then returned to the 'caller', but it's not the sort of response it knows how to deal with causing the exception?)
EDIT: I reproduced the problem in a seperate little project:
Default.aspx
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="SessionTest._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<div>
<asp:UpdatePanel id="upCategorySelector" runat="server">
<ContentTemplate>
Category:
<asp:DropDownList ID="ddlCategory" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlCategories_SelectedIndexChanged">
<asp:ListItem>Item 1</asp:ListItem>
<asp:ListItem>Item 2</asp:ListItem>
<asp:ListItem>Item 3</asp:ListItem>
</asp:DropDownList>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
namespace SessionTest
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// If I do this, the exception does not occur.
if (Session["key"] == null)
Session.Add("key", 0);
}
protected void ddlCategories_SelectedIndexChanged(object sender, EventArgs e)
{
// If Session["key"] has not been created, setting it from
// the async call causes the excaption
Session.Add("key", ((DropDownList)sender).SelectedValue);
}
}
}

Resources