Reputation: 4497
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
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
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
Reputation: 3221
You need to make the method static and try this:
<%# GetTranslate("Admin_HeaderInfo")%>
Upvotes: 0
Reputation: 67080
You can't use <%
and %>
inside a server a tag with runat="server"
. You can set that property from code.
Upvotes: 3