Mennan
Mennan

Reputation: 4497

Use asp.net expression to define server tag in text value?

I have a function in codebehind and want to use it on aspx.

codebehind :

    public string GetTranslate(string Text)
    {
       return Glob.GetTranslate(Text);
    }

aspx :

<asp:LinkButton Text='<%= GetTranslate("Admin_HeaderInfo")%>' id="blabla" runat="server" />

result :

LinkButton Text On Page => "<%= GetTranslate("Admin_HeaderInfo")%>"

Upvotes: 3

Views: 3272

Answers (4)

Richard Friend
Richard Friend

Reputation: 16018

We have used ExpressionBuilders in the past for this type of thing, they work pretty well and are available even if you are not databinding.

We use the Code Expression Builder in some of our older WebForms projects.

See this article for other details about Expression Builders

This will allow you a syntax like

<asp:Label runat="server" Text='<%$ Lookup : SomeLookupValue %>'></asp:Label>

More explained on this SO post

Upvotes: 2

StuartLC
StuartLC

Reputation: 107237

You can use DataBinding, i.e. <%#, however then you will need to explicitly call DataBind() from your code behind, i.e.

.aspx

<asp:LinkButton  Text='<%#GetTranslate("Admin_HeaderInfo")%>' id="blabla" runat="server" />

Code Behind:

    protected void Page_Load(object sender, EventArgs e)
    {
        blabla.DataBind();
    }

As Adriano mentioned, the other way is to set it from code behind, e.g.:

    protected void Page_Load(object sender, EventArgs e)
    {
        blabla.Text = GetTranslate("Admin_HeaderInfo");
    }

Note that you will need to take PostBack and page lifecycle aspects into account when using determining where to place the code behind.

Upvotes: 3

Dave Hogan
Dave Hogan

Reputation: 3221

You need to make the method static and try this:

<%# GetTranslate("Admin_HeaderInfo")%>

Upvotes: 0

Adriano Repetti
Adriano Repetti

Reputation: 67080

You can't use <% and %> inside a server a tag with runat="server". You can set that property from code.

Upvotes: 3

Related Questions