Creating ASP.NET Server Controls Dynamically....

Discussion in 'ASP .Net Web Controls' started by Pai, Aug 25, 2003.

  1. Pai

    Pai Guest

    Hello there,

    I need to create a Check Box Dynamically,

    I have the following code in my page:

    Button my_btn = new Button() ;
    my_btn.Text = "hello";
    this.Controls.Add(my_btn);

    The server gives an error

    Control '_ctl1' of type 'Button' must be placed inside a form tag with
    runat=server.

    I am sure i am mising something and there is something more to go,

    Any Help please.

    Kind Regards,
    Srikanth Pai
    Pai, Aug 25, 2003
    #1
    1. Advertising

  2. Pai

    Alvin Bruney Guest

    you need to add the controls to the forms collection to solve this problem
    form.controls.add blah blah blah
    "Pai" <> wrote in message
    news:...
    > Hello there,
    >
    > I need to create a Check Box Dynamically,
    >
    > I have the following code in my page:
    >
    > Button my_btn = new Button() ;
    > my_btn.Text = "hello";
    > this.Controls.Add(my_btn);
    >
    > The server gives an error
    >
    > Control '_ctl1' of type 'Button' must be placed inside a form tag with
    > runat=server.
    >
    > I am sure i am mising something and there is something more to go,
    >
    > Any Help please.
    >
    > Kind Regards,
    > Srikanth Pai
    Alvin Bruney, Aug 25, 2003
    #2
    1. Advertising

  3. Pai

    Srikanth Pai Guest

    Hello Bruney,

    I was not able to add it to the form.

    I did add it to a panel, I believe it requieres a container to hold the
    dynamically created controls.

    Since I have checkboxes added to the Panel, I would like to iterate
    through these dynamically added controls to check which of the
    checkboxes are checked.

    Also I was able to figure out that I need to add this in the Page Load
    event of the page,If I have added it to the click event of a button the
    controls will disappear when the checkbox is clicked, it should be
    because it is submitted again as I have set the PostBack Property of the
    check box to true, can u also let me know how to got about this.

    Kind Regards,
    Srikanth PAi



    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
    Srikanth Pai, Aug 25, 2003
    #3
  4. Pai

    Teemu Keiski Guest

    In addition you could check this as discussion about dynamically created
    controls:
    http://www.asp.net/Forums/ShowPost.aspx?tabindex=1&PostID=242132

    Just for background. ;-)

    --
    Teemu Keiski
    MCP, Designer/Developer
    Mansoft tietotekniikka Oy
    http://www.mansoft.fi

    AspInsiders Member, www.aspinsiders.com
    ASP.NET Forums Moderator, www.asp.net
    AspAlliance Columnist, www.aspalliance.com

    "Pai" <> wrote in message
    news:...
    > Hello there,
    >
    > I need to create a Check Box Dynamically,
    >
    > I have the following code in my page:
    >
    > Button my_btn = new Button() ;
    > my_btn.Text = "hello";
    > this.Controls.Add(my_btn);
    >
    > The server gives an error
    >
    > Control '_ctl1' of type 'Button' must be placed inside a form tag with
    > runat=server.
    >
    > I am sure i am mising something and there is something more to go,
    >
    > Any Help please.
    >
    > Kind Regards,
    > Srikanth Pai
    Teemu Keiski, Aug 25, 2003
    #4
  5. Pai

    Srikanth Pai Guest

    Hello Keiski,

    I have added controls dynamically like as below and some time it might
    ne 5 and then some times 6 controls.....

    for (int i=1;i<3;i++)
    {
    B1 = new CheckBox();
    B1.ID = "Check"+i;
    B1.Text = "Check"+i;
    B1.Visible = true;
    B1.AutoPostBack = true;
    Panel1.Controls.Add(B1);

    }

    How do i get the value of whether the checkbox is checked or not.

    Kind Regards,
    Srikanth pai.






    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
    Srikanth Pai, Aug 25, 2003
    #5
  6. Pai

    Pai Guest

    Srikanth Pai <> wrote in message news:<uxDQ#>...
    > Hello Keiski,
    >
    > I have added controls dynamically like as below and some time it might
    > ne 5 and then some times 6 controls.....
    >
    > for (int i=1;i<3;i++)
    > {
    > B1 = new CheckBox();
    > B1.ID = "Check"+i;
    > B1.Text = "Check"+i;
    > B1.Visible = true;
    > B1.AutoPostBack = true;
    > Panel1.Controls.Add(B1);
    >
    > }
    >
    > How do i get the value of whether the checkbox is checked or not.
    >
    > Kind Regards,
    > Srikanth pai.
    >
    >
    > Hello there,


    I have tried the following code...

    Control C = new Control();

    foreach (Control M in Panel1.Controls)
    {
    Response.Write("M:"+M.GetType().ToString().ToString() +"<br>");

    if ( ((CheckBox) M).Checked)
    {
    Response.Write ("Checked");
    }
    }

    But i get the following error...

    "Specified cast is not valid"

    Any suggestions here ....

    Kind Regards,
    Srikanth Pai
    > *** Sent via Developersdex http://www.developersdex.com ***
    > Don't just participate in USENET...get rewarded for it!
    Pai, Aug 25, 2003
    #6
  7. Pai

    Teemu Keiski Guest

    If you have Panel and Button on Page like:
    <form id="Form1" method="post" runat="server">
    <asp:panel ID="Panel1" Runat="server" /><asp:Button id="Button1"
    style="Z-INDEX: 101; LEFT: 18px; POSITION: absolute; TOP: 54px"
    runat="server" Text="Button"></asp:Button>
    </form>

    Then on Page_Load:

    private void Page_Load(object sender, System.EventArgs e)
    {
    //Creating CheckBoxes
    for (int i=1;i<3;i++)
    {
    CheckBox B1 = new CheckBox();
    B1.ID = "Check"+i;
    B1.Text = "Check"+i;
    B1.Visible = true;
    B1.AutoPostBack = true;
    Panel1.Controls.Add(B1);
    }
    }

    And on Button Click:
    private void Button1_Click(object sender, System.EventArgs e)
    {
    foreach (Control c in Panel1.Controls)
    {

    CheckBox chk=c as CheckBox;
    if(chk != null)
    {
    if (chk.Checked)
    {
    Response.Write ("Checked");
    }
    }
    }
    }

    So as a result you actually can't read the Checked property ftrom dynamical
    CheckBoxes on Page_load because postback data is loaded after Page_Load for
    dynamical controls. The lifecycle is as I told in a thread at ASP.NET
    Forums.

    1. Instantiate
    2. Initialize
    3. TrackViewState
    4. LoadViewState (postback)
    5. Load postback data (postback, IPostBackDatahandler.LoadPostdata)
    6. Load
    7. Load postback data for dynamical controls added on Page_Load
    8. Raise Changed Events (postback,
    IPostBackDatahandler.RaisePostDataChanged)
    9. Raise postback event (postback, IPostBackEventHandler.RaisePostBackEvent)
    10.PreRender
    11. SaveViewState
    12. Render
    13. Unload
    14. Dispose

    And it is the 7th that explains if control are created at Page_Load theyr
    postback data is not available yet. Another solution certainly could be that
    create ChekBoxes in Page_INit (Initialize phase) when postback data should
    be loaded before Page_Load (as the cycle explains)

    --
    Teemu Keiski
    MCP,Designer/Developer
    Mansoft tietotekniikka Oy
    http://www.mansoft.fi

    AspInsiders Member, www.aspinsiders.com
    ASP.NET Forums Moderator, www.asp.net
    AspAlliance Columnist, www.aspalliance.com

    "Pai" <> kirjoitti viestissä
    news:...
    > Srikanth Pai <> wrote in message

    news:<uxDQ#>...
    > > Hello Keiski,
    > >
    > > I have added controls dynamically like as below and some time it might
    > > ne 5 and then some times 6 controls.....
    > >
    > > for (int i=1;i<3;i++)
    > > {
    > > B1 = new CheckBox();
    > > B1.ID = "Check"+i;
    > > B1.Text = "Check"+i;
    > > B1.Visible = true;
    > > B1.AutoPostBack = true;
    > > Panel1.Controls.Add(B1);
    > >
    > > }
    > >
    > > How do i get the value of whether the checkbox is checked or not.
    > >
    > > Kind Regards,
    > > Srikanth pai.
    > >
    > >
    > > Hello there,

    >
    > I have tried the following code...
    >
    > Control C = new Control();
    >
    > foreach (Control M in Panel1.Controls)
    > {
    > Response.Write("M:"+M.GetType().ToString().ToString() +"<br>");
    >
    > if ( ((CheckBox) M).Checked)
    > {
    > Response.Write ("Checked");
    > }
    > }
    >
    > But i get the following error...
    >
    > "Specified cast is not valid"
    >
    > Any suggestions here ....
    >
    > Kind Regards,
    > Srikanth Pai
    > > *** Sent via Developersdex http://www.developersdex.com ***
    > > Don't just participate in USENET...get rewarded for it!
    Teemu Keiski, Aug 26, 2003
    #7
  8. Pai

    Pai Guest

    "Teemu Keiski" <> wrote in message news:<>...
    > If you have Panel and Button on Page like:
    > <form id="Form1" method="post" runat="server">
    > <asp:panel ID="Panel1" Runat="server" /><asp:Button id="Button1"
    > style="Z-INDEX: 101; LEFT: 18px; POSITION: absolute; TOP: 54px"
    > runat="server" Text="Button"></asp:Button>
    > </form>
    >
    > Then on Page_Load:
    >
    > private void Page_Load(object sender, System.EventArgs e)
    > {
    > //Creating CheckBoxes
    > for (int i=1;i<3;i++)
    > {
    > CheckBox B1 = new CheckBox();
    > B1.ID = "Check"+i;
    > B1.Text = "Check"+i;
    > B1.Visible = true;
    > B1.AutoPostBack = true;
    > Panel1.Controls.Add(B1);
    > }
    > }
    >
    > And on Button Click:
    > private void Button1_Click(object sender, System.EventArgs e)
    > {
    > foreach (Control c in Panel1.Controls)
    > {
    >
    > CheckBox chk=c as CheckBox;
    > if(chk != null)
    > {
    > if (chk.Checked)
    > {
    > Response.Write ("Checked");
    > }
    > }
    > }
    > }
    >
    > So as a result you actually can't read the Checked property ftrom dynamical
    > CheckBoxes on Page_load because postback data is loaded after Page_Load for
    > dynamical controls. The lifecycle is as I told in a thread at ASP.NET
    > Forums.
    >
    > 1. Instantiate
    > 2. Initialize
    > 3. TrackViewState
    > 4. LoadViewState (postback)
    > 5. Load postback data (postback, IPostBackDatahandler.LoadPostdata)
    > 6. Load
    > 7. Load postback data for dynamical controls added on Page_Load
    > 8. Raise Changed Events (postback,
    > IPostBackDatahandler.RaisePostDataChanged)
    > 9. Raise postback event (postback, IPostBackEventHandler.RaisePostBackEvent)
    > 10.PreRender
    > 11. SaveViewState
    > 12. Render
    > 13. Unload
    > 14. Dispose
    >
    > And it is the 7th that explains if control are created at Page_Load theyr
    > postback data is not available yet. Another solution certainly could be that
    > create ChekBoxes in Page_INit (Initialize phase) when postback data should
    > be loaded before Page_Load (as the cycle explains)
    >
    > --
    > Teemu Keiski
    > MCP,Designer/Developer
    > Mansoft tietotekniikka Oy
    > http://www.mansoft.fi
    >
    > AspInsiders Member, www.aspinsiders.com
    > ASP.NET Forums Moderator, www.asp.net
    > AspAlliance Columnist, www.aspalliance.com
    >
    > "Pai" <> kirjoitti viestissä
    > news:...
    > > Srikanth Pai <> wrote in message

    > news:<uxDQ#>...
    > > > Hello Keiski,
    > > >
    > > > I have added controls dynamically like as below and some time it might
    > > > ne 5 and then some times 6 controls.....
    > > >
    > > > for (int i=1;i<3;i++)
    > > > {
    > > > B1 = new CheckBox();
    > > > B1.ID = "Check"+i;
    > > > B1.Text = "Check"+i;
    > > > B1.Visible = true;
    > > > B1.AutoPostBack = true;
    > > > Panel1.Controls.Add(B1);
    > > >
    > > > }
    > > >
    > > > How do i get the value of whether the checkbox is checked or not.
    > > >
    > > > Kind Regards,
    > > > Srikanth pai.
    > > >
    > > >
    > > > Hello there,

    > >
    > > I have tried the following code...
    > >
    > > Control C = new Control();
    > >
    > > foreach (Control M in Panel1.Controls)
    > > {
    > > Response.Write("M:"+M.GetType().ToString().ToString() +"<br>");
    > >
    > > if ( ((CheckBox) M).Checked)
    > > {
    > > Response.Write ("Checked");
    > > }
    > > }
    > >
    > > But i get the following error...
    > >
    > > "Specified cast is not valid"
    > >
    > > Any suggestions here ....
    > >
    > > Kind Regards,
    > > Srikanth Pai
    > > > *** Sent via Developersdex http://www.developersdex.com ***
    > > > Don't just participate in USENET...get rewarded for it!


    Hello Teemu Keiski ,

    Thnaks for the response.

    It did work.

    Kind Regards,
    Srikanth Pai
    Pai, Aug 26, 2003
    #8
  9. Pai

    Pai Guest

    (Pai) wrote in message news:<>...
    > "Teemu Keiski" <> wrote in message news:<>...
    > > If you have Panel and Button on Page like:
    > > <form id="Form1" method="post" runat="server">
    > > <asp:panel ID="Panel1" Runat="server" /><asp:Button id="Button1"
    > > style="Z-INDEX: 101; LEFT: 18px; POSITION: absolute; TOP: 54px"
    > > runat="server" Text="Button"></asp:Button>
    > > </form>
    > >
    > > Then on Page_Load:
    > >
    > > private void Page_Load(object sender, System.EventArgs e)
    > > {
    > > //Creating CheckBoxes
    > > for (int i=1;i<3;i++)
    > > {
    > > CheckBox B1 = new CheckBox();
    > > B1.ID = "Check"+i;
    > > B1.Text = "Check"+i;
    > > B1.Visible = true;
    > > B1.AutoPostBack = true;
    > > Panel1.Controls.Add(B1);
    > > }
    > > }
    > >
    > > And on Button Click:
    > > private void Button1_Click(object sender, System.EventArgs e)
    > > {
    > > foreach (Control c in Panel1.Controls)
    > > {
    > >
    > > CheckBox chk=c as CheckBox;
    > > if(chk != null)
    > > {
    > > if (chk.Checked)
    > > {
    > > Response.Write ("Checked");
    > > }
    > > }
    > > }
    > > }
    > >
    > > So as a result you actually can't read the Checked property ftrom dynamical
    > > CheckBoxes on Page_load because postback data is loaded after Page_Load for
    > > dynamical controls. The lifecycle is as I told in a thread at ASP.NET
    > > Forums.
    > >
    > > 1. Instantiate
    > > 2. Initialize
    > > 3. TrackViewState
    > > 4. LoadViewState (postback)
    > > 5. Load postback data (postback, IPostBackDatahandler.LoadPostdata)
    > > 6. Load
    > > 7. Load postback data for dynamical controls added on Page_Load
    > > 8. Raise Changed Events (postback,
    > > IPostBackDatahandler.RaisePostDataChanged)
    > > 9. Raise postback event (postback, IPostBackEventHandler.RaisePostBackEvent)
    > > 10.PreRender
    > > 11. SaveViewState
    > > 12. Render
    > > 13. Unload
    > > 14. Dispose
    > >
    > > And it is the 7th that explains if control are created at Page_Load theyr
    > > postback data is not available yet. Another solution certainly could be that
    > > create ChekBoxes in Page_INit (Initialize phase) when postback data should
    > > be loaded before Page_Load (as the cycle explains)
    > >
    > > --
    > > Teemu Keiski
    > > MCP,Designer/Developer
    > > Mansoft tietotekniikka Oy
    > > http://www.mansoft.fi
    > >
    > > AspInsiders Member, www.aspinsiders.com
    > > ASP.NET Forums Moderator, www.asp.net
    > > AspAlliance Columnist, www.aspalliance.com
    > >
    > > "Pai" <> kirjoitti viestissä
    > > news:...
    > > > Srikanth Pai <> wrote in message

    > news:<uxDQ#>...
    > > > > Hello Keiski,
    > > > >
    > > > > I have added controls dynamically like as below and some time it might
    > > > > ne 5 and then some times 6 controls.....
    > > > >
    > > > > for (int i=1;i<3;i++)
    > > > > {
    > > > > B1 = new CheckBox();
    > > > > B1.ID = "Check"+i;
    > > > > B1.Text = "Check"+i;
    > > > > B1.Visible = true;
    > > > > B1.AutoPostBack = true;
    > > > > Panel1.Controls.Add(B1);
    > > > >
    > > > > }
    > > > >
    > > > > How do i get the value of whether the checkbox is checked or not.
    > > > >
    > > > > Kind Regards,
    > > > > Srikanth pai.
    > > > >
    > > > >
    > > > > Hello there,
    > > >
    > > > I have tried the following code...
    > > >
    > > > Control C = new Control();
    > > >
    > > > foreach (Control M in Panel1.Controls)
    > > > {
    > > > Response.Write("M:"+M.GetType().ToString().ToString() +"<br>");
    > > >
    > > > if ( ((CheckBox) M).Checked)
    > > > {
    > > > Response.Write ("Checked");
    > > > }
    > > > }
    > > >
    > > > But i get the following error...
    > > >
    > > > "Specified cast is not valid"
    > > >
    > > > Any suggestions here ....
    > > >
    > > > Kind Regards,
    > > > Srikanth Pai
    > > > > *** Sent via Developersdex http://www.developersdex.com ***
    > > > > Don't just participate in USENET...get rewarded for it!

    >
    > Hello Teemu Keiski ,
    >
    > Thnaks for the response.
    >
    > It did work.


    But the Controls get placed one after other, I would like them to be
    placed one below another any property of the Panel whcih needs to be
    set.
    >
    > Kind Regards,
    > Srikanth Pai
    Pai, Aug 26, 2003
    #9
    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. =?Utf-8?B?UGF0cmljaw==?=

    Dynamically creating a web form with server controls

    =?Utf-8?B?UGF0cmljaw==?=, May 20, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    748
    Steven Cheng[MSFT]
    May 23, 2005
  2. Bob Rock
    Replies:
    1
    Views:
    611
    Cowboy \(Gregory A. Beamer\)
    Sep 17, 2006
  3. Andy B
    Replies:
    2
    Views:
    352
    Andy B
    Jun 16, 2008
  4. Chris Lane
    Replies:
    1
    Views:
    107
    Hermit Dave
    Nov 25, 2003
  5. Replies:
    0
    Views:
    610
Loading...

Share This Page