从动态创建的文本框中获取值 [英] Get a Value from dynamically created Textbox

查看:81
本文介绍了从动态创建的文本框中获取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在运行时在Table中创建了文本框.我如何从所有文本框中获取值.
这是我的代码

 受保护的  Page_Load( ByVal 发​​件人 As  对象 ByVal  e  As  System.EventArgs)句柄  .Load
        如果 不是 IsPostBack >然后
            会话(" )=  0 
             Dim  As  Table = 新建表( )

            table.ID = " 
            Page.Form.Controls.Add(表)
             Dim  i  As  整数
            对于 i =  0    3 
                 Dim 文本 As  TextBox = 新建 TextBox( )
                text.ID = " &一世

                Page.Form.Controls.Add(新建 TextBox())
            下一步
            会话(" )=会话("  count")+ 结束 如果
    结束  



 受保护的  Button1_Click( ByVal 发​​件人 As  对象 ByVal  e  As  EventArgs)句柄 Button1.Click

 Dim  ds  As  DataTable =  New  DataTable( )
        ds.Columns.Add(" )
         Dim 标签 As  Table =  New 表( )
        tab.ID = " 
        Page.Form.Controls.Add(标签)
         Dim  As  整数 =会话(" )

        对于 i =  0  >也
             Dim  As  TableRow = 新建 TableRow( )
            对于 j =  0    3 
                 Dim 单元格 As  TableCell = 新建 TableCell( )
                 Dim  textb  As  TextBox =  New  TextBox( )
                textb.ID = " &我和" & Ĵ
                ds.Rows.Add(textb.ID)
                cell.Controls.Add(textb)
                row.Cells.Add(单元格)
            下一步

            tab.Rows.Add(行)
        下一步
        会话(" )=会话("  count")+ " )= ds
    结束 

受保护的  Button2_Click( ByVal 发​​件人目标 对象 ByVal  e 句柄 Button2.Click

-----在这里,我想输入代码何时,当我单击此按钮控件时,我想获取动态地获得一个值制作的TEXBOX.


 结束  

解决方案

在页面回发中(例如在OnInit事件中),重新创建相同的控件(具有相同的ID),您将能够像其他所有控件一样检索值.

假设您在panel.Controls.Add(myTextBox)中添加了一些控件.现在,在OnInit事件中的PostBack或CallBack上再次添加它.

欢呼声


我们都倾向于认为(我曾经),一旦我们向页面动态添加控件,它将自动从那时起可用(如果有人从未进入此类别,请原谅我).产生这种误解的原因是,我们查看了.aspx页页面中添加的普通控件,并认为一旦添加完所有控件,这些控件就可以自动在所有回发中使用数据,因此我们不需要做任何事情,但是我们不需要做任何事情意识到甚至那些控件都已重新创建并在每次回发期间分配了回发数据.在动态添加控件(-)的情况下,有时甚至可以期望viewstate会抢救出来,但是只有当我们意识到屏幕背后到底发生了什么(页面生命周期)以及什么viewstate是我们可以嘲笑我们所使用的思想时,才有希望.请参阅此文章,它可以提供很多帮助.


I created the textbox at runtime in Table. how do i get a value from that all textbox.
Here is My Code

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Session("count") = 0
            Dim table As Table = New Table()

            table.ID = "Tab1"
            Page.Form.Controls.Add(table)
            Dim i As Integer
            For i = 0 To 3
                Dim text As TextBox = New TextBox()
                text.ID = "text_" & i

                Page.Form.Controls.Add(New TextBox())
            Next
            Session("count") = Session("count") + 1
        End If
    End Sub



Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click

Dim ds As DataTable = New DataTable()
        ds.Columns.Add("Textboxid")
        Dim tab As Table = New Table()
        tab.ID = "Table1"
        Page.Form.Controls.Add(tab)
        Dim too As Integer = Session("count")

        For i = 0 To too
            Dim row As TableRow = New TableRow()
            For j = 0 To 3
                Dim cell As TableCell = New TableCell()
                Dim textb As TextBox = New TextBox()
                textb.ID = "text_" & i & "Col" & j
                ds.Rows.Add(textb.ID)
                cell.Controls.Add(textb)
                row.Cells.Add(cell)
            Next

            tab.Rows.Add(row)
        Next
        Session("count") = Session("count") + 1
        Session("dataTable") = ds
    End Sub

Protected Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button2.Click

-----HERE I WANT CODE WHEN I CLICK THIS BUTTON CONTROL I WANT GET A VALUE FROM DYNAMICALLY CREATED TEXBOX.


 End Sub

解决方案

On page postback (at example in OnInit event), recreate the same control (with the same ID) and you will be able to retrieve the value as with every other control.

Let''s say that you added some control in a panel.Controls.Add(myTextBox). Now add it again on PostBack or CallBack in the OnInit event.

Cheers


We all tend to think( I used to) that once we dynamically add a control to a page it will automatically be available from then onwards (forgive me if someone never been in this category). This misconception occurs because we look at the normal controls added in the .aspx page page and think once we added they were all automatically available in all the postbacks from then on with data,we didnt need to do a single thing, but what we dont realize is even those controls were recreated and assigned the posted back data during each post back. Even viewstate is sometimes expected to come to the rescue in case of dynamically adding controls :-), but only when we realize whats exactly happening behind the screen(page life cycle) and what viewstate is that we can laugh at what we used think. Refer to this article it can help a lot.


这篇关于从动态创建的文本框中获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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