5

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!

Jón Trausti Arason
  • 4,548
  • 1
  • 39
  • 46

1 Answers1

4

<%= 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.

Community
  • 1
  • 1
Crab Bucket
  • 6,219
  • 8
  • 38
  • 73