3

I have this code in my ASP page:

<%@ Register TagPrefix="genies" Namespace="TheNamespace" Assembly="TheAssembly"%>

...

<% string name = GetTag().Name; %>
<div class="<%= name %>"></div>
<genies:BarGenie ID="BarGenie1" runat="server" Title="<%= name %>" />

The BarGenie class has the following code:

public class BarGenie : Control {

    public string Title { get; set; }

    protected override void Render(HtmlTextWriter writer) {
        writer.Write("<div>" + Title + "</div>");
    }

}

This is the HTML that is generated:

<div class="E00383"></div>
<div><%= name %></div>

I'd like to see this HTML:

<div class="E00383"></div>
<div>E00383</div>

I've tried various things in the ASPX code: @name, <%# name %> etc but I can't figure out what's going on. Is there some other way of inserting a custom control that I should be using? Do I have the wrong approach to using this custom control?

3 Answers 3

3

Try using GetTag().Name instead of name.

Also check out http://www.scottfindlater.co.uk/blog/asp-net-inline-code-and it may help

As in

<div class="<%= GetTag().Name%>"></div>
<genies:BarGenie ID="BarGenie1" runat="server" Title="<%= GetTag().Name%>" />
Sign up to request clarification or add additional context in comments.

4 Comments

He's calling the variable because he wants to reuse the value in at least two places.
I made changes suggested by @JoshEarl, then replaced 'name' with 'GetTag().Name' and it's now working fine.
But I don't get why GetTag().Name works, but name doesn't? So. Weird.
@Gage, it needs to be Title="<%# GetTag().Name %>" with the <%# at the start (<%= doesn't work).
2

Try this replacing the <%= %> tags with <%# %> and adding a call to this.DataBind(); in your codebehind.

1 Comment

OK, added this.DataBind() as the first line of the Render method. I get a compiler error: The name 'name' does not exist in the current context. It looks like it's complaining about the 'Title="<%# name %>"' parameter...
1

how about setting that value in code-behind:

this.BarGenie1.Title = GetTag().Name;

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.