Microsoft KB Archive/822317

= PRB: AutoPostBack for the DropDownList Control May Not Work as Expected When You Use Caching and VaryByControl Is Set to DropDownList =

Article ID: 822317

Article Last Modified on 11/26/2007

-

APPLIES TO


 * Microsoft ASP.NET 1.1
 * Microsoft ASP.NET 1.0

-



SYMPTOMS
When you set the VaryByControl attribute of the @ OutputCache directive to DropDownList in an ASP.NET page, the AutoPostBack property of the DropDownList control may not work as expected.



CAUSE
You can use the @ OutputCache directive attribute VaryByControl to cache multiple versions of user control output, based on a GET query string or on form POST parameters. However, after the user control is cached, the __doFormPost method is not generated for postback.

Registration of the postbackscript occurs on the OnPreRender method. However, the OnPreRender method of the cached control is not called when you cache the user control. Therefore, when the user control is cached, the __doFormPost method is not generated.



WORKAROUND
To work around this problem, call the GetPostBackEventReference method in the Page_Load event of an ASPX page. To work around the problem that is discussed in the &quot;More Information&quot; section, modify the code of the Page_Load event as follows:

Visual C# .NET Sample Code
public void Page_Load {    DateTime Created = DateTime.Now; CreatedStamp.InnerHtml = Created.ToString(&quot;r&quot;); GetPostBackEventReference(this); }

Visual Basic .NET Sample Code
Public Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim Created As DateTime Created = DateTime.Now CreatedStamp.InnerHtml = Created.ToString(&quot;s&quot;) GetPostBackEventReference(Me) End Sub



STATUS
This behavior is by design.



Steps to Reproduce the Behavior
 Start Microsoft Visual Studio .NET. Use Microsoft Visual Basic .NET or Microsoft Visual C# .NET to create a new ASP.NET Web Application project. By default, WebForm1.aspx is created. In Design view, right-click WebForm1, and then click View HTML Source.  Replace the existing code with the following sample code to add DropDownList and user control registration.

Visual C# .NET Sample Code <%@ Register TagPrefix=&quot;Fragment&quot; TagName=&quot;Simple&quot; Src=&quot;fragmentCache_byControl.ascx&quot; %> <%@ Page %>    public void Page_Load {    DateTime Created = DateTime.Now; CreatedStamp.InnerHtml = Created.ToString(&quot;r&quot;); // Uncomment the following line to work around the problem: // GetPostBackEventReference(this); } </Script> </HEAD> <body MS_POSITIONING=&quot;GridLayout&quot;> <FONT size=&quot;6&quot;>Fragment Cache VaryByControl example:</FONT> <HR SIZE=&quot;1&quot;> <form id=&quot;MyContainer&quot; method=&quot;post&quot; runat=&quot;server&quot;> <FRAGMENT:SIMPLE id=&quot;UserCon1&quot; runat=&quot;server&quot;></FRAGMENT:SIMPLE> <HR size=&quot;1&quot;> <Font size=&quot;6&quot;>This page was created at         <Font color=&quot;red&quot;><B id=&quot;CreatedStamp&quot; runat=&quot;server&quot;> </B></Font> </Font> </HTML> Visual Basic .NET Sample Code <%@ Page %> <%@ Register TagPrefix=&quot;Fragment&quot; TagName=&quot;Simple&quot; Src=&quot;fragmentCache_byControl.ascx&quot; %> <HTML> <HEAD> <Script Language=&quot;vb&quot; runat=&quot;server&quot;> Public Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim Created As DateTime Created = DateTime.Now CreatedStamp.InnerHtml = Created.ToString(&quot;s&quot;) ' Uncomment the following line to work around the problem: ' GetPostBackEventReference(Me) End Sub </Script> </HEAD> <body MS_POSITIONING=&quot;GridLayout&quot;> <Font size=&quot;6&quot;>Fragment Cache VaryByControl example:</Font> <HR size=&quot;1&quot;> <form id=&quot;MyContainer&quot; method=&quot;post&quot; runat=&quot;server&quot;> <Fragment:simple id=&quot;UserCon1&quot; runat=&quot;server&quot; /> <HR size=&quot;1&quot;> <Font size=&quot;6&quot;>This page was created at <Font color=&quot;red&quot;> <B id=&quot;CreatedStamp&quot; runat=&quot;server&quot;></B></Font> </Font> </HTML> </li> In Solution Explorer, right-click your project name, point to Add, and then click Add New Item.</li> Click Web User Control under Templates, and then name the control FragmentCache_byControl.ascx .</li> In Design view, right-click WebForm1, and then click View HTML Source.</li>  Replace the existing code with the following sample code:

Visual C# .NET Sample Code <%@ OutputCache Duration=&quot;20&quot; varybyparam=&quot;none&quot; VaryByControl=&quot;Category&quot;%> <%@ Control Language=&quot;C#&quot; AutoEventWireup=&quot;true&quot; %> <Script runat=&quot;server&quot;> public void Page_Load { // Get the Date and Time. This should not change after the first run. DateTime NowTime = DateTime.Now; DateTime Expires = NowTime.AddSeconds(60); CreatedStamp.InnerHtml = NowTime.ToString(&quot;r&quot;); ExpiresStamp.InnerHtml = Expires.ToString(&quot;r&quot;); } </Script> <BR> Category: <asp:DropDownList id=&quot;Category&quot; AutoPostBack=&quot;True&quot; runat=&quot;server&quot;> <asp:ListItem></asp:ListItem> <asp:ListItem Value=&quot;business&quot;>psychology</asp:ListItem> <asp:ListItem Value=&quot;psychology&quot;>business</asp:ListItem> <asp:ListItem Value=&quot;literature&quot;>literature</asp:ListItem> </asp:DropDownList>

<Font size=&quot;3&quot;><B id=&quot;CategoryItem&quot; runat=&quot;server&quot;></B> <P> Fragment Cache created: <Font color=&quot;red&quot;><B id=&quot;CreatedStamp&quot; runat=&quot;server&quot;></B></Font> <BR> Fragment Cache expires: <Font color=&quot;red&quot;><B id=&quot;ExpiresStamp&quot; runat=&quot;server&quot;></B></Font> </P> </Font> Visual Basic .NET Sample Code <%@ Control Language=&quot;vb&quot; AutoEventWireup=&quot;true&quot; %> <%@ OutputCache Duration=&quot;20&quot; varybyparam=&quot;none&quot; VaryByControl=&quot;Category&quot;%> <Script runat=&quot;server&quot;> Public Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim NowTime As DateTime Dim Expires As DateTime NowTime = DateTime.Now Expires = NowTime.AddSeconds(60) Dim Created As DateTime CreatedStamp.InnerHtml = Created.Now.ToString(&quot;s&quot;) ExpiresStamp.InnerHtml = Expires.ToString(&quot;s&quot;) End Sub </Script> <BR> Category: <asp:DropDownList id=&quot;Category&quot; AutoPostBack=&quot;True&quot; runat=&quot;server&quot;> <asp:ListItem></asp:ListItem> <asp:ListItem Value=&quot;business&quot;>psychology</asp:ListItem> <asp:ListItem Value=&quot;psychology&quot;>business</asp:ListItem> <asp:ListItem Value=&quot;literature&quot;>literature</asp:ListItem> </asp:DropDownList> <Font size=&quot;3&quot;><B id=&quot;CategoryItem&quot; runat=&quot;server&quot;></B> <P> Fragment Cache created: <Font color=&quot;red&quot;><B id=&quot;CreatedStamp&quot; runat=&quot;server&quot;></B></Font> <BR> Fragment Cache expires: <Font color=&quot;red&quot;><B id=&quot;ExpiresStamp&quot; runat=&quot;server&quot;></B></Font> </P> </Font> </li> On the Debug menu, click Start to save and then run the project.</li> Click the first item in the drop-down list box. Notice that a new fragment cache time stamp is created.</li> Click the second item in the drop-down list box. Notice that a different fragment cache time stamp is created.</li> Repeat steps 9 and 10.</li> Notice that the time stamps do not change, regardless of the item that you select in the drop-down list box.</li></ol>

<div class="references_section">