Home All Groups Group Topic Archive Search About

Dynamic checkbox loses checked state

Author
13 Feb 2006 9:39 PM
Oxns
Hi,

Further to my earlier post - I have created a very simple test app which
demonstrates this problem. Any advice gratefully accepted ;-).

In a simple website (I am using .Net V2.0.50727.42) add the following to the
default.aspx page :

<asp:CheckBox ID="CheckBox1" runat="server" />

<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

<hr />

<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>

<hr />

<asp:Button ID="Button1" runat="server" Text="Button"
OnClick="Button1_Click" />



Now in Page_Load add :

Label1.Text = CheckBox1.Checked.ToString();

CheckBox ck = new CheckBox();

ck.Checked = CheckBox1.Checked;

PlaceHolder1.Controls.Add(ck);



So - the question is WHY does the dynamic checkbox not get checked ???.

Pressing the button should transfer the CheckBox1 checked status to the
dynamic control ???. The label is just for confirmation.

Thanks

Graham

Author
13 Feb 2006 10:12 PM
Phillip Williams
Hi Graham,

To understand the page lifecycle's tidbit that you ran into; move the
following 2 lines from the page_load to the page_init and see what happens:

CheckBox ck = new CheckBox();
PlaceHolder1.Controls.Add(ck);

After doing that look at this demo to experience another effect of creating
controls dynamically:
http://www.societopia.net/Samples/DynamicallyCreatedControls.aspx

Then search this newsgroup for previous threads in which we discussed the
page lifecycle and dynamically created controls.

Show quoteHide quote
"Oxns" wrote:

> Hi,
>
> Further to my earlier post - I have created a very simple test app which
> demonstrates this problem. Any advice gratefully accepted ;-).
>
> In a simple website (I am using .Net V2.0.50727.42) add the following to the
> default.aspx page :
>
> <asp:CheckBox ID="CheckBox1" runat="server" />
>
> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
>
> <hr />
>
> <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
>
> <hr />
>
> <asp:Button ID="Button1" runat="server" Text="Button"
> OnClick="Button1_Click" />
>
>
>
> Now in Page_Load add :
>
> Label1.Text = CheckBox1.Checked.ToString();
>
> CheckBox ck = new CheckBox();
>
> ck.Checked = CheckBox1.Checked;
>
> PlaceHolder1.Controls.Add(ck);
>
>
>
> So - the question is WHY does the dynamic checkbox not get checked ???.
>
> Pressing the button should transfer the CheckBox1 checked status to the
> dynamic control ???. The label is just for confirmation.
>
> Thanks
>
> Graham
>
>
>
>
>
Author
14 Feb 2006 8:49 AM
Oxns
Philip,

Many thanks for the excellent response :-)). Made the change suggested but
made ck globally available so that I could also change it in Page_Load and
its fine :-)).

Started to review the other stuff but not finished yet.

The 'annoying factor' is that almost everything else works as expected with
the code in Page_Load :-O, textboxes, labels, dropdowns etc. :-O.

Many Thanks

Regards

Graham

Show quoteHide quote
"Phillip Williams" <WEBSWAPP@newsgroups.nospam> wrote in message
news:128BCE5D-3EC5-4430-9CFC-3574BD5C27B1@microsoft.com...
> Hi Graham,
>
> To understand the page lifecycle's tidbit that you ran into; move the
> following 2 lines from the page_load to the page_init and see what
> happens:
>
> CheckBox ck = new CheckBox();
> PlaceHolder1.Controls.Add(ck);
>
> After doing that look at this demo to experience another effect of
> creating
> controls dynamically:
> http://www.societopia.net/Samples/DynamicallyCreatedControls.aspx
>
> Then search this newsgroup for previous threads in which we discussed the
> page lifecycle and dynamically created controls.
>
> --
> HTH,
> Phillip Williams
> http://www.societopia.net
> http://www.webswapp.com
>
>
> "Oxns" wrote:
>
>> Hi,
>>
>> Further to my earlier post - I have created a very simple test app which
>> demonstrates this problem. Any advice gratefully accepted ;-).
>>
>> In a simple website (I am using .Net V2.0.50727.42) add the following to
>> the
>> default.aspx page :
>>
>> <asp:CheckBox ID="CheckBox1" runat="server" />
>>
>> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
>>
>> <hr />
>>
>> <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
>>
>> <hr />
>>
>> <asp:Button ID="Button1" runat="server" Text="Button"
>> OnClick="Button1_Click" />
>>
>>
>>
>> Now in Page_Load add :
>>
>> Label1.Text = CheckBox1.Checked.ToString();
>>
>> CheckBox ck = new CheckBox();
>>
>> ck.Checked = CheckBox1.Checked;
>>
>> PlaceHolder1.Controls.Add(ck);
>>
>>
>>
>> So - the question is WHY does the dynamic checkbox not get checked ???.
>>
>> Pressing the button should transfer the CheckBox1 checked status to the
>> dynamic control ???. The label is just for confirmation.
>>
>> Thanks
>>
>> Graham
>>
>>
>>
>>
>>
Author
14 Feb 2006 1:17 PM
Phillip Williams
You are welcome, Graham.
Show quoteHide quote
"Oxns" wrote:

> Philip,
>
> Many thanks for the excellent response :-)). Made the change suggested but
> made ck globally available so that I could also change it in Page_Load and
> its fine :-)).
>
> Started to review the other stuff but not finished yet.
>
> The 'annoying factor' is that almost everything else works as expected with
> the code in Page_Load :-O, textboxes, labels, dropdowns etc. :-O.
>
> Many Thanks
>
> Regards
>
> Graham
>
> "Phillip Williams" <WEBSWAPP@newsgroups.nospam> wrote in message
> news:128BCE5D-3EC5-4430-9CFC-3574BD5C27B1@microsoft.com...
> > Hi Graham,
> >
> > To understand the page lifecycle's tidbit that you ran into; move the
> > following 2 lines from the page_load to the page_init and see what
> > happens:
> >
> > CheckBox ck = new CheckBox();
> > PlaceHolder1.Controls.Add(ck);
> >
> > After doing that look at this demo to experience another effect of
> > creating
> > controls dynamically:
> > http://www.societopia.net/Samples/DynamicallyCreatedControls.aspx
> >
> > Then search this newsgroup for previous threads in which we discussed the
> > page lifecycle and dynamically created controls.
> >
> > --
> > HTH,
> > Phillip Williams
> > http://www.societopia.net
> > http://www.webswapp.com
> >
> >
> > "Oxns" wrote:
> >
> >> Hi,
> >>
> >> Further to my earlier post - I have created a very simple test app which
> >> demonstrates this problem. Any advice gratefully accepted ;-).
> >>
> >> In a simple website (I am using .Net V2.0.50727.42) add the following to
> >> the
> >> default.aspx page :
> >>
> >> <asp:CheckBox ID="CheckBox1" runat="server" />
> >>
> >> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
> >>
> >> <hr />
> >>
> >> <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
> >>
> >> <hr />
> >>
> >> <asp:Button ID="Button1" runat="server" Text="Button"
> >> OnClick="Button1_Click" />
> >>
> >>
> >>
> >> Now in Page_Load add :
> >>
> >> Label1.Text = CheckBox1.Checked.ToString();
> >>
> >> CheckBox ck = new CheckBox();
> >>
> >> ck.Checked = CheckBox1.Checked;
> >>
> >> PlaceHolder1.Controls.Add(ck);
> >>
> >>
> >>
> >> So - the question is WHY does the dynamic checkbox not get checked ???.
> >>
> >> Pressing the button should transfer the CheckBox1 checked status to the
> >> dynamic control ???. The label is just for confirmation.
> >>
> >> Thanks
> >>
> >> Graham
> >>
> >>
> >>
> >>
> >>
>
>
>