Setting Session Variable from UpdatePanel - asp.net

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);
}
}
}

Related

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.

UpdatePanel dynamically loaded Web UserControl will disappear after clicking any button inside the UserControl

I've a ASP.Net page (Default.aspx) which will load UserControl (ucontrol.ascx) dynamically into an UpdatePanel. By using UpdatePanel, we believe we're able to prevent the entire page to be posted-back.
My problem is, within the UserControl, we have some form controls, such as inputs and buttons; after the UserControl is loaded, clicking on any button inside the UserControl will cause the UpdatePanel to be blanked. During the investigation, I found that, breakpoints within the Button1_Click() in the code-behind for the UserControl is never reached. Please reference to the code below, this references to [REFERENCE]1. It's an urgent task, please kindly help.
Thanks!
William
Default.aspx:
<%# 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">
<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" EnablePartialRendering="true">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<span>This is hosting page</span><br />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Add UserControl" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Button1" />
</Triggers>
</asp:UpdatePanel>
<asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:PlaceHolder ID="PartHolder" runat="server">
</asp:PlaceHolder>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Button1" />
</Triggers>
</asp:UpdatePanel>
</form>
</body>
</html>
Default.aspx.cs:
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
loadparts();
}
private void loadparts(){
Control formpart = LoadControl("ucontrol.ascx");
formpart.ID = "formpart";
PartHolder.Controls.Add(formpart);
}
}
ucontrol.ascx:
<%# Control Language="C#" AutoEventWireup="true" CodeFile="ucontrol.ascx.cs" Inherits="ucontrol" %>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
This is Web User Control in UpdatePanel<br />
<br />
<br />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click"/>
ucontrol.ascx.cs:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class ucontrol : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = TextBox1.Text;
TextBox1.Text = "";
TextBox1.Focus();
}
}
Here's what's happening:
First page load. No user control on page.
Person clicks Button1 on the page.
Page posts back (update panels do a full postback behind the scenes) and your Button1_Click handler on the page is run, which loads the user control into the page.
User now sees the user control and clicks ITS Button1.
Page posts back, but this time the Button1_Click handler on the page is not run (different button was clicked, even though you named them the same). Which means that loadparts() is not called and the user control is NOT loaded into the control tree.
Your Button1_Click handler in the user control is not run because the user control is not around to see that the button was clicked.
What you need to do is ensure that the user control is loaded into the page's control tree, even when you click the user control's button. That's why Andrew suggested you put the loadparts() into the Page_Load event. I can see that doesn't fit with the flow of your page, so you'll have to find another way to load it at the correct times.
try putting loadparts() method inside
protected void Page_Load(object sender, EventArgs e)
{
if(!isPostBack)
{
loadparts();
}
}
This worked for me:
protected void Page_Load(object sender, EventArgs e)
{
if(isPostBack)
{
loadparts();
}
}

Slow UpdatePanel in ASP.NET application

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.

Resources