Jón Trausti Arason
Jón Trausti Arason

Reputation: 4698

Passing dynamic variable to UserControl attribute

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

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.

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?

Any feedback would be appreciated! Thanks!

Upvotes: 5

Views: 3609

Answers (1)

Crab Bucket
Crab Bucket

Reputation: 6277

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

Variable="<%#dynamicVariable%>"

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

EDIT

To database i think it's a straight

testControl.DataBind()

supported in ASP.Net 3.5 and greater.

Upvotes: 4

Related Questions