简体   繁体   中英

Save the value of Dynamically created textbox

UPDATE1: I am Investigating this link. Retaining dynamically created controls during PostBack - VB.net It may explained what I need to know. The comment in above link mentioned to recreate the control using the same ID. ASP.net will automatically retain the value. I will then be able to find the control and get the typed value. .. thanks ..

UPDATE2: Thanks to Win's comment bellow and a link above, I think I figure this out. Will confirm and post answer later.

I must apologize it seems like there is a thousand of similar question to this. But after reading many question and answer I still cannot seem to make my simple page working. I am very new at this. Please allow me to ask this again.

I have this very simple ASPX page with one dropdown, a table and one button. The drop down is populated using datatable (datatable is from SQL table). the table is used for a container of a dynamically created textbox. and a button to do the updating. below is my code snippet for the ASPX and vb.net code behind

The problem that I am facing is that the page.findcontrol is unable to locate the control that was dynamically created. I vaguely understand that is a problem due to post back, page_load vs page_init. but I still don't have full understanding after all the tutorials I read :( .. could you please help how to make this work?

thank you so much

Extra info: I tried to do what is suggested in the coment anyway, and I recreate the control on page load or init, but what value would be in the textbox when I re create it? here is the flow as what the user see.

  1. step0 The first time the page load, no dynamic textbox yet
  2. Step1 the user select value 34
  3. step2 autopostback selectedindexchanged fired and value 34 passed back to the server and return 2 names joe and jack, it create dynamic textbox_1 with joe and dynamic textbox_2 with jack.
  4. Step3 the user typed the value jane in textbox_1. and click button1.
  5. Step4 button1 click event fired and I am trying to capture the word jane in textbox_1 but I cannot. because I can't find the control of textbox_1 due to timing or my limited knowledge. this is s where I need some help.

ASPX

<body>
    <form id="form1" runat="server">
    <div>

        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True">
        </asp:DropDownList>
        <br />
        <br />
        <asp:Table ID="Table1" runat="server">
        </asp:Table>
        <br />
        <asp:Button ID="Button1" runat="server" Text="Button" />

    </div>
    </form>
</body>

VB.net

Imports System.Data.SqlClient
Public Class DynamicControl
    Inherits System.Web.UI.Page
    Dim connString As String = "Server=Local;Database=SampleData;Trusted_Connection=True;Max Pool Size=1000"
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            getData()
        End If
    End Sub
    Private Sub getData()
        Dim dt As New DataTable
        Dim SQLString As String
        Dim conn As New SqlConnection
        conn.ConnectionString = connString
        conn.Open()
        SQLString = "select DepartmentID from Employee where departmentid is not null group by departmentid"
        getDataBySQLConn(SQLString, dt, conn)
        DropDownList1.DataSource = dt
        DropDownList1.DataValueField = "DepartmentID"
        DropDownList1.DataTextField = "DepartmentID"
        DropDownList1.DataBind()
    End Sub
    Protected Sub DropDownList1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles DropDownList1.SelectedIndexChanged
        Dim dt As New DataTable
        Dim SQLString As String
        Dim conn As New SqlConnection
        conn.ConnectionString = connString
        conn.Open()
        SQLString = "select employeeid,lastname from Employee where departmentid =" & DropDownList1.SelectedValue.ToString
        getDataBySQLConn(SQLString, dt, conn)
        For Each rows As DataRow In dt.Rows
            Dim tTextBox As New TextBox
            Dim tr As New TableRow
            Dim tc As New TableCell
            tTextBox.ID = "txtEmployee_" & rows("EmployeeID")
            tTextBox.Text = rows("lastname")
            tr.Cells.Add(tc)
            tc.Controls.Add(tTextBox)
            Table1.Rows.Add(tr)
        Next
    End Sub
    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        'Update Employee set lastname = '' where employeeID = 2
        Dim iEmployeeID As Integer
        Dim sLastName As String
        Dim tTextBox As TextBox
        iEmployeeID = DirectCast(Page.FindControl("txtEmployee_1"), TextBox).ToString
        tTextBox = Page.FindControl("txtEmployee_1")
        sLastName = tTextBox.Text
    End Sub
End Class

Note: the button1 click event is not complete. but once I can figure out how to capture the data being typed in the textbox I will be able to get the rest done. My main problem is I am unable to get the value of the txtEmployee_1 nor can I locate the controls. I seem to have used findcontrol at the wrong time , or initialized the control at the wrong time.

And this is my table

╔════════════╦══════════════╦════════════╗
║  LastName  ║ DepartmentID ║ EmployeeID ║
╠════════════╬══════════════╬════════════╣
║ Rafferty   ║           31 ║          1 ║
║ Jones      ║           33 ║          2 ║
║ Heisenberg ║           33 ║          3 ║
║ Robinson   ║           34 ║          4 ║
║ Smith      ║           34 ║          5 ║
╚════════════╩══════════════╩════════════╝

I'm doing something similar in an application I am working on. Let me give you a very simple example that I think will help:

Default.aspx:

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="WebApplication3._Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="Button1" runat="server" Text="Make textbox" />
        <asp:Button ID="Button2" runat="server" Text="Get textbox text" />
    </div>
    </form>
</body>
</html>

Default.aspx.vb:

Public Class _Default
    Inherits System.Web.UI.Page

    Protected controlBag As New Dictionary(Of String, Control)
    Protected Const textBox1Name As String = "textBox1"
    Protected Const textBox1EnabledName As String = "textBox1Status"

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If ViewState(textBox1EnabledName) = True Then
            makeTextBox()
        End If
    End Sub

    Private Sub makeTextBox()
        Dim x As New TextBox
        x.ID = textBox1Name
        If Not controlBag.ContainsKey(textBox1Name) Then
            form1.Controls.Add(x)
            controlBag.Add(x.ID, x)
        End If
    End Sub

    Private Sub removeTextBox()
        If controlBag.ContainsKey(textBox1Name) Then
            form1.Controls.Remove(controlBag(textBox1Name))
        End If
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        ViewState(textBox1EnabledName) = True
        makeTextBox()
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim x As New Literal

        If controlBag.ContainsKey(textBox1Name) Then
            x.Text = String.Format("Here's the text: {0}", DirectCast(controlBag(textBox1Name), TextBox).Text)
            removeTextBox()

            ViewState(textBox1EnabledName) = False
        Else
            x.Text = "The textbox does not exist"
        End If

        form1.Controls.Add(x)
    End Sub
End Class

General idea:

  1. Build dynamic controls. Add them to the page, but also add them to a dictionary. I use Dictionary(Of String, Control). This facilitates lookup of their data later. You'll need to add logic on when to create controls and what to bind them to.
  2. On postback, you must recreate the exact same controls, even if you don't want them on the final page output. You must do this before accessing their data. Add them to the page control collection and don't forget to add them to your dictionary.
  3. Use your dictionary and the name of the control id to look it up and DirectCast it to your desired control type.
  4. Do whatever you needed to do with the data from the controls. You mentioned textboxes, so you would use the .Text property of your casted control.
  5. If you are done with the control, you can remove it from the page control collection. Update whatever logic you used to not create the textbox again.

I solved this by using

For Each s As String In Request.Params

Instead of

DirectCast(Page.FindControl("txtEmployee_1"), TextBox).ToString

Using Request.Params I am able to capture what the user typed as folows

For Each s As String In Request.Params
  If s.contains(txtEmployee_1) THEN txtUserTyped = Request(s)
Next

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM