从动态文本和动态下拉列表中保存数据? [英] save data from dynamic text and dynamic dropdown?

查看:58
本文介绍了从动态文本和动态下拉列表中保存数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个网页,其中有2个按钮(添加行保存)

第一个创建一行,每次单击时都有2个文本框。

第二个保存输入的数据,但我不能参考这些数据。

如何完成每个动态控件的保存?



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

推荐答案

我有一些代码给你



假设您在页面加载时添加了一些控件,例如



I have some code for you

Let say you are adding some control on page load like

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

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





希望这会有所帮助..



Hope this helps..


我找到了解决方案

I found the solution
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


这篇关于从动态文本和动态下拉列表中保存数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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