在Table中动态生成的TextBox无法在PostBack时刷新数据
问题如题,请高手帮忙看看问题在哪?
1.不用Textbox时,在点击DropDownList时,页面PostBack后,Table是可以刷新数据的(代码如下)
test.aspx:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="test.aspx.vb" Inherits="test" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True">
<asp:ListItem Value="1元">苹果</asp:ListItem>
<asp:ListItem Value="2元">桔子</asp:ListItem>
</asp:DropDownList>
<asp:Table ID="Table1" runat="server">
</asp:Table>
</div>
</form>
</body>
</html>
test.aspx.vb
Partial Class test
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim i As Integer
Dim myRow As New TableRow
For i = 0 To 1
Dim myCell As New TableCell
Select Case i
Case 0
myCell.Text = DropDownList1.SelectedItem.Text
Case 1
myCell.Text = DropDownList1.SelectedItem.Value
End Select
myRow.Cells.Add(myCell)
Next
Table1.Rows.Add(myRow)
End Sub
End Class
2.在Table中动态生成了TextBox,以便我编辑修改的需要,但是在点击DropDownList时,页面PostBack后,页面显示的数据不变(代码如下):
text1.aspx
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="test1.aspx.vb" Inherits="test1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True">
<asp:ListItem Value="1元">苹果</asp:ListItem>
<asp:ListItem Value="2元">桔子</asp:ListItem>
</asp:DropDownList>
<asp:Table ID="Table1" runat="server">
</asp:Table>
</div>
</form>
</body>
</html>
test1.aspx.vb
Partial Class test1
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim i As Integer
Dim myRow As New TableRow
For i = 0 To 1
Dim myCell As New TableCell
Select Case i
Case 0
Dim myTextBox As New TextBox
myTextBox.Text = DropDownList1.SelectedItem.Text
myCell.Controls.Add(myTextBox)
Case 1
Dim myTextBox As New TextBox
myTextBox.Text = DropDownList1.SelectedItem.Value
myCell.Controls.Add(myTextBox)
End Select
myRow.Cells.Add(myCell)
Next
Table1.Rows.Add(myRow)
End Sub
End Class