开发者

Asp.net : use of <%# or <%

i'm having the following problem. I've a ASPX page with no databound and the following syntax:

<asp:label runat=s开发者_StackOverflow中文版erver.... text='<%# MyFunction("parameter") %>' />

MyFunction is declared as follow:

protected function MyFunction(par as string) as string
  if par = "1" then
     MyFunction="something"
  else

  end if
end function

But ASP.NET don't evaluate MYFUNCTION .

What's i'm going wrong ?


<%# is used for data binding expressions. Typically you'll see <%# Eval("FieldName") %>. If you're trying to call a method defined in your code-behind, you should use <%= MyFunction("param") %>

That said... you should consider a different approach. It would be better to do something as follows (using your existing example):

<asp:label name="MyLabel" runat="server" />

along with the following in your code-behind:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    MyLabel.Text = MyFunction("param")
End Sub

Protected Function MyFunction(par as string) as string
  if par = "1" then
     MyFunction="something"
  else

  end if
End Function

This approach ensures that your UI declaration knows nothing about methods defined on your code-behind, increasing the overall maintainability of your system.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜