0

I need to bind an ASP.NET control something like so:

  <asp:label ID="lblName" Text=<%# GetName()) %>

and in CodeBehind file I have this method:

   protected string GetName()
   {
      ...
   }

Is this right, or how I can do something like this?

2 Answers 2

1

Methods in code-behind need to be public I believe; I could be wrong, but I've gotten this to work:

<asp:label ID="lblName" Text='<%= GetName() %>' />

With

public string GetName()
{
      ...
}

HTH

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

2 Comments

in GetName() could you just return "Something" ?
@Ranhiru, you could return whatever you wanted from this method. You can return objects too, and then do something like: <%= GetName().PropertyOfObject %>.
1

Try:

<asp:Label ID="Status" runat="server"><%# this.Test() %></asp:Label>

The above code assumes that you have a method called Test() with public access that returns a string in its implementation file.

2 Comments

Thanks! I can write in this way only for default property. But how about another?
What do you mean by how about another?? You can access public and protected property from your markup, but not private.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.