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. [VS] - Visual Studio 智能感知无法启用 之解决

    背景 VS 2017 智能感知无法使用,重置 "导入和导出设置..." 后仍无法使用. 解决 我在 VS 上安装了 Resharper 的,猜测可能跟其配置有关,重置 Intell ...

  2. Quartz.Net—IJob特性

    IJob默认情况下是无状态的,和其他系统没有关系  特别是job里面的jobdata每次都是新的.可以无限扩展. PersistJobDataAfterExecution JobData持久化 Job ...

  3. 使用Lombok总结

    Lombok学习总结 Project Lombok is a java library that automatically plugs into your editor and build tool ...

  4. ~postman基础断言方法

    postman官方文档:https://learning.getpostman.com/docs/postman/scripts/test_examples/ 断言1:检查响应主体是否包含字符串 // ...

  5. web框架解析

    一.白手起家 要想模拟出web请求响应的流程,先想想平时我们是怎么上网浏览网页的?首先打开浏览器,然后在地址栏中输入我们想要访问的页面,紧接着按下回车键Enter,最后跳转至目标页面(当然我们也会出现 ...

  6. vim常用命令的使用

    中文博客:https://www.cnblogs.com/lijia0511/p/5644566.html 英文原文:http://yannesposito.com/Scratch/en/blog/L ...

  7. BC26模组UDP调试

    BC26模组调试 数据上报AT流程 [15:33:46.819]收←◆ F1: 0000 0000 V0: 0000 0000 [0001] 00: 0006 000C 01: 0000 0000 U ...

  8. chrome下载离线安装包

    chrome下载离线安装包 - codeflyto - 博客园 下载页面:

  9. Spring Boot配置文件yml讲解--行内对象的配置方式

    yml行内对象的配置方法,一般是采取 上面的缩进方式,我只想配置在一行怎么处?——

  10. (转)基于FFPMEG2.0版本的ffplay代码分析

    ref:http://zzhhui.blog.sohu.com/304810230.html 背景说明 FFmpeg是一个开源,免费,跨平台的视频和音频流方案,它提供了一套完整的录制.转换以及流化音视 ...