Set value of UserControl property

Discussion in 'ASP .Net Web Controls' started by Jim Ross [MVP], Sep 24, 2003.

  1. 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
     
    Jim Ross [MVP], Sep 24, 2003
    #1
    1. Advertising

  2. 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.
     
    Jacob Yang [MSFT], Sep 24, 2003
    #2
    1. Advertising

  3. 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.

    (Jacob Yang [MSFT]) wrote:

    >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
     
    Jim Ross [MVP], Sep 24, 2003
    #3
  4. 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.
     
    Jacob Yang [MSFT], Sep 25, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Iain
    Replies:
    3
    Views:
    705
    Chris Bower
    Nov 18, 2003
  2. moid
    Replies:
    2
    Views:
    631
    John Timney \(Microsoft MVP\)
    Jan 23, 2004
  3. Mike Dole
    Replies:
    3
    Views:
    6,244
    Karl Seguin
    Nov 17, 2004
  4. Fabiano
    Replies:
    2
    Views:
    609
    =?Utf-8?B?c3Jpbmk=?=
    Jun 3, 2005
  5. KJ
    Replies:
    0
    Views:
    146
Loading...

Share This Page