在GridView文本框中获取值TemplateField [英] Get Values in GridView textbox TemplateField

查看:187
本文介绍了在GridView文本框中获取值TemplateField的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个称为Default.aspx的asp.net页面,它的主页是Site.master.在Default.aspx中,我添加了一个带有3个数据绑定字段和1个Templatefield的gridview,然后将一个TextBox拖动到此templatefield内.

I have an asp.net page called Default.aspx, and it's master page is Site.master. In the Default.aspx, i added a gridview with 3 databound fields and 1 Templatefield, and then, dragged a TextBox inside this templatefield.

我正在尝试使用FindControl方法获取此gridview中每一行的文本框值,但是它返回Nothing.

I'm trying to get the textbox values for each row in this gridview, using the FindControl method, but it's returning Nothing.

这是我用来检索这些值的代码:

Here is the code that i'm using to retrieve these values:

For Each gvr As GridViewRow In GridView1.Rows

        Dim tb As TextBox = DirectCast(gvr.FindControl("TextBox1"), TextBox)
        Dim txt As String = tb.Text
        MsgBox(txt)

    Next

注意:我正在使用masterPages,并且我认为这是导致问题的原因.

Note: I'm using masterPages, and i'm thinking this is causing the problem.

在Page_load事件中,为了绑定gridview,我正在使用以下代码:

In the Page_load event, to bound the gridview, i'm using the code:

        GridView1.DataSource = f.xDa
        GridView1.DataBind()

在Button1中,我添加了代码:

In the Button1, I've added the code:

For Each gvr As GridViewRow In GridView1.Rows

        Dim tb As TextBox = DirectCast(gvr.FindControl("TextBox1"), TextBox)
        Dim txt As String = tb.Text
        MsgBox(txt)

    Next

但是我总是得到一个空的文本框.

But i'm Always getting an empty textbox.

谢谢大家!

推荐答案

您需要将Page_Load代码更新为此:

You need to update your Page_Load code to this:

If Not IsPostBack Then
    GridView1.DataSource = f.xDa
    GridView1.DataBind()
End If

当代码到达Button_Click事件时,它已经用数据库中的数据重新填充了GridView(覆盖了用户在TextBox中键入的内容).

By the time your code gets to the Button_Click event, it has already repopulated the GridView with data from your database (overwriting what your user typed into the TextBox).

我在上面添加的代码使数据仅在第一次加载-然后ASP.NET viewstate进行处理以确保GridView的状态保持最新.

The code I've added above causes the data to be loaded only the first time - then the ASP.NET viewstate handles making sure the state of the GridView is kept up-to-date.

这篇关于在GridView文本框中获取值TemplateField的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆