ChildControls state

Discussion in 'ASP .Net' started by mauricio, Mar 11, 2008.

  1. mauricio

    mauricio Guest

    Hi,

    Sorry for my english.

    I have a custom server control that derive from Panel.

    In my control I want to add an Checkbox, but after an postback it
    checkbox lost the state of checked property.

    My code:

    public class CheckboxPanel : System.Web.UI.WebControls.Panel,
    IPostBackDataHandler
    {
    private CheckBox _chk = new CheckBox();

    protected override void CreateChildControls()
    {
    _chk = new CheckBox();
    _chk.ID = this.ClientID + "_chk";
    }

    public override void RenderBeginTag(HtmlTextWriter writer)
    {
    base.RenderBeginTag(writer);

    writer.AddStyleAttribute(HtmlTextWriterStyle.Width,
    "100%");

    writer.RenderBeginTag(HtmlTextWriterTag.Table);

    writer.RenderBeginTag(HtmlTextWriterTag.Tr);

    writer.RenderBeginTag(HtmlTextWriterTag.Td);

    _chk.RenderControl(writer);

    writer.RenderEndTag(); // TD

    writer.RenderEndTag(); // TR

    writer.RenderBeginTag(HtmlTextWriterTag.Tr);

    writer.RenderBeginTag(HtmlTextWriterTag.Td);
    }

    public override void RenderEndTag(HtmlTextWriter writer)
    {
    writer.RenderEndTag();
    writer.RenderEndTag();
    writer.RenderEndTag();

    base.RenderEndTag(writer);
    }
    }
     
    mauricio, Mar 11, 2008
    #1
    1. Advertising

  2. mauricio

    bruce barker Guest

    the ceckbox you create is private variable, not a child of your control, so
    that your code is handling the event cycle for the checkbox. you need to
    handle the loadpostbackdata event also if you want postback data loaded. you
    can either fire the event, or load the postback yourself.

    the other approach is to make the checkbox a real child. create table (or
    generic) objects in CreateChildControls and add to your Controls collection,
    then add the checkbox to the proper child.

    -- bruce (sqlwork.com)


    "mauricio" wrote:

    > Hi,
    >
    > Sorry for my english.
    >
    > I have a custom server control that derive from Panel.
    >
    > In my control I want to add an Checkbox, but after an postback it
    > checkbox lost the state of checked property.
    >
    > My code:
    >
    > public class CheckboxPanel : System.Web.UI.WebControls.Panel,
    > IPostBackDataHandler
    > {
    > private CheckBox _chk = new CheckBox();
    >
    > protected override void CreateChildControls()
    > {
    > _chk = new CheckBox();
    > _chk.ID = this.ClientID + "_chk";
    > }
    >
    > public override void RenderBeginTag(HtmlTextWriter writer)
    > {
    > base.RenderBeginTag(writer);
    >
    > writer.AddStyleAttribute(HtmlTextWriterStyle.Width,
    > "100%");
    >
    > writer.RenderBeginTag(HtmlTextWriterTag.Table);
    >
    > writer.RenderBeginTag(HtmlTextWriterTag.Tr);
    >
    > writer.RenderBeginTag(HtmlTextWriterTag.Td);
    >
    > _chk.RenderControl(writer);
    >
    > writer.RenderEndTag(); // TD
    >
    > writer.RenderEndTag(); // TR
    >
    > writer.RenderBeginTag(HtmlTextWriterTag.Tr);
    >
    > writer.RenderBeginTag(HtmlTextWriterTag.Td);
    > }
    >
    > public override void RenderEndTag(HtmlTextWriter writer)
    > {
    > writer.RenderEndTag();
    > writer.RenderEndTag();
    > writer.RenderEndTag();
    >
    > base.RenderEndTag(writer);
    > }
    > }
    >
     
    bruce barker, Mar 11, 2008
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Graham

    ChildControls and ViewState

    Graham, Sep 6, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    581
    =?Utf-8?B?SnVsaWFuIEplbGZz?=
    Sep 6, 2005
  2. Richard Watt

    DataGrid childcontrols problem

    Richard Watt, Apr 23, 2004, in forum: ASP .Net Building Controls
    Replies:
    5
    Views:
    219
    Guest
    Apr 27, 2004
  3. Peter

    Rendered control vs childcontrols

    Peter, May 1, 2005, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    148
    Peter
    May 1, 2005
  4. Sandy

    ChildControls disappear after Clicking

    Sandy, Aug 12, 2005, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    128
    Sandy
    Aug 12, 2005
  5. Nico
    Replies:
    0
    Views:
    148
Loading...

Share This Page