Dynamically add a asp:checkbox in a table.

U

UJ

I have a table that I need to manually generate based on stuff in the
database. I build the actual table string and stuff it into an asp:panel. So
far it works fine. I'm now trying to add a checkbox to each cell so that
whether it's selected or not.

Problem is, I set the code in the cell during page_load but if I look at the
actual HTML, it says <asp:checkbox ..... whereas everything that is
not dynamic is normal html. So apparently the code is not getting translated
into html.

Here's what I'm actually trying to do: we have pictures we put on our
website. We one a day and remove one a day. The customer would like to see a
'calendar' of pictures and select/deselect the ones they receive. This means
that if it's a Tuesday, they would like to see from Tuesday forward. Monday
would be blank. If it's Thursday, Sunday, Monday, Tuesday and Wednesday
would be blank. Like I said I manually created the table and it works (it's
pretty wild code though) but maybe somebody has a better suggestion on doing
it. The only thing I can think of is to retrieve the data, then prepad the
days so they always start with Sunday and hide everything before this day of
the week.

Anybody got any suggestions?

TIA - Jeff.
 
P

Peter Zolja

Problem is, I set the code in the cell during page_load but if I look at
the actual HTML, it says <asp:checkbox ..... whereas everything that
is not dynamic is normal html. So apparently the code is not getting
translated into html.

How are you adding to the panel? Creating dynamic controls can be a bit
tricky. One thing to keep in mind is that you have to recreate the control
array on every postback; otherwise ASP.NET won't restore the objects (you
won't see them). You also need to do this to keep the viewstate untainted.

Here's an example (not tested)

protected void Page_Init(object sender, EventArgs e)
{
for (int i = 0; i < 20; i++)
{
CheckBox box = new CheckBox();
box.ID = "myBox" + i.ToString();
box.Text = "label " + i.ToString();

Panel1.Controls.Add(box);
}
}
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top