Control.FindControl (String):在当前的命名容器中搜索带指定 id参数的服务器控件。

有点类似javascript中的getElementById(string);

简单的例子:

 <form id="form1" runat="server">

     <div>

         <asp:TextBox ID="TextBox1" runat="server">TextBox</asp:TextBox>

         <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />

         <br />

         <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

      </div>

 </form>

如果需要获得页面中的"TextBox1",代码中可以使用this.TextBox1来引用,这里我们使用FindControl:

 protected void Button1_Click(object sender, EventArgs e)

   {

       //Control c = this.FindControl("TextBox1");

       //TextBox tb= (TextBox)c;

       //FindControl返回的是一个Control类型的控件,需要强制类型转化成TextBox类型

       TextBox tb=(TextBox)this.FindControl("TextBox1");

       this.Label1.Text = tb.Text;
    }

当TextBox1放到其他控件里应该怎么查找呢?

<div>

        <asp:Panel ID="Panel1" runat="server" Height="50px" ;125px">

           <asp:TextBox ID="TextBox1" runat="server">TextBox</asp:TextBox>

           <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

           <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />

        </asp:Panel>

    </div>

当TextBox1放到Panel里,似乎没什么影响 TextBox tb=(TextBox)this.FindControl("TextBox1"),

当查看生存页面的HTML代码是发现,TextBox的ID并没有改变,所以可以获得TextBox1。

<div>

       <div id="Panel1" style="height:50px;;">

          <input name="TextBox1" type="text" value="TextBoxdsd" id="TextBox1" />

          <span id="Label1">TextBoxdsd</span>

          <input type="submit" name="Button1" value="Button" id="Button1" />

        </div>

</div>

当TextBox1放到DataGrid中

 <asp:DataGrid ID="dg1" runat="server" OnSelectedIndexChanged="dg1_SelectedIndexChanged">

        <Columns>

           <asp:TemplateColumn>

              <ItemTemplate>

                 <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

              </ItemTemplate>

            </asp:TemplateColumn>

          <asp:ButtonColumn CommandName="Select" Text="选择"></asp:ButtonColumn>

        </Columns>

   </asp:DataGrid>

这时候this.FindControl("TextBox1")==null,无法获得TextBox1,查看生成页面HTML发现,页面有多个

<input name="dg1$ctl02$TextBox1" type="text" id="dg1_ctl02_TextBox1" />

<input name="dg1$ctl03$TextBox1" type="text" id="dg1_ctl03_TextBox1" />

TextBox1隐藏了,给DataGrid添加选择列,通过以下方法获得被选择行的TextBox1

protected void dg1_SelectedIndexChanged(object sender, EventArgs e)

  {

        Control c = this.dg1.Items[this.dg1.SelectedIndex].FindControl("TextBox1");

        //Control c = this.dg1.SelectedItem.FindControl("TextBox1");

        TextBox tb = (TextBox)c;

        tb.Text = "TextBox";

    }

 protected void dg1_EditCommand(object source, DataGridCommandEventArgs e)

  {

        TextBox tb = (TextBox)e.Item.FindControl("TextBox1");

        this.Label1.Text = tb.Text.ToString();

  }

如果是在DataGrid的页眉和页脚:

 ((TextBox)].Controls[].FindControl("TextBoxH")).Text = "Head";

 ((TextBox)].Controls[].Controls.Count -].FindControl("TextBoxF")).Text = "Footer";

TextBox1在Repeater中

   <asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1" OnItemCommand="Repeater1_ItemCommand">

          <ItemTemplate>

            <asp:TextBox ID="TextBox1" runat="server" Text=""></asp:TextBox><%#DataBinder.Eval(Container.DataItem,"ProductName")%>

            <asp:Button ID="btn"OnClick="btn_click" runat="server" Text="dddd" /><br />

          </ItemTemplate>

   </asp:Repeater>

通过按钮来获得TextBox1:

   <asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1" OnItemCommand="Repeater1_ItemCommand">

          <ItemTemplate>

            <asp:TextBox ID="TextBox1" runat="server" Text=""></asp:TextBox><%#DataBinder.Eval(Container.DataItem,"ProductName")%>

            <asp:Button ID="btn"OnClick="btn_click" runat="server" Text="dddd" /><br />

          </ItemTemplate>

   </asp:Repeater>

或者

foreach (RepeaterItem item in this.Repeater1.Items)

 {

       ((TextBox)item.FindControl("TextBox1")).Text = "Text2";

  }

自定义控件里的TextBox1

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="WebUserControl" %>

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

引用<uc1:WebUserControl ID="WebUserControl1" runat="server" />

获取TextBox1:

((TextBox)this.WebUserControl1.FindControl("TextBox1")).Text = "userc";

模板页访问页面TextBox1

        //模板页的TextBox1

        TextBox tbM = (TextBox)this.FindControl("TextBox1");

        //页面中的TextBox1

        TextBox tbC = (TextBox)this.FindControl("ContentPlaceHolder1").FindControl("TextBox1");

        tbC.Text = tbM.Text;

页面使用模板页的TextBox1

       //模板页的TextBox1

        TextBox tbM = (TextBox)Master.FindControl("TextBox1");

        //本页面的TextBox1

        //错误的方法:TextBox tbC = (TextBox)this.FindControl("TextBox1");

        TextBox tbC = (TextBox)Master.FindControl("ContentPlaceHolder1").FindControl("TextBox1");

        tbM.Text = tbC.Text.ToString();

WebForm FindControl的使用方法的更多相关文章

  1. ASP.NET -- WebForm -- HttpRequest类的方法和属性

    ASP.NET -- WebForm --  HttpRequest类的方法和属性 1. HttpRequest类的方法(1) BinaryRead: 执行对当前输入流进行指定字节数的二进制读取. ( ...

  2. ASP.NET -- WebForm -- HttpResponse 类的方法和属性

    ASP.NET -- WebForm -- HttpResponse 类的方法和属性 1. HttpResponse 类的方法 (1) AddCacheDependency: 将一组缓存依赖项与响应关 ...

  3. webform 页面传值的方法总结

    ASP.NET页面之间传递值的几种方式   页面传值是学习asp.net初期都会面临的一个问题,总的来说有页面传值.存储对象传值.ajax.类.model.表单等.但是一般来说,常用的较简单有Quer ...

  4. FindControl的使用方法

    Control.FindControl (String):在当前的命名容器中搜索带指定 id参数的服务器控件.(有点类似javascript中的getElementById(string)) 简单的例 ...

  5. WebForm页面间传值方法(转)

    Asp.NET WEB FORMS 给开发者提供了极好的事件驱动开发模式.Asp .NET为我们提供了三种方式,一种是可以通过用QueryString来传送相应的值,再一种是通过session变量来传 ...

  6. 更改新建Asp.net WebForm的模板 的方法

    C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\ItemTemplates\CSharp\Web\2052\WebFor ...

  7. C# 中 FindControl 方法及使用

    FindControl 的使用方法 FindControl (String  id): 在页命名容器中搜索带指定标识符的服务器控件.(有点类似javascript中的getElementById(st ...

  8. MVC和Webform的比较和替换总结

    1.自定义控件,页面赋值可用HtmlHelper进行扩展 2.aspx的母版页可用Layout代替 3.webform的request,response方法在MVC中同样适应,只是类有点不同,例如表单 ...

  9. C#系统登录随机验证码生成及其调用方法

    话不多说,直接上代码 public ValidateCode() { } /// <summary> /// 验证码的最大长度 /// </summary> public in ...

随机推荐

  1. hdoj4099(字典树+高精度)

    题目链接:https://vjudge.net/problem/HDU-4099 题意:给T组询问,每个询问为一个字符串(长度<=40),求以该字符串为开始的fibonacci数列的第一个元素的 ...

  2. 网页授权access_token,基础支持access_token,jsapi_ticket

    微信开发中网页授权access_token与基础支持的access_token异同 问题1:网页授权access_token与分享的jssdk中的access_token一样吗? 答:不一样.网页授权 ...

  3. From 虚拟机模板 创建单节点K8S1.14.1的操作步骤

    半年前总结的 还是有记不住的地方... 1. 根据上一篇blog 自己创建了一个虚拟机 里面包含 k8s1.14.1 的k8s集群 这里简单说一下 虚拟机开机之后 如何处理以能够使用k8s 的简单过程 ...

  4. Tp5.1 管理后台开发纪要

    1. tp5.1 对网页是有缓存机制的 E:\phpStudy\PHPTutorial\WWW\NewAdmin\thinkphp\library\think\Template.php 下displa ...

  5. STL源码剖析——空间配置器Allocator#3 自由链表与内存池

    上节在学习第二级配置器时了解了第二级配置器通过内存池与自由链表来处理小区块内存的申请.但只是对其概念进行点到为止的认识,并未深入探究.这节就来学习一下自由链表的填充和内存池的内存分配机制. refil ...

  6. Linux02 cd命令以及绝对路径和相对路径

    一.cd 这是一个非常基本,也是大家常用的命令,用于切换当前目录,他的参数就是要切换的目录的路径,可以是绝对路径,也可以是相对路径. cd /home/keshengtao/ 绝对路径 cd ./pa ...

  7. A Story of One Country (Hard) CodeForces - 1181E2 (分治)

    大意: 给定$n$个平面上互不相交的矩形. 若一个矩形区域只包含一个矩形或者它可以水平或垂直切成两块好的区域, 那么这个矩形区域是好的. 求判断整个平面区域是否是好的. 分治判断, 可以用链表实现删除 ...

  8. jQuery控制页面滚动条上下滚动

    .向上滚动  $(); .向下滚动   $(); 参数解读:$(this)表示要实现上下滚动的对象,-50表示向上滚动50px , +50表示向下滚动50px ,1000表示滚动速度

  9. POJ 3233-Matrix Power Series( S = A + A^2 + A^3 + … + A^k 矩阵快速幂取模)

    Matrix Power Series Time Limit: 3000MS   Memory Limit: 131072K Total Submissions: 20309   Accepted:  ...

  10. linux之rename和mv的区别

    rename 命令格式 rename [ -v ] [ -n ] [ -f ] perlexpr [ files ] 参数介绍 -v:被替换掉的字符串 -n:替换成的字符串 -f:匹配要替换的文件模式 ...