Click here to Skip to main content
16,013,944 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I use this code to create dynamic controls.
How can I save entered data from dynamic controls to a database?


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
Posted
Comments
[no name] 30-Jul-13 19:20pm    
"How can I save entered data from dynamic controls to a database?", the exact same way you save the data from any control. Just because you created the controls "dynamically" makes no difference.

1 solution

You need to create the controls in the On_Init event or else you will have issues with ViewState, assuming you are using it.

Then in your button click or wherever you are accessing them you can get them from the Request.Form object.
 
Share this answer
 
Comments
Dena2013 4-Aug-13 15:37pm    
ryanb31 I didn't got your solution! give me an example plz.
in another way I need to fill all dropdownlists with specific data and save entered data from all controls to a database.
so I try to find controls by type but it didn't work with me. Is there any solutions for that?

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