views:

283

answers:

1

Hi, I have a listing in an UpdatePanel
I have some filters for that list in the form a checkboxList control. The checkboxList is created dynamically on page load

During the Ajax update (postback), the checkbox list is not populated form the viewstate, so I cannot get the listing to filter.

Note: If I put the checkbox list items directly in the markup, it all works, just not if the listing is populated on-postback.

protected override void OnLoad(EventArgs e)
{
    if (!Page.IsPostBack)
    {
        foreach (var p in global.Product)
            CheckListManufacurer.Items.Add(new ListItem(p, p));
    }
    base.OnLoad(e);
}


<form id="ProductListForm" runat="server">
<asp:ScriptManager ID="ScriptManager" runat="server" EnablePartialRendering="true"></asp:ScriptManager>
  <asp:CheckBoxList ID="CheckListManufacurer" runat="server" EnableViewState="true">
     <asp:ListItem Value="" Text="(All)"></asp:ListItem>
  </asp:CheckBoxList>  
  <asp:Button id="btnTestAjax" runat="server" Text="Test" />  
  <asp:UpdatePanel ID="ProductsPanel" runat="server" UpdateMode="Conditional">
      <Triggers>
          <asp:AsyncPostBackTrigger ControlID="btnTestAjax"  />
          <asp:AsyncPostBackTrigger ControlID="CheckListManufacurer" />
      </Triggers>
      <ContentTemplate>
          <sr:ProductList ID="Products" runat="server" />
      </ContentTemplate>
  </asp:UpdatePanel>
</form>
+1  A: 

if you populate the list in Page_Load then you're too late.

in the asp.net page event lifecycle viewstate is populated just before Page_PreLoad (see here: http://msdn.microsoft.com/en-us/library/ms178472.aspx )

so if you want viewstate to be loaded into controls you dynamically add to the page you need to add them in Page_Init

AndreasKnudsen
Yup, that got it. Move the creation of the checkbox list into the OnInit. In this case, I had to move a bunch of other data load logic around that previous developer had piled into the prerender, but it solved it.
Rob