0
A
回答
2
随着绑定列和只读模式下可以使用GridView1.Rows[x].Cells[x].Text
但编辑模式下,您必须使用控件集合得到控制的参考。此方法返回一个Control
对象。
Control control=GridView1.Rows[x].Cells[x].Controls[0]; // later you may cast it to appropriate control class.
如果模板字段使用,那么你必须发出从Cells
收集FindControl
方法,以获取基于其ID
控制的参考。您也可以使用Cells[x].Controls
集合。
Control control=GridView1.Rows[x].Cells[x].FindControl("ID_Of_Control"); // later you may cast it to appropriate control class.
编辑:
这还可能的是可以有具有横跨模板列相同的名称/ ID的一个或多个控制。在这种情况下,您不能使用FindControl
方法。
例子:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="Button1" runat="server" Text="Button" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="Button1" runat="server" Text="Button" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
我们获得按钮,并从第二行和第一小区改变其文本:
Button btn = GridView1.Rows[1].Cells[0].Controls[1] as Button ;
if(btn!=null)
btn.Text = "Hello";
3
如果它是一个BoundField,你可以做
gv.Rows[1].Cells[1].Text;
如果它是一个TemplateField,你必须得到有你想要的值控制。
Label L = gv.Rows[1].FindControl("yourcontrolId") as Label;
L.Text;
相关问题
- 1. 获取知道索引的Collection元素?
- 2. 如何获取单元格的值,如果我知道的GridView
- 3. 获取GridView的行索引
- 4. 如何在GridView中获取单元格值(不使用单元格索引)
- 5. 获取指定单元格的行和列索引
- 6. 获取来自81元素索引数组的行和列值
- 7. 获得具有给定行索引和列索引的datagrid单元格的值
- 8. 使用JavaScript获取Gridview单元值使用JavaScript获取Gridview单元格值
- 9. 通过字符串而不是索引获取Gridview单元格的列引用
- 10. GridView单元格的单元格索引
- 11. 获取GridView中选定行的索引
- 12. C#DataTable,按行/列索引获取值
- 13. 如何获得对数组中单元格的引用,只知道它的值?
- 14. 从系列中获取索引和值
- 15. 如何获取Datagrid行索引以获取上一行的单元格值
- 16. 如何在gridview中获取单元格索引
- 17. 如何按C#中Datatable的单元格值获取行索引
- 18. 获取索引给定的行和列
- 19. Gridview丢失行索引值
- 20. 获取列值GridView
- 21. 当我点击一个gridview单元格时获取行和列的名称值?
- 22. 从gridview单元格获取值#
- 23. 从GridView单元格获取TextBox值
- 24. 从特定的Gridview单元获取值
- 25. 在Gridview中获取单元格的值
- 26. 在gridview中获取单元格的值
- 27. 从ASP.NET GridView获取值单元格
- 28. 如何从gridview获取单元格值?
- 29. 从Gridview单元获取价值
- 30. 如果我知道单元格的行和列索引,如何更改HTML表格单元格中的属性?
嘿,thx,但我实际上应用此代码之前,它不工作...我使用模板字段fyi ...任何替代? – rofans91 2011-12-27 02:18:54
@ Rofans.NET - 有两种方法可以从TemplateField单元格获取控件 - FindControl方法和Controls集合。 – adatapost 2011-12-27 02:24:48
@AVD:'FindControl'找到控件而不会提取单元格索引,对吧? – naveen 2011-12-27 02:28:37