[英]Save the value of Dynamically created textbox
UPDATE1:我正在研究此鏈接。 在PostBack期間保留動態創建的控件-VB.net它可以解釋我需要知道的內容。 上面鏈接中的注釋提到使用相同的ID重新創建控件。 ASP.net將自動保留該值。 然后,我將能夠找到控件並獲得鍵入的值。 .. 謝謝 ..
UPDATE2:感謝Win的注釋和下面的鏈接,我想我可以解決這個問題。 稍后將確認並發布答案。
我必須道歉,好像有成千上萬的類似問題。 但是,在閱讀了許多問答之后,我似乎仍然無法使我的簡單頁面正常工作。 我對此很陌生。 請允許我再問一次。
我有一個非常簡單的ASPX頁面,其中包含一個下拉列表,一個表格和一個按鈕。 使用數據表填充下拉列表(數據表來自SQL表)。 該表用於動態創建的文本框的容器。 和一個按鈕進行更新。 以下是我背后的ASPX和vb.net代碼的代碼段
我面臨的問題是page.findcontrol無法找到動態創建的控件。 我模糊地理解這是由於回發page_load與page_init而引起的問題。 但是在我閱讀了所有教程之后,我仍然沒有完全理解:( ..您能幫忙如何完成這項工作嗎?
非常感謝
額外信息:無論如何,我還是嘗試執行建議中的建議,並在頁面加載或初始化時重新創建了控件,但是當我重新創建該控件時,它在文本框中會有什么值? 這是用戶看到的流程。
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
注意:button1單擊事件尚未完成。 但是一旦我弄清楚了如何捕獲文本框中鍵入的數據,我就可以完成其余工作。 我的主要問題是我無法獲取txtEmployee_1的值,也無法找到控件。 我似乎在錯誤的時間使用了findcontrol,或者在錯誤的時間初始化了控件。
這是我的桌子
╔════════════╦══════════════╦════════════╗
║ LastName ║ DepartmentID ║ EmployeeID ║
╠════════════╬══════════════╬════════════╣
║ Rafferty ║ 31 ║ 1 ║
║ Jones ║ 33 ║ 2 ║
║ Heisenberg ║ 33 ║ 3 ║
║ Robinson ║ 34 ║ 4 ║
║ Smith ║ 34 ║ 5 ║
╚════════════╩══════════════╩════════════╝
我正在我正在處理的應用程序中執行類似的操作。 讓我給你一個非常簡單的例子,我認為這會有所幫助:
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
大概的概念:
我通過使用解決了這個問題
For Each s As String In Request.Params
代替
DirectCast(Page.FindControl("txtEmployee_1"), TextBox).ToString
使用Request.Params,我可以捕獲用戶鍵入的內容
For Each s As String In Request.Params
If s.contains(txtEmployee_1) THEN txtUserTyped = Request(s)
Next
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.