简体   繁体   中英

Passing value to a user control from asp.net Page

I have following user control my .ascx Page. (Test.ascx)

<uc:Addresses runat="server" itemId='<%# StringId %>'></uc:SNETAddresses>

In the code behind of Test.ascx I have

protected string StringId = "{2A06199B-ED96-42F0-AB9A-602139E58BFB}";

In the code behind of user control Addresses.cs I have:

 public string itemId { get; set; }

So basically I want to pass a string to the variable itemId . But Somehow its not getting the value of variable "StringId". This simple thing is taking my so much time. I checked this post asp.net passing string variable to a user control but I am so sorry I could not get the answer. The reply is:

You may need to call DataBind on your Page in CreateChildControls or some other method 

I am new to Asp.Net and I didn't get what the user mean here.

You need to call Page.DataBind() , eg in your Page_Load() event handler.

Data-binding expressions ( <%# ... %> ) are only processed when you call the DataBind() method.


Alternatively (instead of using a data-binding expression), you can also simply assign the value to the user control's property from your page's code-behind:

// add an ID to the user control (in the markup)
<uc:Addresses runat="server" id="myControl"></uc:SNETAddresses>

Then in your Page_Load() method simply assign the value to the user control's property:

myControl.itemId = StringId;

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