簡體   English   中英

將值從asp.net頁傳遞給用戶控件

[英]Passing value to a user control from asp.net Page

我有以下用戶控制我的.ascx頁面。 (Test.ascx)

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

Test.ascx后面的代碼中,我有

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

在用戶控件Addresses.cs后面的代碼中,我具有:

 public string itemId { get; set; }

所以基本上我想將字符串傳遞給變量itemId 但是不知何故,它沒有得到變量“ StringId”的值。 這個簡單的事情花了我很多時間。 我檢查了此帖子asp.net將字符串變量傳遞給用戶控件,但對不起我無法得到答案。 答復是:

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

我是Asp.Net的新手,但我沒有明白用戶的意思。

您需要調用Page.DataBind() ,例如在您的Page_Load()事件處理程序中。

數據綁定表達式( <%# ... %> )僅在調用DataBind()方法時處理。


另外,(而不是使用數據綁定表達式),您還可以簡單地從頁面代碼的后面將值分配給用戶控件的屬性:

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

然后在您的Page_Load()方法中,只需將值分配給用戶控件的屬性即可:

myControl.itemId = StringId;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM