UPDATE1:我正在研究此链接。在PostBack期间保留动态创建的控件-VB.net它可以解释我需要知道的内容。上面链接中的注释提到使用相同的ID重新创建控件。ASP.net将自动保留该值。然后,我将能够找到控件并获得键入的值。.. 谢谢 ..
UPDATE2:感谢Win的注释和下面的链接,我想我明白了。稍后将确认并发布答案。
我必须道歉,好像有成千上万的类似问题。但是,在阅读了许多问答之后,我似乎仍然无法使我的简单页面正常工作。我对此很陌生。请允许我再问一次。
我有一个非常简单的ASPX页面,其中包含一个下拉列表,一个表格和一个按钮。使用datatable填充下拉列表(datatable来自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 ║
╚════════════╩══════════════╩════════════╝
我通过使用解决了这个问题
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
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句