这个问题的提出,是我想在Gridview中整理添加记录的功能。
如果有数据的时候,我们可以把空的newTextBox放在FooterTemplate中,在程序里可以用
Gridview1.FooterRow.FindControl("newTextBox")来取得这个控件,完成添加记录的功能。
但如果数据表中没有记录,header和footer都不会显示出来。 这时,只会显示EmptyDataTemplate里的内容。
奇怪的是EmptyDataRow并不是Gridview的成员,于是上面的方法不行了。
找了很久,找到这样一个变通的办法来获得EmptyDataTemplate里的控件:
Dim txbNew As TextBox = GridView1.Controls(0).Controls(0).FindControl("newTextBox")
如果有数据的时候,我们可以把空的newTextBox放在FooterTemplate中,在程序里可以用
Gridview1.FooterRow.FindControl("newTextBox")来取得这个控件,完成添加记录的功能。
但如果数据表中没有记录,header和footer都不会显示出来。 这时,只会显示EmptyDataTemplate里的内容。
奇怪的是EmptyDataRow并不是Gridview的成员,于是上面的方法不行了。
找了很久,找到这样一个变通的办法来获得EmptyDataTemplate里的控件:
![](https://andydavis.cnblogs.com/Images/OutliningIndicators/None.gif)
很变态,但确实可行。
1
Protected Sub btnAddNew_Click(ByVal sender As Object, ByVal e As System.EventArgs)
2
Label1.Text = TypeName(GridView1.Controls(0)).ToString + " "
3
Dim EmptyChildTable As Table = GridView1.Controls(0)
4
Dim EmptyGridView As GridViewRow = EmptyChildTable.Rows(0)
5
Label1.Text = Label1.Text + EmptyGridView.RowType.ToString + " "
6
Dim txbNewCltName As TextBox = EmptyGridView.FindControl("txbNewCltName")
7
Label1.Text = Label1.Text + txbNewCltName.Text
8
End Sub
![](https://andydavis.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
2
![](https://andydavis.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
3
![](https://andydavis.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
4
![](https://andydavis.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
5
![](https://andydavis.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
6
![](https://andydavis.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
7
![](https://andydavis.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
8
![](https://andydavis.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
其中的txbNewCltName和btnAddNew都是放在EmptyTemplate里的控件。
显示的结果是:
ChildTable EmptyDataRow 新添加的内容
显示的结果是:
ChildTable EmptyDataRow 新添加的内容
可以知道,第一层Control是Table,第二层Control是Row.