CheckBox.Text not saved after PostBack

P

Peter Zolja

I've been fighting with this thing for quite a while now, though it should
be simple. For testing purposes I have a server control that stores a list
of checkboxes. In a testing ASPX page, in the Init event, I do the
following:

// "global" variables
private ListTest lt1;
private CheckBox c1;

private void WebForm1_Init(object sender, System.EventArgs e)
{
lt1 = new ListTest();

if (!IsPostBack)
{
c1 = new CheckBox();
c1.Text = "box1";

CheckBox c2 = new CheckBox();
c2.Text = "box2";

lt1.AddCheckBoxToList(c1);
lt1.AddCheckBoxToList(c2);

lt1.DataBind();
}
pnlList1.Controls.Add(lt1);
}

The ListTest class is in charge of storing a list of check boxes and
restoring them after a postback (the class body is listed below). This works
OK, except that after postback only the checked or unchecked state is
restored; the actual text associated with each checkbox is not. If I set the
text for a checkbox in Page_Load, it works as I expect:

private void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
c1.Text = "can u see me?";
}
}

My guess is that this is because the TrackingViewState phase starts after
the Initialization phase, so the initial values are not stored in ViewState.
How can I work around this, or I am missing or misunderstanding something?
Any hints or ideas are welcome.

Thanks you!
(sorry for the long post <g>)


Here's the code for the class:

public class ListTest : System.Web.UI.WebControls.WebControl,
INamingContainer
{
protected ArrayList _checkBoxList = null;

public ListTest()
{
_checkBoxList = new ArrayList();
}

protected override void CreateChildControls()
{
Controls.Clear();

if (ViewState["CC"] != null)
{
CreateControlHierarchy(true);
}
}

public override void DataBind()
{
base.OnDataBinding(EventArgs.Empty);
// clear the existing control hierarchy
Controls.Clear();

// clear any existing view state for the children because
// data binding creates a new hierarchy.
ClearChildViewState();

// start tracking changes made during the data-binding process
TrackViewState();

// re-create the new control hierarchy using the assigned data source
CreateControlHierarchy(false);

// set the flag to indicate that child controls have created so that
// CreateChildControls does not get called
ChildControlsCreated = true;
}

protected virtual void CreateControlHierarchy(bool useViewState)
{
if (useViewState)
{
BuildCheckBoxList((int)ViewState["CC"]);
AddCheckBoxListToControls();
}
else
{
AddCheckBoxListToControls();
ViewState["CC"] = CheckBoxList.Count; // CC = control
count
}
}

private void AddCheckBoxListToControls()
{
CheckBox box;

for (int i = 0; i < CheckBoxList.Count; i++)
{
box = (CheckBox)CheckBoxList;
Controls.Add(box);
}
}

public void AddCheckBoxToList(CheckBox box)
{
CheckBoxList.Add(box);
}

private void BuildCheckBoxList(int ItemCount)
{
CheckBoxList.Clear();

for (int i = 0; i < ItemCount; i++)
{
CheckBox box = new CheckBox();
AddCheckBoxToList(box);
}
}

protected override void Render(HtmlTextWriter writer)
{
AddAttributesToRender(writer);

foreach (WebControl control in Controls)
{
control.RenderControl(writer);
writer.Write("<br>");
}
}

public virtual ArrayList CheckBoxList
{
get
{
return _checkBoxList;
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,991
Messages
2,570,212
Members
46,800
Latest member
Tobi1987

Latest Threads

Top