gridview里自定义个按钮修改内容找不到TEXTBOX得值

xiaoheilf 2010-09-21 07:13:19
gridview里自定义了个按钮修改内容
想获取TextBox里修改后的值,总是查找不到TextBox,但找Linkbotton都能找到啊,提示
未将对象引用设置到对象的实例
各种方法都想了 我好郁闷啊,搞了一天了 ,求大大懂的解决下好么
主要是实现验证用户是否登录然后动态显示编辑按钮执行编辑

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False" OnPageIndexChanging="GridView1_PageIndexChanging" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" OnSelectedIndexChanging="GridView1_SelectedIndexChanging" PageSize="3" DataKeyNames="tb_ID,tb_Title,tb_Content" OnRowCancelingEdit="GridView1_RowCancelingEdit" Width="660px" BackColor="White" BorderColor="White" BorderStyle="None" BorderWidth="5px" OnRowDataBound="GridView1_RowDataBound" OnRowCommand="GridView1_RowCommand">
<Columns>
<asp:TemplateField HeaderText="留言内容" >
<EditItemTemplate>
<table>
<tr>
<td>
留言者 :
<asp:Label ID="Label2" runat="server" Text=<%# Eval("tb_name") %>></asp:Label></td>
</tr>
<tr>
<td>
留言标题 <asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("tb_Title") %>'></asp:TextBox>
</td>
</tr>
<tr >
<td>
留言内容 :<asp:TextBox ID="TextBox6" runat="server" Text='<%# Bind("tb_content") %>'></asp:TextBox>
</td>
</tr>
<tr >
<td>
留言回复:<%# Eval("tb_mark") %>
</td>
</tr>
<tr>
<td>
 <asp:LinkButton ID="LinkButton3" Visible="false" runat="server">编辑</asp:LinkButton>
<asp:LinkButton ID="LinkButton4" Visible="false" runat="server">删除</asp:LinkButton>
<asp:Label ID="Label1" runat="server" Visible="false" Text=<%# Eval("tb_ID") %>> </asp:Label>
<asp:LinkButton ID="LinkButton5" Visible="true" OnClick="LinkButton5_Click" CommandName="Edit" CommandArgument=<%# Eval("tb_ID") %> runat="server">111</asp:LinkButton> 
<asp:LinkButton ID="LinkButton6" Visible="false" OnClick="LinkButton6_Click" CommandArgument=<%# Eval("tb_ID") %> runat="server" CommandName="Update">保存</asp:LinkButton> 
<asp:LinkButton ID="LinkButton7" Visible="false" OnClick="LinkButton7_Click" CommandArgument=<%# Eval("tb_ID") %> runat="server" CommandName="Cancel">取消</asp:LinkButton>
<asp:LinkButton ID="LinkButton8" Visible="false" OnClick="LinkButton8_Click" CommandArgument=<%# Eval("tb_ID") %> runat="server" CommandName="Delete">删除</asp:LinkButton>
</td>
<td style="width: 7px">
</td>
</tr>

</table>
</EditItemTemplate>

<ItemTemplate>
<table>
<tr>
<td style="width: 246px">
留言者 :
<asp:Label ID="Label2" runat="server" Text=<%# Eval("tb_name") %>></asp:Label></td>
</tr>
<tr>
<td style="width: 246px">
留言标题 :<asp:Label ID="Label3" runat="server" Text=<%# Eval("tb_title") %>></asp:Label>
</td>
</tr>
<tr >
<td style="width: 246px">
留言内容 :<asp:Label ID="Label4" runat="server" Text=<%# Eval("tb_content") %>></asp:Label>
</td>
</tr>
<tr >
<td style="width: 246px">
留言回复:<asp:Label ID="Label5" runat="server" Text=<%# Eval("tb_mark") %>></asp:Label>
</td>
</tr>
<tr>
<td style="width: 246px">
 <asp:LinkButton ID="LinkButton3" Visible="false" runat="server">编辑</asp:LinkButton>
<asp:LinkButton ID="LinkButton4" Visible="false" runat="server">删除</asp:LinkButton>
<asp:Label ID="Label1" runat="server" Visible="false" Text=<%# Eval("tb_ID") %>> </asp:Label>
<asp:LinkButton ID="LinkButton5" Visible ="true" OnClick="LinkButton5_Click" CommandName="Edit" runat="server" CommandArgument=<%# Eval("tb_ID") %>></asp:LinkButton>
<asp:LinkButton ID="LinkButton6" OnClick="LinkButton6_Click" CommandArgument=<%# Eval("tb_ID") %> runat="server" CommandName="Update"></asp:LinkButton>
<asp:LinkButton ID="LinkButton7" OnClick="LinkButton7_Click" CommandArgument=<%# Eval("tb_ID") %> runat="server" CommandName="Cancel"></asp:LinkButton>
<asp:LinkButton ID="LinkButton8" OnClick="LinkButton8_Click" CommandArgument=<%# Eval("tb_ID") %> runat="server" CommandName="Delete"></asp:LinkButton>
</td>
<td style="width: 7px">
</td>
</tr>

</table>
</ItemTemplate>
</asp:TemplateField>

</Columns>

</asp:GridView>





protected void LinkButton6_Click(object sender, EventArgs e)
{
for (int i = 0; i < GridView1.Rows.Count; ++i)
{

string xx= ((TextBox)GridView1.Rows[i].FindControl("TextBox5")).Text;
Response.Write(xx);
//string tbtstr = tbt1.Text;
// TextBox str = (TextBox)(GridView1.Rows[2].FindControl("TextBox5"));也不行
}
}
...全文
76 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
孟子E章 2010-09-21
  • 打赏
  • 举报
回复


TextBox5只是在编辑的行里才有的,不是每个都有
for (int i = 0; i < GridView1.Rows.Count; ++i)
这么遍历肯定是不行,你只能找你编辑的那行的index

可以参考这个
http://dotnet.aspx.cc/file/Add-Delete-Update-Edit-Data-With-Paging-in-GridView.aspx
PandaIT 2010-09-21
  • 打赏
  • 举报
回复
GridView.Rows[].Cells[].FindControl//换成这个

62,272

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧