如何将输入的数据从动态控件保存到数据库? [英] how can I save entered data from dynamic controls to a database?

查看:171
本文介绍了如何将输入的数据从动态控件保存到数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用此代码来创建动态控件。

如何将输入的数据从动态控件保存到数据库?





I use this code to create dynamic controls.
How can I save entered data from dynamic controls to a database?


 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

推荐答案

您需要在On_Init中创建控件事件或者你会遇到ViewState的问题,假设你正在使用它。



然后在你的按钮点击或你访问它们的任何地方你可以从请求中获取它们.Form对象。
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.


这篇关于如何将输入的数据从动态控件保存到数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆