繁体   English   中英

gridview findcontrol 返回空“”

[英]gridview findcontrol returning empty “”

我正在尝试使用此代码从 gridview 中的文本框中读取

    protected void Button1_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow row in GridView1.Rows)
        {
            string textBoxText = ((TextBox)row.FindControl("numTC")).Text;
            Response.Write(textBoxText);

        }
    }

此代码不断返回“”(空)

知道为什么会这样吗?

谢谢

确保您没有在页面的 PostBack 上重新绑定 GridView。 这可能是问题所在。

编辑

确保绑定GridView的代码在以下代码中:

C#

if ( !Page.IsPostBack ){
    // Code to bind the control
}

VB

If Not Page.IsPostBack Then
    ' Code to bind the control
End If

否则会发生什么是控件被“重建”并且值都丢失在 TextBox 的

更新:

出于测试目的,请尝试执行 GridView1.DataBind(); 在您的方法开始时。

尝试像这样调试:

在 Button1_Click 方法的末尾设置断点。

以调试模式 (F5) 运行该站点。

当执行在 Button1_Click 结束时停止时,打开位于屏幕底部的即时 Window。

在那里输入:

GridView1.Rows 并查看它是否包含它应该包含的行数。

应该是这样的:

System.Web.UI.WebControls.GridViewRowCollection} 计数:53 <-- 行数

如果它确实返回超过 0 行,则输入:

GridView1.Rows[0].Controls 并查看它是否返回一行上正确数量的控件。

我可以使用 GridView1.Rows[2].Controls[n] 直接访问一行上的控件,其中 n 是该行中控件的顺序。

也尝试 (TextBox)GridView1.Rows[0].FindControl("numTC") 看看它返回什么。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM