views:

14

answers:

2

Hi, there!

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?

A: 

Hey,

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

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

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.

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