Click here to Skip to main content
15,876,991 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I create a web page which has 2 button (add row-save)
the first one create one row with 2 text boxes each time I click it.
the second one to save the entered data, but I can't refer to these data.
How can I complete the saving from each these dynamic control?

Imports System.Data.SqlClient
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
    Dim dd As New DropDownList
    Public strConn As String = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString()
    'Dim cn As New SqlConnection(strConnString)
    Dim cmd As New SqlCommand()
    Dim con As New SqlConnection(strConn)
    Dim objDs As New DataSet()
    Dim dAdapter As New SqlDataAdapter()
    'Dim tb As New TextBox()
    Private numOfColumns As Integer = 1
    'Private ctr As Integer = 0
    Public ctr As Integer = 0
    Private table As Table = Nothing
    Dim mycontrol As Control = FindControl("TextBoxRow_" & ctr)
    Dim mycontrol2 As Control = FindControl("TextBox2Row_" & ctr)
    Dim mydd As Control = FindControl("dd_" & ctr)
    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 FillClasses()
        
        cmd.Connection = con
        cmd.CommandType = CommandType.Text
        cmd.CommandText = "SELECT classno, classname FROM t1"
       
        dAdapter.SelectCommand = cmd
        con.Open()
        dAdapter.Fill(objDs)
        con.Close()
        If objDs.Tables(0).Rows.Count > 0 Then
            dd.DataSource = objDs.Tables(0)
            dd.DataTextField = "ClassName"
            dd.DataValueField = "classno"
            dd.DataBind()
            dd.Items.Insert(0, "--Select--")
        Else
            Response.Write("No class found")
        End If

    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
        tb.Text = "TextBoxRow_" & ctr
        ' 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 = "dd_" & ctr

        dd.Width = 80

        Dim tb1 As New TextBox()
        tb1.ID = "TextBox2Row_" & ctr
        tb1.Text = "TextBox2Row_" & 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)
        FillClasses()


    End Sub


   

    Protected Sub btnsave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnsave.Click
        con.Open()
        'the below 2 line code is not working as saving data from textboxes and dropdownlist to table t2

        'Dim sql As String = "insert into t2 (idd,col1,col2) values( mycontrol , mycontrol2 ,mydd )"

        'Dim sql As String = "insert into t2 (idd,col1,col2) values ( '" + ("TextBoxRow_" & ctr) + "', '" + ("TextBox2Row_" & ctr) + "','" + ("dd_" & ctr) + "')"



        Dim cmd As New SqlCommand(sql, con)

        Dim obj As Object = cmd.ExecuteNonQuery()


        If IsDBNull(obj) Then

            Response.Write("error, try agin")
        Else

            Response.Write("Done")
        End If
    End Sub

  
End Class
Posted
Updated 14-Aug-13 7:31am
v2
Comments
Sergey Alexandrovich Kryukov 8-Aug-13 19:24pm    
Dynamic? What would be not dynamic text? and not dynamic control? :-)
—SA
Dena2013 9-Aug-13 3:22am    
dynamic control which is created at runtime
Sergey Alexandrovich Kryukov 9-Aug-13 10:33am    
There are no controls which are created otherwise. Not a single one.
—SA

I have some code for you

Let say you are adding some control on page load like

C#
protected void Page_Load(object sender, EventArgs e)
        {
            TextBox txt = new TextBox();
            txt.ID = "txtbox";
            form1.Controls.Add(txt);

        }


and accessing it after button click

C#
protected void btn1_Click(object sender, EventArgs e)
{
    if (Request["txtbox"] != null)
    {
        Response.Write((Request["txtbox"].ToString()));
    }
}


Hope this helps..
 
Share this answer
 
Comments
Dena2013 9-Aug-13 3:25am    
sumit thanks for your help, but I need to know how to save not how to create dynamic controls.
sumit_kapadia 9-Aug-13 3:35am    
you mean you want to persists dynamic controls after each postback..
Dena2013 9-Aug-13 19:35pm    
No sumit
I mean I need to save data entered in these controls to a database as I wrote above in the code inside (btnsave_Click)
I found the solution
VB
Protected Sub btnsave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnsave.Click
    con.Open()
    Dim sql As String = "insert into t2 (idd,col1,col2) values ( @t1,@t2,@ddd)"
    Dim cmd As New SqlCommand(Sql, con)
    cmd.Parameters.AddWithValue("@t1", CType(Panel1.FindControl("TextBoxRow_" & ctr), TextBox).Text)
    cmd.Parameters.AddWithValue("@t2", CType(Panel1.FindControl("TextBox2Row_" & ctr), TextBox).Text)
    cmd.Parameters.AddWithValue("@ddd", CType(Panel1.FindControl("dd_" & ctr), DropDownList).Text)
    Dim obj As Object = cmd.ExecuteNonQuery()
    If IsDBNull(obj) Then
        Response.Write("error, try agin")
    Else
        Response.Write("Done")
    End If
End Sub
 
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