Dymanic Checkbox problems (Web App)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tomas Vera

    Dymanic Checkbox problems (Web App)

    Hello All,
    I'm having problems creating a page with dynamic checkboxes in a WebApp.

    In my app, I need to query a database, then (based on results) add
    checkboxes to my form and set their "Checked" state. Since the controls are
    dynamically created, I'm using the OnInit event to create the checkboxes
    and set the "Checked" state from the DB.

    Next, I want to capture the postback event (AutoPostBack=t rue) and update
    my database based on the cleared/set item.

    Lastly, I need to re-render the entire checkbox collection, since clicking
    on a "parent" will update "children" on the form.

    Simple enough, eh?

    The problem is this:

    If I have a "Render" operation in the Page_PreRender function, the
    CheckedChanged event handler is not getting called, and the database is not
    being updated. This in turn leaves the children out of sync with the
    database.

    After messing around a bit, I've written a very small program that
    illustrates this problem (inlude below, inline). To trigger the problem,
    simply uncomment the line inthe Page_PreRender function.

    QUESTION: Why does the CheckedChanged handler not get called?

    To use this code, create a web app, and simply add a PlaceHolder control to
    take the checkbox.



    using System;
    using System.Collecti ons;
    using System.Componen tModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.Sess ionState;
    using System.Web.UI;
    using System.Web.UI.W ebControls;
    using System.Web.UI.H tmlControls;

    namespace CheckTest
    {
    /// <summary>
    /// Summary description for WebForm1.
    /// </summary>
    public class WebForm1 : System.Web.UI.P age
    {
    protected System.Web.UI.W ebControls.Plac eHolder
    PlaceHolder1;


    protected void LclHandleCheck( object sender, System.EventArg s e)
    {
    CheckBox ckBox = (CheckBox)sende r;
    ckBox.Text = "Check state: " + ckBox.Checked.T oString();
    }

    protected void SetItemStatus(C heckBox item)
    {
    item.Checked = true;
    }

    protected void CreateCheckBox( )
    {
    CheckBox newBox;
    PlaceHolder1.Co ntrols.Clear();

    newBox = new CheckBox();
    newBox.Text = "OriginalBo x";
    newBox.AutoPost Back = true;
    newBox.CheckedC hanged += new
    System.EventHan dler(this.LclHa ndleCheck);
    newBox.Checked = true;

    this.PlaceHolde r1.Controls.Add (newBox);
    }

    private void Page_PreRender( object sender, System.EventArg s e)
    {
    //CreateCheckBox( );
    }
    private void Page_Load(objec t sender, System.EventArg s e)
    {
    // Put user code to initialize the page here
    }

    #region Web Form Designer generated code
    override protected void OnInit(EventArg s e)
    {
    //
    // CODEGEN: This call is required by the ASP.NET
    Web Form Designer.
    //
    InitializeCompo nent();
    base.OnInit(e);
    CreateCheckBox( );


    }

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeCompo nent()
    {
    this.Load += new
    System.EventHan dler(this.Page_ Load);
    this.PreRender += new
    System.EventHan dler(this.Page_ PreRender);

    }
    #endregion
    }
    }


  • Amit

    #2
    Re: Dymanic Checkbox problems (Web App)

    Try setting the ID of the checkbox before you add it to the placeholder


    "Tomas Vera" <tavera@NOxSPAM sbcglobal.net> wrote in message
    news:456gu0lga6 dciom246rre0tgj a468dpj34@4ax.c om...[color=blue]
    > Hello All,
    > I'm having problems creating a page with dynamic checkboxes in a WebApp.
    >
    > In my app, I need to query a database, then (based on results) add
    > checkboxes to my form and set their "Checked" state. Since the controls[/color]
    are[color=blue]
    > dynamically created, I'm using the OnInit event to create the checkboxes
    > and set the "Checked" state from the DB.
    >
    > Next, I want to capture the postback event (AutoPostBack=t rue) and update
    > my database based on the cleared/set item.
    >
    > Lastly, I need to re-render the entire checkbox collection, since clicking
    > on a "parent" will update "children" on the form.
    >
    > Simple enough, eh?
    >
    > The problem is this:
    >
    > If I have a "Render" operation in the Page_PreRender function, the
    > CheckedChanged event handler is not getting called, and the database is[/color]
    not[color=blue]
    > being updated. This in turn leaves the children out of sync with the
    > database.
    >
    > After messing around a bit, I've written a very small program that
    > illustrates this problem (inlude below, inline). To trigger the problem,
    > simply uncomment the line inthe Page_PreRender function.
    >
    > QUESTION: Why does the CheckedChanged handler not get called?
    >
    > To use this code, create a web app, and simply add a PlaceHolder control[/color]
    to[color=blue]
    > take the checkbox.
    >
    >
    >
    > using System;
    > using System.Collecti ons;
    > using System.Componen tModel;
    > using System.Data;
    > using System.Drawing;
    > using System.Web;
    > using System.Web.Sess ionState;
    > using System.Web.UI;
    > using System.Web.UI.W ebControls;
    > using System.Web.UI.H tmlControls;
    >
    > namespace CheckTest
    > {
    > /// <summary>
    > /// Summary description for WebForm1.
    > /// </summary>
    > public class WebForm1 : System.Web.UI.P age
    > {
    > protected System.Web.UI.W ebControls.Plac eHolder
    > PlaceHolder1;
    >
    >
    > protected void LclHandleCheck( object sender, System.EventArg s e)
    > {
    > CheckBox ckBox = (CheckBox)sende r;
    > ckBox.Text = "Check state: " + ckBox.Checked.T oString();
    > }
    >
    > protected void SetItemStatus(C heckBox item)
    > {
    > item.Checked = true;
    > }
    >
    > protected void CreateCheckBox( )
    > {
    > CheckBox newBox;
    > PlaceHolder1.Co ntrols.Clear();
    >
    > newBox = new CheckBox();
    > newBox.Text = "OriginalBo x";
    > newBox.AutoPost Back = true;
    > newBox.CheckedC hanged += new
    > System.EventHan dler(this.LclHa ndleCheck);
    > newBox.Checked = true;
    >
    > this.PlaceHolde r1.Controls.Add (newBox);
    > }
    >
    > private void Page_PreRender( object sender, System.EventArg s e)
    > {
    > //CreateCheckBox( );
    > }
    > private void Page_Load(objec t sender, System.EventArg s e)
    > {
    > // Put user code to initialize the page here
    > }
    >
    > #region Web Form Designer generated code
    > override protected void OnInit(EventArg s e)
    > {
    > //
    > // CODEGEN: This call is required by the ASP.NET
    > Web Form Designer.
    > //
    > InitializeCompo nent();
    > base.OnInit(e);
    > CreateCheckBox( );
    >
    >
    > }
    >
    > /// <summary>
    > /// Required method for Designer support - do not modify
    > /// the contents of this method with the code editor.
    > /// </summary>
    > private void InitializeCompo nent()
    > {
    > this.Load += new
    > System.EventHan dler(this.Page_ Load);
    > this.PreRender += new
    > System.EventHan dler(this.Page_ PreRender);
    >
    > }
    > #endregion
    > }
    > }
    >
    >[/color]


    Comment

    • Tomas Vera

      #3
      Re: Dymanic Checkbox problems (Web App)

      Thank, but....

      That does not change the behavior. This is beginning to look like some
      sort of bug, since the eventhandler should always be called, if the
      item has changed.

      It is as if the code is "looking ahead" at PreRender and determining
      that the value will not be altered, therefore the value is not being
      evaluted to determine if the handler needs to be called.

      Time to call the MS Boys and see what they know.

      -tomas



      On Fri, 14 Jan 2005 23:10:38 -0500, "Amit" <remove.amitig@ hotmail.com>
      wrote:
      [color=blue]
      >Try setting the ID of the checkbox before you add it to the placeholder
      >
      >
      >"Tomas Vera" <tavera@NOxSPAM sbcglobal.net> wrote in message
      >news:456gu0lga 6dciom246rre0tg ja468dpj34@4ax. com...[color=green]
      >> Hello All,
      >> I'm having problems creating a page with dynamic checkboxes in a WebApp.
      >>
      >> In my app, I need to query a database, then (based on results) add
      >> checkboxes to my form and set their "Checked" state. Since the controls[/color]
      >are[color=green]
      >> dynamically created, I'm using the OnInit event to create the checkboxes
      >> and set the "Checked" state from the DB.
      >>
      >> Next, I want to capture the postback event (AutoPostBack=t rue) and update
      >> my database based on the cleared/set item.
      >>
      >> Lastly, I need to re-render the entire checkbox collection, since clicking
      >> on a "parent" will update "children" on the form.
      >>
      >> Simple enough, eh?
      >>
      >> The problem is this:
      >>
      >> If I have a "Render" operation in the Page_PreRender function, the
      >> CheckedChanged event handler is not getting called, and the database is[/color]
      >not[color=green]
      >> being updated. This in turn leaves the children out of sync with the
      >> database.
      >>
      >> After messing around a bit, I've written a very small program that
      >> illustrates this problem (inlude below, inline). To trigger the problem,
      >> simply uncomment the line inthe Page_PreRender function.
      >>
      >> QUESTION: Why does the CheckedChanged handler not get called?
      >>
      >> To use this code, create a web app, and simply add a PlaceHolder control[/color]
      >to[color=green]
      >> take the checkbox.
      >>
      >>
      >>
      >> using System;
      >> using System.Collecti ons;
      >> using System.Componen tModel;
      >> using System.Data;
      >> using System.Drawing;
      >> using System.Web;
      >> using System.Web.Sess ionState;
      >> using System.Web.UI;
      >> using System.Web.UI.W ebControls;
      >> using System.Web.UI.H tmlControls;
      >>
      >> namespace CheckTest
      >> {
      >> /// <summary>
      >> /// Summary description for WebForm1.
      >> /// </summary>
      >> public class WebForm1 : System.Web.UI.P age
      >> {
      >> protected System.Web.UI.W ebControls.Plac eHolder
      >> PlaceHolder1;
      >>
      >>
      >> protected void LclHandleCheck( object sender, System.EventArg s e)
      >> {
      >> CheckBox ckBox = (CheckBox)sende r;
      >> ckBox.Text = "Check state: " + ckBox.Checked.T oString();
      >> }
      >>
      >> protected void SetItemStatus(C heckBox item)
      >> {
      >> item.Checked = true;
      >> }
      >>
      >> protected void CreateCheckBox( )
      >> {
      >> CheckBox newBox;
      >> PlaceHolder1.Co ntrols.Clear();
      >>
      >> newBox = new CheckBox();
      >> newBox.Text = "OriginalBo x";
      >> newBox.AutoPost Back = true;
      >> newBox.CheckedC hanged += new
      >> System.EventHan dler(this.LclHa ndleCheck);
      >> newBox.Checked = true;
      >>
      >> this.PlaceHolde r1.Controls.Add (newBox);
      >> }
      >>
      >> private void Page_PreRender( object sender, System.EventArg s e)
      >> {
      >> //CreateCheckBox( );
      >> }
      >> private void Page_Load(objec t sender, System.EventArg s e)
      >> {
      >> // Put user code to initialize the page here
      >> }
      >>
      >> #region Web Form Designer generated code
      >> override protected void OnInit(EventArg s e)
      >> {
      >> //
      >> // CODEGEN: This call is required by the ASP.NET
      >> Web Form Designer.
      >> //
      >> InitializeCompo nent();
      >> base.OnInit(e);
      >> CreateCheckBox( );
      >>
      >>
      >> }
      >>
      >> /// <summary>
      >> /// Required method for Designer support - do not modify
      >> /// the contents of this method with the code editor.
      >> /// </summary>
      >> private void InitializeCompo nent()
      >> {
      >> this.Load += new
      >> System.EventHan dler(this.Page_ Load);
      >> this.PreRender += new
      >> System.EventHan dler(this.Page_ PreRender);
      >>
      >> }
      >> #endregion
      >> }
      >> }
      >>
      >>[/color]
      >[/color]

      Comment

      Working...