繁体   English   中英

从母版页使用变量调用用户控件

[英]Calling a usercontrol from a master page with a variable

在default.master中,我正在调用此用户控件

<uc8:MenuMain2 ID="MenuMain2" runat="server" RootCategoryId="30" ImageHeight="40"
                    ImageWidth="900" />

我想在代码中使用变量形式作为RootCategoryId的值,如果我尝试使用变量,我将得到“ this is not script let. will output as plain text ”,我该怎么做this is not script let. will output as plain text

MenuMain2是一个class 向其添加一个名为RootCategoryId的公共属性。 这将允许您从客户端和服务器端为其分配值:

例如,将其添加到您的MenuMain2控件后面的代码中:

public string RootCategoryId {get; set;}

正如@Boomer所提到的,您应该在类实现中使用属性,而不是字段。

每个控件都应注意其在ViewState中的属性。 这是微软实践MSDN的证明链接

使用不足

public string RootCategoryId {get; set;}

将值保存在ViewState中:

public string RootCategoryId
{
  get
  {
    return ViewState["RootCategoryId"] == null ?
       "Default Value!" :
       (string)ViewState["RootCategoryId"];
  }
  set { ViewState["RootCategoryId"] = value; }
}

在这种情况下,您在源代码中为此属性分配的值将在回发期间保持不变。

另一个要阅读的资源: http : //aspnetresources.com/articles/ViewState

暂无
暂无

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

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