簡體   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