Creating ASP.NET Server Controls Dynamically....

P

Pai

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
 
A

Alvin Bruney

you need to add the controls to the forms collection to solve this problem
form.controls.add blah blah blah
 
S

Srikanth Pai

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
 
S

Srikanth Pai

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.
 
P

Pai

Srikanth Pai said:
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
 
T

Teemu Keiski

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
 
P

Pai

Teemu Keiski said:
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

Hello Teemu Keiski ,

Thnaks for the response.

It did work.

Kind Regards,
Srikanth Pai
 
P

Pai

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.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top