views:

1622

answers:

2

Hi everyone,

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!

/Ylva

+1  A: 

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());
}
Yossarian
A: 

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:

  Gridview.Datasource=//ex:data; 

  Gridview.Databind();

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="15%"></colgroup>
<colgroup width="47%"></colgroup>
<thead>
    <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>
    </tr>
</thead> 
<tbody >
<%try
  { %>
   <% foreach (var SamAuditLog in ViewData.Model)
      { %>
        <tr>

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


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

 <%} %>

</tbody>

TStamper