简体   繁体   English

如何从用户控件更改MasterPage中的控件值?

[英]How to change control value in MasterPage from user control ?

I want to change control value exists in Master page from user control and the scenario is like this: 我想从用户控件更改Master页面中存在的控件值,方案如下:
On user control load method I use a query string parameter called "catID" to get category entity, then I want to set the category title as value of title tag (Category title) ,which locate in Master page . 在用户控件加载方法中,我使用一个名为“catID”的查询字符串参数来获取类别实体,然后我想将类别标题设置为标题标签(类别标题)的值,它位于主页面中。
I tried to change the title in user control page load control but the Master page load method executes earlier. 我试图在用户控制页面加载控件中更改标题,但主页面加载方法更早执行。

Any ideas ? 有任何想法吗 ?

Provide a public method in your MasterPage that sets the title, for example: 在MasterPage中提供设置标题的公共方法,例如:

public void setTitle(string title)
{
    this.LblTitle.Text = title;
}

Then you can call it from your UserControl( YourMasterPage is the actual type of the MasterPage): 然后你可以从你的UserControl中调用它( YourMasterPage是MasterPage的实际类型):

((YourMasterPage)this.Page.Master).setTitle("new Title");

If it's a query string parameter you should be able to read "catId" ie from Masterpage code behind as well and set the title. 如果它是一个查询字符串参数,您应该能够读取“catId”,即从后面的Masterpage代码中读取并设置标题。

EDIT: 编辑:

Try a property then: In usercontrol 然后尝试一个属性:在usercontrol中

protected void Page_Load(object sender, EventArgs e)
{
    this.MyTitle = "SomeTitle";
}
public string MyTitle { get; set; }

On masterpage: 在主页上:

protected void Page_Load(object sender, EventArgs e)
{
    WebUserControl.PreRender += new EventHandler(WebUserControl_PreRender);
}

void WebUserControl_PreRender(object sender, EventArgs e)
{
    string str = WebUserControl.MyTitle;
    this.Header.Title = str;
}
 ((Label)Master.FindControl("loadlbl")).Text = "your text";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM