从内容页面更新位于母版页控件上的asp.net标签

时间:2022-10-19 23:24:23

I have a master page in my asp.net application which has a user control named menu like this:

我的asp.net应用程序中有一个母版页,它有一个名为menu的用户控件,如下所示:

  <asp:Literal ID="SavedCVLiteral" runat="server" Text="1" /

and a public property like this:

和这样的公共财产:

 public string lbl_Text
        {
            get { return SavedCVLiteral.Text; }
            set { SavedCVLiteral.Text = value; }
        }

In my master page, I have created a property like this:

在我的母版页中,我创建了一个这样的属性:

<%@ Register Src="Controls/CompanyLhsMenu.ascx" TagName="CompanyLhsMenu" TagPrefix="uc" %>


 <uc:CompanyLhsMenu ID="menu" runat="server" />


     public string SavedCVCount
            {
                get { return menu.lbl_Text; }
                set { menu.lbl_Text = value; }
            }

in my content page, I want to update the label so I added this:

在我的内容页面中,我想更新标签,所以我添加了这个:

 <%@ MasterType VirtualPath="~/LoggedCompany.master" %>

and using it like this:

并像这样使用它:

 int count = HREmployee.GetSavedCVsCount(EmployeeID);
            Master.SavedCVCount = count.ToString();

but it is not updating the label and no error as well. I set break points and they are not hit in master page and control's properties.

但它没有更新标签,也没有错误。我设置了断点,它们没有在母版页和控件的属性中命中。

Please suggest how to update label located on a control in master page from the content page ?

请建议如何从内容页面更新位于母版页控件上的标签?

2 个解决方案

#1


1  

Place following code into content page:

将以下代码放入内容页面:

protected void Page_Load(object sender, EventArgs e)
{
    UserControl US = FindControl("CompanyLhsMenu") as UserControl;
    Literal ltrel;

    try
    {
        US = (UserControl)Master.FindControl("menu");

        ltrel = (Literal)US.FindControl("SavedCVLiteral");

        if (ltrel != null)
        {
            ltrel.Text = "update it";
        }
    }
    catch (Exception ex)
    {
    }
}

#2


1  

You can access the Masterpage as a property on your current page. master page controls are protected so you can access them by using FindControl(string name). Try something like that

您可以在当前页面上将主页面作为属性进行访问。母版页控件受到保护,因此您可以使用FindControl(字符串名称)访问它们。尝试类似的东西

// Gets a reference to a Literal control inside a ContentPlaceHolder
ContentPlaceHolder mpContentPlaceHolder;
Literal ltrel;
mpContentPlaceHolder = 
    (ContentPlaceHolder)Master.FindControl("ContentPlaceHolder1");
if(mpContentPlaceHolder != null)
{
    ltrel = (Literal) mpContentPlaceHolder.FindControl("SavedCVLiteral");
    if(ltrel != null)
    {
        ltrel .Text = "update it";
    }
}

You have to this kind of scenario..

你必须要这种情况..

Hope it helps..

希望能帮助到你..

#1


1  

Place following code into content page:

将以下代码放入内容页面:

protected void Page_Load(object sender, EventArgs e)
{
    UserControl US = FindControl("CompanyLhsMenu") as UserControl;
    Literal ltrel;

    try
    {
        US = (UserControl)Master.FindControl("menu");

        ltrel = (Literal)US.FindControl("SavedCVLiteral");

        if (ltrel != null)
        {
            ltrel.Text = "update it";
        }
    }
    catch (Exception ex)
    {
    }
}

#2


1  

You can access the Masterpage as a property on your current page. master page controls are protected so you can access them by using FindControl(string name). Try something like that

您可以在当前页面上将主页面作为属性进行访问。母版页控件受到保护,因此您可以使用FindControl(字符串名称)访问它们。尝试类似的东西

// Gets a reference to a Literal control inside a ContentPlaceHolder
ContentPlaceHolder mpContentPlaceHolder;
Literal ltrel;
mpContentPlaceHolder = 
    (ContentPlaceHolder)Master.FindControl("ContentPlaceHolder1");
if(mpContentPlaceHolder != null)
{
    ltrel = (Literal) mpContentPlaceHolder.FindControl("SavedCVLiteral");
    if(ltrel != null)
    {
        ltrel .Text = "update it";
    }
}

You have to this kind of scenario..

你必须要这种情况..

Hope it helps..

希望能帮助到你..