惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
Visual Studio Blog
量子位
大猫的无限游戏
大猫的无限游戏
Hugging Face - Blog
Hugging Face - Blog
S
SegmentFault 最新的问题
Blog — PlanetScale
Blog — PlanetScale
月光博客
月光博客
Google DeepMind News
Google DeepMind News
小众软件
小众软件
WordPress大学
WordPress大学
宝玉的分享
宝玉的分享
MongoDB | Blog
MongoDB | Blog
B
Blog RSS Feed
博客园 - Franky
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
B
Blog
博客园 - 聂微东
The GitHub Blog
The GitHub Blog
Recent Announcements
Recent Announcements
Y
Y Combinator Blog
Microsoft Security Blog
Microsoft Security Blog
雷峰网
雷峰网
Jina AI
Jina AI
酷 壳 – CoolShell
酷 壳 – CoolShell

博客园 - Sunny

开发人员一定要加入收藏夹的网站(转载) 我写的一个小程序 tabs研究 dotnetnuke( modules /tabs )权限研究 数据库知识2 数据库学习1 asp.net缓存、企业程序库缓存及格式化日期 - Sunny - 博客园 可爱的外甥女 dotnetnuke 里profile 的使用 怎样写自己的ConfigurationHandler(DOTNETNUKE 研究)? 编写upload程序 多国技术总结 远程教育系统总结 项目(多国技术) 总结 循环遍历一个控件方法 页面验证码程序(C#) string与stringbuilder flyweigth享元设计模式与委托总结 dotnetnuke 本地化技术杂谈
怎样获取获取GridView里面的label
Sunny · 2006-03-26 · via 博客园 - Sunny

GridView如下:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound">
      
<Columns>
 
<asp:BoundField DataField="errornum" HeaderText="错误数" />   //control[0]
 
<asp:BoundField DataField="rightnum" HeaderText="正确数" />   //controls[1]
 
<asp:BoundField DataField="adatatime" HeaderText="答题时间" /> //control[2]
    
  
<asp:TemplateField HeaderText="正确率">                                       //control[3]
      
<ItemTemplate>
       
<asp:Label ID="bfb" runat=server>aa</asp:Label>
      
</ItemTemplate>
      
</asp:TemplateField>

      
</Columns>
      
</asp:GridView>

我们现在要在 GridView1_RowDataBound(object sender, GridViewRowEventArgs e)事件中获取
<asp:Label ID="bfb" runat=server>aa</asp:Label>的引用
代码如下:

1     protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
2     {
3         if (e.Row.RowType == DataControlRowType.DataRow)
4         {
5              Label bfb = (Label)e.Row.Controls[3].FindControl("bfb");
  
6 
7         }
8 
9     }

上面的第五行代码 Label bfb = (Label)e.Row.Controls[3].FindControl("bfb");
也可以改为     Label bfb = (Label)e.Row.Controls[3].Controls[1];
因为

<asp:TemplateField HeaderText="正确率">  </asp:TemplateField>
包含
-System.Web.UI.LiteralControl      //controls[0]
-System.Web.UI.WebControls.Label  //controls[1]
-System.Web.UI.LiteralControl      //control[2]
三个子控件,所以label 的index为1。