Thursday, October 16, 2008

Creating Dynamic Table in ASP.NET

The central point is, with ASP.NET, you need to forget HTML. Stop thinking of rendering a "table". Instead, use an ASP.NET Server Control. It will render the table.

Now,I am creating a dynamic(run time ) Table with a Textbox and Dropdownlist with 10 rows

And Here is the code

IN ASP.NET


<form id="form1" runat="server">
<div>
<asp:Table ID="table1" runat ="server" >
<asp:TableHeaderRow ID="TableHeaderRow1" CssClass="TableBorderHeadline" runat="server" >
<asp:TableHeaderCell ID="TableHeaderCell1" runat="server" >TextBox </asp:TableHeaderCell>
<asp:TableHeaderCell ID="TableHeaderCell2" runat="server" >Dropdownlist</asp:TableHeaderCell>
</asp:TableHeaderRow>
</asp:Table>
</div>
</form>




IN VB.NET

Public tblCol(10) As TableCell
Public tblRow(10) As TableRow
Public txtfirst(10) As TextBox
Public ddlfirst(10) As DropDownList


Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
Dim itemCount As Integer
For itemCount = 1 To 10
tblRow(itemCount) = New TableRow

'----this is for txtfirst textbox---------
tblCol(1) = New TableCell
txtfirst(itemCount) = New TextBox
txtfirst(itemCount).ID = "txtfirst" & itemCount
tblCol(1).Controls.Add(txtfirst(itemCount))
tblRow(itemCount).Cells.Add(tblCol(1))
table1.Rows.Add(tblRow(itemCount))


'------this is for ddlfirst dropdownlist------

tblCol(2) = New TableCell
ddlfirst(itemCount) = New DropDownList
ddlfirst(itemCount).ID = "ddlfirst" & itemCount
ddlfirst(itemCount).Width = "70"
tblCol(2).Controls.Add(ddlfirst(itemCount))
tblRow(itemCount).Cells.Add(tblCol(2))
table1.Rows.Add(tblRow(itemCount))
Next
End Sub

No comments: