1

I have the following html on my website:

<meta name="keywords" content="<%=Keywords%>"/>
<meta name="description" content="<%=Description%>"/>

This is in the master page for my site. In the code behind for the master page, I have this:

    private String _description = "A default description for my site";
    public String Description
    {
        get { return _description; }
        set { _description = value; }
    }

    private String _keywords = "my Site my Company keywords ";
    public String Keywords
    {
        get {return _keywords; }
        set { _keywords = value; }
    }

The idea is that all the pages on my site will have a default description/keywords, unless I need to set them to something else on a particular page (using Master.Description = "whatever").

However, I am having some troubles: the opening bracket for the inline code block delcarations are escaped in the generated HTML, so the code blocks don't work. Here is an excerpt of the generated html for my page:

<meta name="keywords" content="&lt;%=Keywords%>" /><meta name="description" content="&lt;%=Description%>" />

I'm used to using C#, but I'm new to asp.net, and I can't work out the right syntax to get this working correctly. What do I need to do to get inline code blocks working right?

2 Answers 2

1

Run your controls server side and assign values there:

    <meta name="keywords" id="keywords" runat="server" />
    <meta name="description" id="desc" runat="server" />

    keywords.Attributes("content") = Keywords;
    desc.Attributes("content") = Description;
Sign up to request clarification or add additional context in comments.

4 Comments

This is difficult using master pages, as the child pages can't see the keywords & description elements. Funneling all changes to these elements through properties on the master page lets me put prefixes/suffixes on easily.
You could put the code in the master page code behind. The masterpage code behind file will be able to read the same objects as your masterpage markup file
Is there any reason of efficiency or similar to do it this way rather than with properties? I could get around my question here by doing get {return "\"" + _description + "\""}, but I want to know how to put inline code blocks on the page like this without them being escaped.
0

Use simple/single binding expression.

<meta name="keywords" content="<%# Keywords%>"/>
<meta name="description" content="<%# Description%>"/>

Issue the DataBind() method in code-behind:

 if(!IsPostBack)
    DataBind();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.