Dzwebs.Net

撰写电脑技术杂文十余年

这样来使用FINDCONTROL

Admin | 2011-11-7 11:17:11 | 被阅次数 | 6494

温馨提示!

如果未能解决您的问题,请点击搜索;登陆可复制文章,点击登陆

  FindControl的使用语句为:Control.FindControl (String),它的功能是在当前的命名容器中搜索带指定id参数的服务器控件。

  下面是与FindControl相关的各种实例代码:

  一、直接引用查找

 <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来引用,这里我们使用

    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)this.dg1.Controls[0].Controls[0].FindControl("TextBoxH")).Text = "Head";
 ((TextBox)this.dg1.Controls[0].Controls[this.dg1.Controls[0].Controls.Count -

1].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:

  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();


该杂文来自: 网站开发杂文

上一篇:asp.net与JS传值、访问值

下一篇:C#发送广播消息和接收广播消息的代码示例

网站备案号:

网站备案号:滇ICP备11001339号-7

版权属性:

Copyright 2007-2021-forever Inc. all Rights Reserved.

联系方式:

Email:dzwebs@126.com QQ:83539231 访问统计