0

I would like to write some code in an asp.net Button Text property. Is this possible?

This is how I tried it, but this doesn't work

<% 
    foreach (Reference reference in allReferences)
    {
%>
        <asp:Button Text="<%= reference.Title %>" ID="myButton" runat="server" />
<%
    }           
%>

When this is rendered, it shows literally the string "<%= reference.Title %>".

Is there another syntax, or something else?

Thanks, Vincent

2 Answers 2

2

it looks to me you are using ASP.NET with the old ASP classic approach, in web forms you can do something like this which is, in my opinion, way cleaner and nicer:

foreach(Reference reference in allReferences)
{
    Page.Controls.Add(new Button(reference.Title));
}

Note: this is pseudo code, in the real code you would get an instance of a new Button object, set also other properties like Id, Text, Click eventHandler and you would add it to a certain container like a div, not to a random position in the page like I did. Also, it depends a lot where you add this code snippet, if in Page_Load or, as usual. in Page_Init.

Sign up to request clarification or add additional context in comments.

Comments

1

you need to use '<%= reference.Title %>' , so use a single quote instead of double

2 Comments

Thanks for your reply. But it still shows litterally that string.
can you please try '<%# reference.Title %>' and let me know if it works?

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.