我有以下用户控制我的.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 passing string variable to a user control,但很抱歉,我无法得到答案。答复是:
You may need to call DataBind on your Page in CreateChildControls or some other method
我是Asp.Net的新手,我没有得到用户的意思。
答案 0 :(得分:2)
您需要致电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;