Take the 2-minute tour ×

In my .NET application I need to add a checkbox to each row in a dynamically created asp:Table. Is it possible to do that by dynamically creating an asp:CheckBox for each row and somehow put it inside a TableCell object? (In that case how?)

Or do I need to replace the asp:table control with something else, like a Repeater control or GridView to make it work?

I'm looking for the quickest solution because I don't have much time.

Thanks in advance!


share|improve this question
are you using asp.net mvc or just webpages? –  TStamper Mar 9 '09 at 15:26
I'm using webpages. -and VB as code behind language. –  Ylva D Mar 9 '09 at 15:51
add comment

2 Answers

up vote 0 down vote accepted

You do not want to do it on server side( in the cs as Yossarian said). because every time your page is reloaded or refreshed, you would have to recreate those checkboxes, which would mean new checkboxes every load, which also would mean your checkbox controls info will be lost because they are not on the client side, so all updated info done by the user (checkbox checked)will be lost, so you want be able to find out what is checked unless you add jquery in and it starts to get more complicated then it needs to be

if you are using webpages then it would be best to use asp:Gridview web control and bind the data to the table in code behind as so:



As shown in the example on this page here

but if you are using MVC then you would add them in the client code in a form as so:

      <% using (Html.BeginForm("Presentation", "Home")) %>
        <% { %>
  <table id="Table" class="color" width="100%" border="1"> 
<colgroup width="3%" ></colgroup>
<colgroup width="15%"></colgroup>
<colgroup width="20%"></colgroup>
<colgroup width="47%"></colgroup>
    <tr class="dxgvHeader_Glass"> 
         <th id="CheckBox" class="style1" ><input type="checkbox" class="selectall" id="selectall" name="CheckBox" /></th>

         <th id="DateTime"  runat="server"></th>  
         <th id="Description" runat="server"></th>
<tbody >
  { %>
   <% foreach (var SamAuditLog in ViewData.Model)
      { %>

            <td class="style1" align="center"><%=Html.CheckBox(""+data.ID) %></td>

             <td><%= data.DateTime%></td>
             <td><%= data.Description%></td>
    <% } %>      

 <%} %>


share|improve this answer
add comment

in aspx:

<asp:Table id=T1 runat=server />

in cs:

TableCell tc;
foreach(TableRow tr in T1.Rows)
    tr.Cells.Add(tc = new TableCell());
    ((IParserAccessor)tc).AddParsedSubObject(new CheckBox());
share|improve this answer
add comment

Your Answer


