3

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")%>"

4 Answers 4

3

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

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

5 Comments

Yes if the link must be runat=server, otherwise don't use runat=server and that syntax will work.
yes you can - with an expression builder 4guysfromrolla.com/articles/022509-1.aspx
@RichardFriend an expression builder is pretty different! He has to call a method (static in the class or from the Page object) with parameters (in this case a constant but it's not a rule). They have really different purposes. If you have to write code...well first you should ask yourself why you can't do it with...code.
I disagree - how is it any less code than a normal data binding expression is code : you write code for the expression builder and markup to use it. Sounds like code to me..
@RichardFriend well, it may be a point of view but I see expression builder as something static. If you need an explicit method call or some logic (even if you may use C.E.B.) then I think it's more clear to put that in codebehind (at least it'll make you page more readable). Moreover I should say I think in this case (for translation) he shouldn't force to use code but he may refactor to use as an expression builder in the way they're intended: Text='<%$ Translate : Admin_HeaderInfo %>'.
3

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.

Comments

2

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

Comments

0

You need to make the method static and try this:

<%# GetTranslate("Admin_HeaderInfo")%>

1 Comment

Sorry I thought you were binding.

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.