gridview findcontrol 返回空“”
我正在尝试使用此代码从 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);
}
}
此代码不断返回“”(空)
知道为什么会发生这种情况吗?
谢谢
i am trying to read from a textbox within a gridview by using this code
protected void Button1_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
string textBoxText = ((TextBox)row.FindControl("numTC")).Text;
Response.Write(textBoxText);
}
}
this code keeps returning "" (empty)
any idea why this is hapenning?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
确保您没有在页面的 PostBack 上重新绑定 GridView。这可能就是问题所在。
编辑
确保绑定 GridView 的代码位于以下代码内:
C#
VB
否则,会发生控件被“重建”并且值在 TextBox 中全部丢失的情况
Make sure that you are not re-binding the GridView on the PostBack of the page. This may be the issue.
EDITS
Make sure that the code for Binding the GridView is within the code below:
C#
VB
Otherwise what happens is that the controls is "rebuilt" and the values are all lost within the TextBox's
更新:
出于测试目的,请尝试执行 GridView1.DataBind(); 在
尝试像这样进行调试:
Button1_Click 方法的末尾设置一个断点。
以调试模式 (F5) 运行站点。
当执行在 Button1_Click 结束时停止时,打开位于屏幕底部的立即窗口。
在那里键入:
GridView1.Rows 并查看它是否包含应有的行数。
应该是这样的:
System.Web.UI.WebControls.GridViewRowCollection}
Count: 53 <-- 行数
如果确实返回多于 0 行,则键入:
GridView1.Rows[0].Controls 并查看它是否返回一行上正确的控件数。
我可以使用 GridView1.Rows[2].Controls[n] 直接访问行上的控件,其中 n 是行中控件的顺序。
另请尝试 (TextBox)GridView1.Rows[0].FindControl("numTC") 并查看它返回的内容。
UPDATE:
For testing purposes, try doing GridView1.DataBind(); at the begining of your method.
Try debugging like this:
Set a breakpoint at the end of the Button1_Click method.
Run the site in debug mode (F5).
When executions stops at end of Button1_Click, open the Immediate Window located at bottom of screen.
Type there:
GridView1.Rows and see if it contains the number of rows it should.
Should be something like:
System.Web.UI.WebControls.GridViewRowCollection}
Count: 53 <-- number of rows
If it does return more than 0 rows then type:
GridView1.Rows[0].Controls and see if it returns the right number of controls on a row.
I could access controls on a row directly using GridView1.Rows[2].Controls[n] where n is the order of the control in the row.
Also try (TextBox)GridView1.Rows[0].FindControl("numTC") and see what it returns.