如何在按钮单击子页面后更改母版页标签的文本

时间:2012-05-08 10:41:50

标签: c# asp.net

我正在使用购物车图标显示购物车中的产品数量。但是当我将商品添加到购物车时,购物没有更新。所以我想知道是否有任何方法可以在按钮点击子页面后更改母版页标签的文本。

3 个答案:

答案 0 :(得分:4)

我建议您在MasterPage中提供一个可用于设置/获取标签文本的公共属性。

在你的主人(假设它的类型被称为SiteMaster):

public String ShoppingCartNumber{ 
    get{ return LblShoppingCart.Text; }
    set{ LblShoppingCart.Text = value; }
}

在按钮的点击事件处理程序中:

SiteMaster master = (SiteMaster)Page.Master;
master.ShoppingCartNumber = "1234";

这种方法很简单,不易出错,易于阅读。您甚至可以更改主控件中的控件而无需更改页面(例如,如果要使用TextBox替换Label)。

答案 1 :(得分:0)

试试这个

Label mpLabel = (Label) Master.FindControl("masterPageLabel");
if(mpLabel != null)
{
    Label1.Text = "Master page label = " + mpLabel.Text;
}

答案 2 :(得分:0)

试试这个:
添加您的masterpage.cs文件:

public Label lbl
    {
        get { return YourLabelId; }
        set { YourLabelId= value; }
    }

在您的内容页面中添加:

<%@ MasterType VirtualPath="~/YourMasterPageName.Master" %> 

然后在按钮点击事件的内容页面中访问:

string name = Master.lbl.text;