将动态变量传递给UserControl属性。

时间:2022-09-02 11:59:01

I'm creating an UserControl and I'm passing a variable to it.

我正在创建一个UserControl,并将一个变量传递给它。

It works fine if I do it this way:

如果我这样做,效果很好:

<uc:TestControl ID="testControl" runat="server" Variable="test"></uc:TestControl>

However I want to pass a dynamic variable to the control like this:

但是,我想将一个动态变量传递给控件,如下所示:

<uc:TestControl ID="testControl" runat="server" Variable="<%=dynamicVariable%>"></uc:TestControl>

But unfortunately that doesn't work and I know I could assign it on Page_Load but I don't like that way.

但不幸的是,这不起作用,我知道我可以在Page_Load分配它,但我不喜欢这种方式。

So I'm wondering if it's possible at all. Is there any way to assign a dynamic variable to an attribute like I wanted above? Or am I required to do it in Page_Load?

所以我想知道这是否可能。有没有办法像我上面想的那样给一个属性分配一个动态变量?或者我需要在Page_Load中执行它吗?

Any feedback would be appreciated! Thanks!

如有任何反馈,我们将不胜感激!谢谢!

1 个解决方案

#1


2  

<%= syntax doesn't work with controls marked as runat="server" .Try using the databind syntax

<%=语法不适用于标记为runat="server"的控件。请尝试使用databind语法

Variable="<%#dynamicVariable%>"

Then calling databind on the user control as per this SO question

然后根据这个问题在用户控件上调用databind

EDIT

编辑

To database i think it's a straight

对于数据库,我认为它是直的。

testControl.DataBind()

supported in ASP.Net 3.5 and greater.

支持ASP。Net 3.5和更大的。

#1


2  

<%= syntax doesn't work with controls marked as runat="server" .Try using the databind syntax

<%=语法不适用于标记为runat="server"的控件。请尝试使用databind语法

Variable="<%#dynamicVariable%>"

Then calling databind on the user control as per this SO question

然后根据这个问题在用户控件上调用databind

EDIT

编辑

To database i think it's a straight

对于数据库,我认为它是直的。

testControl.DataBind()

supported in ASP.Net 3.5 and greater.

支持ASP。Net 3.5和更大的。