0

在 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

4

2 回答 2

1

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

每个控件都应该关心他在 ViewState 中的属性。这是微软实践MSDN 证明link

使用还不够

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

于 2013-01-16T10:38:18.923 回答
1

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

例如,将此添加到您的 MenuMain2 控件代码隐藏中:

public string RootCategoryId {get; set;}
于 2013-01-16T08:48:34.207 回答