Microsoft KB Archive/309088

= PRB: Cannot Maintain ViewState on Mobile Controls That You Create Dynamically =

Article ID: 309088

Article Last Modified on 6/14/2002

-

APPLIES TO


 * Microsoft Mobile Internet Toolkit 1.0

-



This article was previously published under Q309088



SYMPTOMS
When you dynamically create mobile controls, the control may not render after you post back to the server.



CAUSE
The ASP.NET page framework creates statically declared controls for you automatically on every request. If you create and add controls dynamically in your code, you must create these controls in every request.



RESOLUTION
When you dynamically create mobile controls, you must re-create the control on each post back to the server.



STATUS
This behavior is by design.



MORE INFORMATION
Use the following code to reproduce this problem. This code sample dynamically creates a list control that does not render after you post back the page to the server. <%@ Page language=&quot;c#&quot; Inherits=&quot;System.Web.UI.MobileControls.MobilePage&quot; %> <%@ Register TagPrefix=&quot;mobile&quot; Namespace=&quot;System.Web.UI.MobileControls&quot; Assembly=&quot;System.Web.Mobile&quot; %>

private void Page_Load(object sender, System.EventArgs e) { if(!Page.IsPostBack) {     // Create the mobile list control. System.Web.UI.MobileControls.List myListControl = new System.Web.UI.MobileControls.List;

// Create items to add to the mobile list control. myListControl.Items.Add(&quot;John&quot;); myListControl.Items.Add(&quot;Sue&quot;); myListControl.Items.Add(&quot;Robert&quot;);

Form1.Controls.Add(myListControl); } }

   This problem occurs because the list control appears within an if-else statement that checks the IsPostBack property of the page. If the code determines that the page is not posted back, the list control is created. If the code determines that the page is posted back, the control is not re-created; therefore, the control does not render after the post back.

To resolve this problem, remove the if(!Page.IsPostBack) condition.

Additional query words: postback if...else

Keywords: kbwebforms kbprb kbservercontrols KB309088

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.