程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> .NET實例教程 >> 如何使用FindControl查找內容頁上的某個控件?

如何使用FindControl查找內容頁上的某個控件?

編輯:.NET實例教程

有以下兩個頁面Default.aspx和Result.ASPx,代碼如下:
<!-- Default.ASPx -->
<%@ Page Language="C#" AutoEventWireup="true" MasterPageFile="~/Default.master"  CodeFile="Default.ASPx.cs" Inherits="_Default" %> 
<ASP:Content ID="Content1" runat="server" ContentPlaceHolderID="ContentPlaceHolder1">
    &nbsp;<asp:Label ID="Label1" runat="server" Text="Please input a string here"></ASP:Label>
    <asp:TextBox ID="TextBox1" runat="server"></ASP:TextBox>
    <asp:Button ID="Button1" runat="server" Text="Button" PostBackUrl="~/Result.aspx" /></ASP:Content>

//Default.ASPx.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
//using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
}
 

<!-- Result.ASPx -->
<%@ Page Language="C#" MasterPageFile="~/Default.master" AutoEventWireup="true" CodeFile="Result.ASPx.cs" Inherits="Result" Title="Untitled Page" %>
<ASP:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <asp:Label ID="Label1" runat="server" Text="The string you input in the previous page is"></ASP:Label>
    <asp:TextBox ID="TextBox1" runat="server"></ASP:TextBox>
</ASP:Content> 
//Result.ASPx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Result : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (PreviousPage != null)
        {
            TextBox tb = (TextBox)PreviousPage.FindControl("TextBox1");
            if (tb != null)
                TextBox1.Text = tb.Text;
        }
    }
}
 

這兩個頁面都指定了MasterPageFile屬性。因為該MasterPage中的內容無關緊要,就不列出來了。在Default.aspx上有兩個控件:TextBox1用於接受用戶的輸入,Button1用於提交頁面,其PostBackUrl指向Result.aspx。在Result.ASPx.cs的Page_Load方法中嘗試在TextBox1中顯示用戶在前一頁面的TextBox1中輸入的字符串。當執行以下語句時:

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


tb的值為null。將以上語句更改為如下代碼:

Content con = (Content)PreviousPage.FindControl("Content1");
if (con == null)
    return;

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

但con的值為null,這樣後續的語句也不可能執行了。問題出在哪裡呢?

經過一番搜索,在forums.ASP.Net中找到了答案,以下引用的是bitmask的說法:
...becasue the Content controls themselves dissapear after the master page rearranges the page. You can use the ContentPlaceHolders, or the <form> on the MasterPage if there are no INamingContainers between the form and the control you need.


所以以上的代碼應該改成:

TextBox tb = (TextBox)PreviousPage.Master.FindControl("ContentPlaceHolder1").FindControl("TextBox1");

bitmask還在他的博客上寫了一篇文章來闡述FindControl方法和INamingContainers接口:
http://www.odetocode.com/Articles/116.ASPx

http://movingboy.cnblogs.com/archive/2006/07/06/444690.Html

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved