Using codeblocks within usercontrols
I tried using a codeblock syn开发者_StackOverflow中文版tax within a property sent to a web user control:
<uc1:MyControl ID="MyControl1" runat="server" SomeProperty="<%= somevalue %>"/>
The user control has the public property SomeProperty declared and also uses code block to display the property value:
<p><% = SomeProperty %></p>
The output on my page is unfortunately
<p><%= somevalue %></p>
And not the actual value. Anyone know of some workaround for this?
You are trying to assign a server side value on a server side control - this is not possible.
You can use code blocks in client side code (that doesn't have a runat="server"
attribute), this of course doesn't not apply to server side controls.
Set the attribute in the code behind (ascx
), before OnRender
:
// In onload, pre render or other event handler
MyControl1.SomeProperty = somevalue; // C#
MyControl1.SomeProperty = somevalue ' VB.NET
Try assigning the value of the property to a Label and call the .DataBind() method on the control.
精彩评论