你可以参考这篇:http://www.cnblogs.com/insus/articles/2050790.html Insus.NET觉得它写得很不理想。因为不想写得这样复杂,或者有其它ID变化的可能,如:
ContentPlaceHolderID="ContentPlaceHolder1" 有可能变为ContentPlaceHolderID="ContentPlaceHolder3",ID="TextBox1"也有可能变为ID="MemberName" 等。
在目标页,得需要写判断是否存在,是否为NULL,如果不这样写,程序有可能发生异常。
另外一个就是不管它是不是在母板页还是不是在母板页,都想它很容易获取得源一页的值。
演示开始,你需要创建一个接口Interface:
View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
/// <summary>
/// Summary description for Itransmitable
/// </summary>
public interface Itransmitable
{
TextBox GetTextBoxControl();
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
/// <summary>
/// Summary description for Itransmitable
/// </summary>
public interface Itransmitable
{
TextBox GetTextBoxControl();
}
接下来,在源页面实作这个接口:
View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page,Itransmitable
{
protected void Page_Load(object sender, EventArgs e)
{
}
public TextBox GetTextBoxControl()
{
return this.TextBox1;
}
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page,Itransmitable
{
protected void Page_Load(object sender, EventArgs e)
{
}
public TextBox GetTextBoxControl()
{
return this.TextBox1;
}
}
最后就是在目标页的Page_Load写:
View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = ((Itransmitable)PreviousPage).GetTextBoxControl().Text;
}
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = ((Itransmitable)PreviousPage).GetTextBoxControl().Text;
}
}
这样做,可以完全脱离masterpage,也让你不再使用FindControl这个方法。