ListControl added as child controls does remember selected items on postback

Last post 07-05-2009 4:38 AM by Abdulla.AbdelHaq. 1 replies.

Sort Posts:

  • ListControl added as child controls does remember selected items on postback

    07-04-2009, 12:35 PM
    • Member
      79 point Member
    • bhav27
    • Member since 06-07-2006, 4:10 PM
    • Posts 96

    Hi

    I am get very frustrated while added listcontrol inside my custom server control. Basically, these controls does not remember what items user selected when postback occurs.

    Just to test this I have created following code. Checkboxlist remembers its items collection but for some bizzare reason does not remember checked items when postback occurs. Please can someone provide me solution or direct me to correct resource;

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Linq;
    using System.Text;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace AspNetServerControls
    {
    	[ToolboxData("<{0}:TestingViewStates runat=server></{0}:TestingViewStates>")]
    	public class TestingViewStates : WebControl
    	{
    		protected override void CreateChildControls()
    		{
    			CheckBoxList chkList = new CheckBoxList();
    			chkList.EnableViewState = true;
    			Controls.Add(chkList);
    
    			if (chkList.Items.Count == 0)
    			{
    				List<string> items = new List<string>();
    				items.Add("London");
    				items.Add("New York");
    				items.Add("Paris");
    
    				chkList.DataSource = items;
    				chkList.DataBind();
    			}
    			
    			base.CreateChildControls();
    		}
    	}
    }
    


     

  • Re: ListControl added as child controls does remember selected items on postback

    07-05-2009, 4:38 AM
    Answer

    you need to add the items in the Render Method not inthe CreateChildControls.

    just move the listitems parts to be in the Render Method.

    Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
    
     End Sub



    Plz remember to click "Mark as Answer" if this helped you.

    Abdulla AbdelHaq    MCTS, MCPD

    - My Articles on ASP Alliance
    - My Weblogs
    - My Sessions on JorDev User Group

    "Experience is simply the name we give our mistakes"
Page 1 of 1 (2 items)