简体   繁体   中英

Calling a usercontrol from a master page with a variable

in the default.master i am calling this user control

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

i want to use a variable form my code behind for the value of RootCategoryId, how can i do this, if i am trying to use a variable i am getting " this is not script let. will output as plain text "

MenuMain2 is a class . Add a public property to it called RootCategoryId . This will allow you to assign values to it from client and server side:

For example, add this to your MenuMain2 control code-behind:

public string RootCategoryId {get; set;}

As @Boomer mention you should use Properties, not fields in your class implementation.

Every Control should care his properties in ViewState. This is Microsoft practice MSDN prooflink

It's not enough to use

public string RootCategoryId {get; set;}

Save your value in ViewState:

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

In this case value that you assign to this propertie in source code will persist during postbacks.

Another source to read: http://aspnetresources.com/articles/ViewState

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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