两个母板页嵌套,在内容页获得第一个母板页数据的问题

问题参考http://topic.csdn.net/u/20111129/09/887007d1-566c-4205-85bd-063ff3b50f5d.html?seed=632864658&r=76705307#r_76705307,来自论坛网友问及:

 

解决,避免使用FindControl方法,改用接口interface.

IGetTextBoxValueable
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for IGetTextBoxValueable
/// </summary>
namespace Insus.NET
{
    public interface IGetTextBoxValueable
    {
        string GetTextBoxValue();
    }
}

 

在第一个MasterPage M1.master.cs实现这个接口:

View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Insus.NET;

public partial class M1 : System.Web.UI.MasterPage,IGetTextBoxValueable
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

   
    public string GetTextBoxValue()
    {
        return this.txtName.Text;
    }
}

 

最后是有内容页,把第一个MasterPage的M1转为接口,因为它有实作接口:

View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Insus.NET;

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

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        IGetTextBoxValueable obj = (IGetTextBoxValueable)this.Master.Master;
        this.Label1.Text = obj.GetTextBoxValue();
    }
}

 

 效果:

 

测试代码:http://download.cnblogs.com/insus/ASPDOTNET/GetMasterPageControlValue.rar

 

posted @ 2011-11-29 11:11  Insus.NET  阅读(661)  评论(2编辑  收藏  举报