在每个按钮上添加新行到asp表 - 单击 [英] Add new row to asp table on each button-Click

查看:76
本文介绍了在每个按钮上添加新行到asp表 - 单击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨我需要在用户点击时用文本框和下拉列表在asp表中添加新行



按钮。

我怎么能为下拉列表指定单元格,为文本框指定其他单元格?







这是我的代码只有文本框



 进口 System.Web 

Imports System.Web.UI

Imports System.Web.UI.WebControls

部分 _Default
继承 System.Web.UI.Page
Dim numOfRows As 字符串
受保护的 Sub Page_Load(< span class =code-keyword> ByVal sender 作为 对象 ByVal e 正如 System.EventArgs)句柄 .Load
' 生成初始加载行

如果 Page.IsPostBack 然后


GenerateTable( numOfRows)
结束 如果

结束 Sub

受保护的 Sub Button1_Click( ByVal sender As 对象 ByVal e As System.EventArgs)句柄 Button1.Click
< span class =code-keyword> If ViewState( RowsCount IsNot 没什么 然后

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


GenerateTable(numOfRows)

结束 如果

结束 Sub
私有 Sub SetPreviousData( ByVal rowsCount 正如 In teger ByVal colsCount 作为 整数

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

如果 IsNot 什么 然后

对于 i 作为 整数 = 0 To rowsCount - 1

对于 j 作为 整数 = 0 colsCount - 1

' 从表中提取动态控件

Dim tb As TextBox = DirectCast (table.Rows( i).Cells(j).FindControl( TextBoxRow _&我和我 Col _& j),TextBox)

' 使用Request对象获取动态文本框的先前数据


tb.Text = Request.Form( TextBoxRow_ & i& Col _& j)

下一步

下一步
结束 如果

结束 Sub



私有 Sub GenerateTable( ByVal rowsCount As 整数


' 创建表并将其添加到页面

Dim 作为 表()

table.ID = Table1

Page.Form。 Controls.Add(table)



' 数量要生成的列

Const colsCount As 整数 = 3
' 您可以根据您的要求更改值3


' < span class =code-comment>现在遍历表并添加您的控件



对于 i As 整数 = 0 rowsCount - 1

Dim 正如 TableRow()

对于 j 作为 整数 = 0 colsCount - 1

Dim cell < span class =code-keyword> As New TableCell()

昏暗 tb 作为 TextBox()



' 设置唯一ID每个TextBox添加

tb.ID = TextBoxRow _ &安培;我和我 Col _& j

' 将控件添加到TableCell

cell.Controls.Add(tb)

' 将TableCell添加到TableRow


row.Cells.Add(cell)

Next



' 最后,将TableRow添加到表


table.Rows.Add(row)
下一步



' 在PostBacks上设置以前的数据

SetPreviousData(rowsCount ,colsCount)



' 疼痛当前行数视图状态

rowsCount + = 1

ViewState( RowsCount)= rowsCount


结束 < span class =code-keyword> Sub



我需要在新行的每个第一个单元格中添加下拉列表。

请帮助我获得这个解决方案。

解决方案

下面是您添加下拉列表的参考代码。它可能对你有帮助。



对于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&我和我Col_& j
cell.Controls.Add(ddl)
Else
Dim tb As New TextBox()
tb.ID =TextBoxRow_&我和我Col_& j
cell.Controls.Add(tb)

结束如果

row.Cells.Add(cell)
Next
Next


试试这个 [在ASP.Net中创建动态DropDownList控件]


< blockquote>我找到了我需要的结果

  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
IMPOR ts System.Web.UI.HtmlControls

部分 公开 [Default2]
继承 System.Web.UI.Page

私有 numOfColumns 作为 整数 = < span class =code-digit> 1
Private ctr As < span class =code-keyword>整数 = 0
私人作为表= 没有

受保护的 Sub Page_Load( ByVal sender As 对象 ByVal e As EventArgs)句柄 .Load
如果 IsPostBack 然后
table = 表()
table.ID = tableBuild
会话( table)= table

ViewState( ctr )= ctr
结束 如果
ctr = CType (ViewState( ctr), Int32
table = DirectCast (会话( table),表)
Panel1.Controls.Add(table)

End Sub
私有 Sub GenerateTable( ByVal colsCount 作为 整数
ctr + = 1

Dim As TableRow()

Dim cell As TableCell()

Dim tb 作为 TextBox()

tb.ID = TextBoxRow _& ctr
tb.Width = 80

' 将控件添加到TableCell

cell.Controls.Add(tb)

' 将TableCell添加到TableRow

row.Cells.Add(cell)

Dim cell1 As TableCell()

Dim dd 作为 DropDownList
dd.ID = dd1& ctr

dd.Width = 80

Dim tb1 作为 TextBox()
tb1.ID = TextBox1Row _& ctr

tb1.Width = 80

' 将控件添加到TableCell

cell1.Controls.Add(tb1)
' ----------
cell1.Controls.Add(dd)
------------------------
' 将TableCell添加到TableRow

row.Cells。添加(cell1)

' 将TableRow添加到表中

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

结束 Sub


受保护的 Sub btnAdd_Click( ByVal sender 作为 对象 ByVal e As System.EventArgs)句柄 btnAdd.Click
numOfColumns = 1

' 根据输入生成表格

GenerateTable (numOfColumns)
结束 Sub

结束 Class





现在怎么样我可以将输入的数据从这些动态控件保存到数据库吗?

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

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.

解决方案

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


Try This [Creating Dynamic DropDownList Controls in ASP.Net]


I find the result which I need

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?


这篇关于在每个按钮上添加新行到asp表 - 单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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