如何使用FindControl查找内容页上的某个控件?
来源:互联网 发布:软件测试说明书模板 编辑:程序博客网 时间:2024/06/06 02:14
<!-- 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>
//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
- 如何使用FindControl查找内容页上的某个控件
- 如何使用FindControl查找内容页上的某个控件?
- 如何使用FindControl查找内容页上的某个控件?
- 如何使用FindControl查找内容页上的某个控件?
- 母版页中使用FindControl,后台使用FindControl查找Span
- FindControl——对内容页使用FindControl方法
- Master内使用FindControl找不到控件的解决方案
- repeater控件中使用FindControl寻找控件
- 如何查找带母版页的控件 Master.Master.FindControl("")
- FindControl——对母版页使用FindControl方法
- FindControl——对母版页使用FindControl方法
- 在使用了母版页的内容页内查找控件
- 解决使用MasterPage后,Page.FindControl方法找不到指定控件的问题
- 如何将光标定位在对话框上的某个控件上
- 《如何更改其他程序ListView控件中某个Item的内容》 -- 天津 赵春生
- 使用MasterPage后FindControl的使用
- 内容页访问母版页属性的方法之一(Master.FindControl())
- findcontrol()方法 内容页对母板页属性修改法的运用
- DataGrid和DataList中CommandButton的问题
- 一個Ajax.NET的查詢實例
- Windows2003Server中DOTNET程序访问COM组件
- 从asp.net页面发送电子邮件
- ASP.NET2.0瞎记-加密链接字符串
- 如何使用FindControl查找内容页上的某个控件?
- About Ajax-关于Ajax
- 在.NET2.0中发送Email
- asp.netWebForm页面间传值方法
- 將datagrid控件內容輸出到excel文件
- 漫谈Ajax在.Net中的使用
- 教你制做Web实时进度条
- 介绍几个ASP.NET中容易忽略但却很重要的方法函数
- 第一次用.net2.0LOGIN登陆控件的困惑和解决方法