日期:2014-05-20  浏览次数:20780 次

[紧急求助]关于动态生成的控件在页面回发后无法取得的问题!
在PAGE_LOAD的时候生成了一批TEXTBOX,但在执行了BUTTON事件后无法取得这些TEXTBOX,求大虾们帮帮忙!取TEXTBOX时候的代码如下:

foreach(System.Object   txt   in   pg.Controls[1].Controls)      
{      
System.Type   sType   =   txt.GetType();    
if(sType.ToString()   ==   "System.Web.UI.WebControls.TextBox ")
{
if(((System.Web.UI.WebControls.TextBox)txt).ID.ToString()   ==   "0_1 ")
{
Response.Write(((System.Web.UI.WebControls.TextBox)txt).Text.ToString());      
}
}
}

------解决方案--------------------
你应该把page_load的代码贴出来,估计有几个原因:
1. pg.Controls[1].Controls) 只是遍历“pg.Controls[1]”的子控件。
2. if(sType.ToString() == "System.Web.UI.WebControls.TextBox ") 改成 if(txt is TextBox) 试试
3. page_load里生成的那部分不要嵌在“if(!IsPostBack)”里面

------解决方案--------------------
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim table As New Table
table.ID = "mytable "
Page.Controls(1).Controls.Add(table)
Dim tr As New TableRow
table.Rows.Add(tr)
Dim td1 As New TableCell
tr.Cells.Add(td1)
Dim td2 As New TableCell
tr.Cells.Add(td2)


Dim t1 As New TextBox
td1.Controls.Add(t1)
Dim t2 As New TextBox
td2.Controls.Add(t2)


End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim table As Table = Page.Controls(1).FindControl( "mytable ")
For Each tr As TableRow In table.Rows
For Each td As TableCell In tr.Cells
Dim tt As TextBox = td.Controls(0)
Response.Write(tt.Text & " <br> ")
Next
Next
End Sub
===============================
vb的,自己改成C#就是了。道理就是这个样子滴~~~~