Set value of UserControl property

J

Jim Ross [MVP]

I have a User Control with a property defined (in the ASCX, not in
CodeBehine. The property is defined like this:

<script runat=server language=vb>
Private m_deptName As String
<Bindable(True)>Public Property DepartmentName As String
Get
Return m_deptName
End Get
Set
m_deptName = value
End Set
End Property
</script>

When I put the control on a page and set the propery like this, it
works:
<uc1:Header id="headerCtl" runat=server DepartmentName="Some
Department"></uc1:Header>

However, if I use a variable that has been defined on the page and
then set the user control property to the value, it does not work, the
value is never set. There are no errors thrown, but, as I said, the
value is not set.

<script runat=server language=vb>
Dim pageDeptName As String = "Page Department Name"
</script>
....
<uc1:Header DepartmentName="<%= pageDeptName %>"
runat=server></uc1:Header>



Jim Ross
MS MVP [ASP.NET][VC/MFC emeritus]

To send email, change 'lotsofspamthroughhere' to 'msn' but please ask all questions in the newsgroups, not via private mail
 
J

Jacob Yang [MSFT]

Hi Jim,

Please right click the page and select "View Source". Based on my research
and experience, we can find the following in the HTML code:

<%= pageDeptName %>

The value of DepartmentName is set to "<%= pageDeptName %>". That should be
the reason for this issue.

Does it answer your question? If I have misunderstood your concern, please
feel free to let me know.

Best regards,

Jacob Yang
Microsoft Online Partner Support
<MCSD>
Get Secure! ¨C www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
J

Jim Ross [MVP]

Jacob,

Another bit of information. If I do a "View Source" on the rendered
page in the browser, I see <%= ValueForProperty %> (that is, whatever
the expressoin was in the control's declaratives) where the actual
value should be. Since the browser treates the < .... > as an HTML
tag, the effect is that nothing displays.

If I remove the ".." from around the <%= %> block, the page throws an
error that <% %> is not allowed within page tags.

Hi Jim,

Please right click the page and select "View Source". Based on my research
and experience, we can find the following in the HTML code:

<%= pageDeptName %>

The value of DepartmentName is set to "<%= pageDeptName %>". That should be
the reason for this issue.

Does it answer your question? If I have misunderstood your concern, please
feel free to let me know.

Best regards,

Jacob Yang
Microsoft Online Partner Support
<MCSD>
Get Secure! ¨C www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Jim Ross
MS MVP [ASP.NET][VC/MFC emeritus]

To send email, change 'lotsofspamthroughhere' to 'msn' but please ask all questions in the newsgroups, not via private mail
 
J

Jacob Yang [MSFT]

Hi Jim,

You are right in this issue.

Based on my research and experience, an explanation for this issue is "a
server tag cannot contain <% ... %> constructs".

For example, we can try to assign the value from a variable to the "href"
attribute of a hyperlink as the following:

<%
string href;
href = "http://www.microsoft.com";
%>

<A href=" <%=href%>"> http://www.microsoft.com </A>

The above HTML code works fine. But we cannot do the similar thing for the
NavigateUrl property of the asp:hyperlink.

I hope it helps.

Best regards,

Jacob Yang
Microsoft Online Partner Support
Get Secure! ¨C www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top