小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

FINDCONTROL的詳細(xì)介紹

 悟靜 2013-02-15

FindControl的使用方法 Control.FindControl (String):在當(dāng)前的命名容器中搜索帶指定 id

參數(shù)的服務(wù)器控件。(有點類似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類型的控件,需要強制類型轉(zhuǎn)化成TextBox類型
      TextBox tb=(TextBox)this.FindControl("TextBox1");
      this.Label1.Text = tb.Text;    }

當(dāng)TextBox1放到其他控件里應(yīng)該怎么查找呢?

    <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>

當(dāng)TextBox1放到Panel里,似乎沒什么影響 TextBox tb=(TextBox)this.FindControl

("TextBox1"),當(dāng)查看生存頁面的HTML代碼是發(fā)現(xiàn),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>

當(dāng)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發(fā)現(xiàn),頁

面有多個

 <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:

    protected void btn_click(object sender, EventArgs e)
    {
        //獲得按鈕
        Button btn = (Button)sender;
        TextBox tb = (TextBox)btn.Parent.FindControl("TextBox1");
        tb.Text = "Text";
    }

或者

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

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多