- 自動產生的控制項
- 被包裝起來的控制項 (例如:GridView FormView)
而FindControl的使用方法是
- 在程式碼中建立控制項
- 指定控制項為找到的控制項
- 自由的控制找到的控制項
一般的搜尋方式(HTML)
1: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="FindControl.WebForm1" %>
2:
3: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4:
5: <html xmlns="http://www.w3.org/1999/xhtml">
6: <head runat="server">
7: <title>找出控制項的方法</title>
8: </head>
9: <body>
10: <form id="form1" runat="server">
11: <div>
12:
13: <asp:TextBox ID="TextBox1" runat="server">這裡是TextBox</asp:TextBox>
14: <br />
15: <asp:Button ID="Button1" runat="server" Text="這裡是Button" />
16: <br />
17: <br />
18: <asp:Button ID="btnFind" runat="server" onclick="btnFind_Click"
19: Text="找出上面兩個控制項" />
20: <br />
21: <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
22:
23: </div>
24: </form>
25: </body>
26: </html>
程式碼
1: using System;
2: using System.Collections.Generic;
3: using System.Linq;
4: using System.Web;
5: using System.Web.UI;
6: using System.Web.UI.WebControls;
7:
8: namespace FindControl
9: {
10: public partial class WebForm1 : System.Web.UI.Page
11: {
12: protected void Page_Load(object sender, EventArgs e)
13: {
14:
15: }
16:
17: protected void btnFind_Click(object sender, EventArgs e)
18: {
19: // 確認TextBox是否存在
20: if (this.FindControl("TextBox1") != null)
21: {
22: // 找出 TextBox1
23: TextBox Findtxt = (TextBox)this.FindControl("TextBox1");
24: // 顯示到 Label1
25: Label1.Text = Findtxt.Text+"<br />";
26: }
27:
28: // 確認 Button 是否存在
29: if (this.FindControl("Button1") != null)
30: {
31: // 找出 TextBox1
32: Button Findbtn = (Button)this.FindControl("Button1");
33: // 顯示到 Label1
34: Label1.Text += Findbtn.Text;
35: }
36: }
37: }
38: }
在有主板頁面的情況
如果網頁本身就在主板頁面的話 就不能夠使用一般的方法來尋找控制項一般的搜尋方式找有主板頁面的控制項都會碰到這個問題
因為控制項已經變成了包在主板頁面內,所以控制項的ID也會變得不一樣
如果要找到主板頁面內的控制項的話,先要找出主板頁面,再找出控制項
以下是主板頁面的方式(HTML)
1: <%@ Page Title="" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true" CodeBehind="MasterForm.aspx.cs" Inherits="FindControl.MasterForm" %>
2: <asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
3: </asp:Content>
4: <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
5: <asp:TextBox ID="TextBox1" runat="server" Width="164px">這個是主板頁面內的TexbBox</asp:TextBox>
6: <br />
7: <asp:Button ID="Button1" runat="server" Text="這個是主板頁面內的Button" />
8: <br />
9: <br />
10: <asp:Button ID="btnFind" runat="server" onclick="btnFind_Click" Text="找出控制項" />
11: <br />
12: <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
13: <br />
14: </asp:Content>
1: using System;
2: using System.Collections.Generic;
3: using System.Linq;
4: using System.Web;
5: using System.Web.UI;
6: using System.Web.UI.WebControls;
7:
8: namespace FindControl
9: {
10: public partial class MasterForm : System.Web.UI.Page
11: {
12: protected void Page_Load(object sender, EventArgs e)
13: {
14:
15: }
16:
17: protected void btnFind_Click(object sender, EventArgs e)
18: {
19: // 找出在主板頁面內的按鈕
20: if (Master.FindControl("ContentPlaceHolder1").FindControl("Button1") != null)
21: {
22: //找出 Button1
23: Button btn1 = (Button)Master.FindControl("ContentPlaceHolder1").FindControl("Button1");
24: // 將 Button1 內容顯示在 Label1
25: Label1.Text = btn1.Text+"<br />";
26: }
27:
28: // 找出在主板頁面內的文字方塊
29: if (Master.FindControl("ContentPlaceHolder1").FindControl("TextBox1") != null)
30: {
31: //找出 TextBox1
32: TextBox txt1 = (TextBox)Master.FindControl("ContentPlaceHolder1").FindControl("TextBox1");
33: // 將 TextBox1 內容顯示在 Label1
34: Label1.Text += txt1.Text;
35: }
36: }
37: }
38: }
參考資料
How to: Reference ASP.NET Master Page Content[習題].FindControl()方法 與 PlaceHolder控制項 #1(動態加入「子控制項」,因Page_Load而發生的錯誤)
沒有留言:
張貼留言