简体   繁体   English

从子页面永久更改主页内容

[英]Altering masterpage content permanently from a subpage

Say I have a masterpage and 2 subpages.假设我有一个母版页和 2 个子页。 For sake of simplicity I'll refer the first subpage as subpage A, and the other as subpage B.为了简单起见,我将第一个子页面称为子页面 A,将另一个子页面称为子页面 B。

Now assume that I made a change in the masterpage label from subpage A, using the below code:现在假设我使用以下代码在子页面 A 的母版页 label 中进行了更改:

var lbl = this.Master.FindControl("my_control") as Label;
lbl.Text = "mynewtext";

It would function normally and I would see that my_control label has a different text.它通常是 function,我会看到 my_control label 有不同的文本。

However if I were to switch to subpage B, the masterpage label would change back to its initial form.但是,如果我切换到子页面 B,母版页 label 将变回其初始形式。 I know why: Each time I load a page with a certain masterpage, the masterpage loads over and over again along with the Page_load event of the childpage.我知道为什么:每次我加载一个带有特定母版页的页面时,母版页会随着子页面的 Page_load 事件一遍又一遍地加载。 And since in the codebehind of the masterpage, the label 'my_control' has a different string value, my code alteration in subpage A disappears.由于在母版页的代码隐藏中,label 'my_control' 具有不同的字符串值,因此我在子页面 A 中的代码更改消失了。

So my question is, how can I prevent that, how can I make a subpage derive the masterpage value?所以我的问题是,如何防止这种情况发生,如何使子页面派生母版页值? OR shortly, how can I make a modification in masterpage by a subpage permanently?或者简而言之,我怎样才能通过子页面永久修改母版页?

The cleanest way I can think, is to persist the change in some kind of storage and create the flow:我能想到的最干净的方法是将更改持久化在某种存储中并创建流程:

  1. 1st load with a default value第一次加载默认值
  2. The 1st sub page, changes the value and persists the change in a database or similar第一个子页面,更改值并将更改保存在数据库或类似的
  3. The second load would take the adjusted value instead of the default.第二个负载将采用调整后的值而不是默认值。

Quick test would be to store the value into session construct.快速测试是将值存储到 session 结构中。

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

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