得GridView的EmptyDataTemplate中的控件

這個問題的提出,是我想在Gridview中整理添加記錄的功能。
如果有數據的時候,我們可以把空的newTextBox放在FooterTemplate中,在程序裏可以用
Gridview1.FooterRow.FindControl("newTextBox")來取得這個控件,完成添加記錄的功能。
但如果數據表中沒有記錄,header和footer都不會顯示出來。 這時,只會顯示EmptyDataTemplate裏的內容。
奇怪的是EmptyDataRow並不是Gridview的成員,於是上面的方法不行了。
找了很久,找到這樣一個變通的辦法來獲得EmptyDataTemplate裏的控件:
Dim txbNew As TextBox = GridView1.Controls(0).Controls(0).FindControl("newTextBox")

很變態,但確實可行。

1    Protected Sub btnAddNew_Click(ByVal sender As ObjectByVal 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
其中的txbNewCltName和btnAddNew都是放在EmptyTemplate裏的控件。
顯示的結果是:
ChildTable EmptyDataRow 新添加的內容
可以知道,第一層Control是Table,第二層Control是Row.
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章