chao's profile清凉水都PhotosBlogListsMore Tools Help

Blog


    September 17

    得GridView的EmptyDataTemplate中的控件 - kingwkb的专栏 - CSDNBlog

     

    转载 得GridView的EmptyDataTemplate中的控件收藏

    新一篇: Visual studio 2005中Source View中不能切换到Design View的解决方法 | 旧一篇: 关于进行Asp.net窗体验证的过程说明
    这个问题的提出,是我想在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()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

    其中的txbNewCltName和btnAddNew都是放在EmptyTemplate里的控件。
    显示的结果是:
    ChildTable EmptyDataRow 新添加的内容

    可以知道,第一层Control是Table,第二层Control是Row.

    得GridView的EmptyDataTemplate中的控件 - kingwkb的专栏 - CSDNBlog

    Comments

    Please wait...
    Sorry, the comment you entered is too long. Please shorten it.
    You didn't enter anything. Please try again.
    Sorry, we can't add your comment right now. Please try again later.
    To add a comment, you need permission from your parent. Ask for permission
    Your parent has turned off comments.
    Sorry, we can't delete your comment right now. Please try again later.
    You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
    Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
    Complete the security check below to finish leaving your comment.
    The characters you type in the security check must match the characters in the picture or audio.

    To add a comment, sign in with your Windows Live ID (if you use Hotmail, Messenger, or Xbox LIVE, you have a Windows Live ID). Sign in


    Don't have a Windows Live ID? Sign up

    Trackbacks

    The trackback URL for this entry is:
    http://ebakeshop.spaces.live.com/blog/cns!1A66D6C80502E805!316.trak
    Weblogs that reference this entry
    • None