Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi i need to add new row in asp table with textbox and dropdownlist when user clicks

the button.
How can I specify cell for dropdownlist and others for textboxes?



Here is my code with textbox only

VB
Imports System.Web

Imports System.Web.UI

Imports System.Web.UI.WebControls

Partial Class _Default
    Inherits System.Web.UI.Page
    Dim numOfRows As String
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'Generate the Rows on Initial Load

        If Not Page.IsPostBack Then


            GenerateTable(numOfRows)
        End If

    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        If ViewState("RowsCount") IsNot Nothing Then

            numOfRows = Convert.ToInt32(ViewState("RowsCount").ToString())


            GenerateTable(numOfRows)

        End If

    End Sub
    Private Sub SetPreviousData(ByVal rowsCount As Integer, ByVal colsCount As Integer)

        Dim table As Table = DirectCast(Page.FindControl("Table1"), Table)

        If table IsNot Nothing Then

            For i As Integer = 0 To rowsCount - 1

                For j As Integer = 0 To colsCount - 1

                    'Extracting the Dynamic Controls from the Table

                    Dim tb As TextBox = DirectCast(table.Rows(i).Cells(j).FindControl("TextBoxRow_" & i & "Col_" & j), TextBox)

                    'Use Request objects for getting the previous data of the dynamic textbox


                    tb.Text = Request.Form("TextBoxRow_" & i & "Col_" & j)

                Next

            Next
        End If

    End Sub



    Private Sub GenerateTable(ByVal rowsCount As Integer)


        'Creat the Table and Add it to the Page

        Dim table As New Table()

        table.ID = "Table1"

        Page.Form.Controls.Add(table)



        'The number of Columns to be generated

        Const colsCount As Integer = 3
        'You can changed the value of 3 based on you requirements


        ' Now iterate through the table and add your controls



        For i As Integer = 0 To rowsCount - 1

            Dim row As New TableRow()

            For j As Integer = 0 To colsCount - 1

                Dim cell As New TableCell()

                Dim tb As New TextBox()



                ' Set a unique ID for each TextBox added

                tb.ID = "TextBoxRow_" & i & "Col_" & j

                ' Add the control to the TableCell

                cell.Controls.Add(tb)

                ' Add the TableCell to the TableRow


                row.Cells.Add(cell)

            Next



            ' And finally, add the TableRow to the Table


            table.Rows.Add(row)
        Next



        'Set Previous Data on PostBacks

        SetPreviousData(rowsCount, colsCount)



        'Sore the current Rows Count in ViewState

        rowsCount += 1

        ViewState("RowsCount") = rowsCount


    End Sub

I need to add dropdownlist in every first cell in new row.
please some one help me for getting this solution.
Posted
Comments
Mahesh Bailwal 27-Jul-13 14:34pm    
You add dropdownlist same way as you are adding text box. Are you getting any error while doing that?
Dholakiya Ankit 27-Jul-13 15:02pm    
what u want?
Dena2013 27-Jul-13 17:33pm    
I want to add dropdownlist in the first cell only and textbox in others cells.

Below is code for your reference for adding dropdownlist. It may help you.

For i As Integer = 0 To rowsCount - 1
           Dim row As New TableRow()
           For j As Integer = 0 To colsCount - 1
               Dim cell As New TableCell()

               If (j = 0) Then
                   Dim ddl As New DropDownList()
                   ddl.ID = "DropDownList" & i & "Col_" & j
                   cell.Controls.Add(ddl)
               Else
                   Dim tb As New TextBox()
                   tb.ID = "TextBoxRow_" & i & "Col_" & j
                   cell.Controls.Add(tb)

               End If

               row.Cells.Add(cell)
           Next
       Next
 
Share this answer
 
 
Share this answer
 
I find the result which I need
VB
Imports System.Data
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls

Partial Public Class [Default2]
    Inherits System.Web.UI.Page

    Private numOfColumns As Integer = 1
    Private ctr As Integer = 0
    Private table As Table = Nothing

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        If Not IsPostBack Then
            table = New Table()
            table.ID = "tableBuild"
            Session("table") = table

            ViewState("ctr") = ctr
        End If
        ctr = CType(ViewState("ctr"), Int32)
        table = DirectCast(Session("table"), Table)
        Panel1.Controls.Add(table)

    End Sub
    Private Sub GenerateTable(ByVal colsCount As Integer)
        ctr += 1

        Dim row As New TableRow()

        Dim cell As New TableCell()

        Dim tb As New TextBox()

        tb.ID = "TextBoxRow_" & ctr
        tb.Width = 80

        ' Add the control to the TableCell

        cell.Controls.Add(tb)

        ' Add the TableCell to the TableRow

        row.Cells.Add(cell)

        Dim cell1 As New TableCell()

        Dim dd As New DropDownList
        dd.ID = "dd1" & ctr

        dd.Width = 80

        Dim tb1 As New TextBox()
        tb1.ID = "TextBox1Row_" & ctr

        tb1.Width = 80

        ' Add the control to the TableCell

        cell1.Controls.Add(tb1)
        '----------
        cell1.Controls.Add(dd)
        '------------------------
        ' Add the TableCell to the TableRow

        row.Cells.Add(cell1)

        ' Add the TableRow to the Table

        table.Rows.Add(row)
        Session("table") = table
        ViewState("ctr") = ctr

    End Sub


    Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        numOfColumns = 1

        'Generate the Table based from the inputs

        GenerateTable(numOfColumns)
    End Sub

End Class


Now how can I save entered data from these dynamic controls to a database?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900