有以下两个页面Default.aspx和Result.aspx,代码如下: //Default.aspx.cs public partial class _Default : System.Web.UI.Page <!– Result.aspx –> public partial class Result : System.Web.UI.Page 这两个页面都指定了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”); TextBox tb = (TextBox)con.FindControl(“TextBox1”); 但con的值为null,这样后续的语句也不可能执行了。问题出在哪里呢? 经过一番搜索,在forums.asp.net中找到了答案,以下引用的是bitmask的说法: TextBox tb = (TextBox)PreviousPage.Master.FindControl(“ContentPlaceHolder1”).FindControl(“TextBox1”); bitmask还在他的博客上写了一篇文章来阐述FindControl方法和INamingContainers接口:
<!– Default.aspx –>
<%@ Page Language=”C#” AutoEventWireup=”true” MasterPageFile=”~/Default.master” CodeFile=”Default.aspx.cs” Inherits=”_Default” %>
<asp:Content ID=”Content1″ runat=”server” ContentPlaceHolderID=”ContentPlaceHolder1″>
<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>
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;
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
<%@ 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;
{
protected void Page_Load(object sender, EventArgs e)
{
if (PreviousPage != null)
{
TextBox tb = (TextBox)PreviousPage.FindControl(“TextBox1”);
if (tb != null)
TextBox1.Text = tb.Text;
}
}
}
if (con == null)
return;
…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.
所以以上的代码应该改成:
http://www.odetocode.com/Articles/116.aspx
如何使用findcontrol查找内容页上的某个控件?_asp.net技巧
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 如何使用findcontrol查找内容页上的某个控件?_asp.net技巧
相关推荐
-      对.net framework 反射的反思_asp.net技巧
-      .net3.5和vs2008中的asp.net ajax_asp.net技巧
-      使用asp.net ajax框架扩展html map控件_asp.net技巧
-      asp.net应用程序资源访问安全模型_asp.net技巧
-      photoshop初学者轻松绘制螺旋漩涡特效_photoshop教程
-      photoshop通道结合图层模式抠狗尾巴草_photoshop教程
-      web.config详解+asp.net优化_asp.net技巧
-      asp.net中多彩下拉框的实现_asp.net技巧