2010-05-28 106 views
1

我遇到下面的問題。 我有一個ASPX頁面,沒有數據綁定和語法如下:Asp.net:使用的<%#或<%

<asp:label runat=server.... text='<%# MyFunction("parameter") %>' /> 

MyFunction的聲明如下:

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

    end if 
end function 

但ASP.NET不評價MyFunction的。

什麼就是我的問題呢?

回答

2

<%#用於數據綁定表達式。通常你會看到<%# Eval("FieldName") %>。如果你想打電話給你定義的方法代碼隱藏,你應該使用<%= MyFunction("param") %>

這就是說...你應該考慮不同的方法。這將是更好的(使用現有的例子)做一些事情如下:

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

與您的代碼隱藏下面一起:

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 

這種方法可以確保您的UI聲明一無所知在代碼隱藏方法中定義的方法,增加了系統的整體可維護性。

+0

嗯..這是行不通的。在HTML中,它返回字符串<%= MyFunction的(「參數」)%> – stighy 2010-05-28 20:31:53

+0

@stighy:那是因爲我沒有在第一次發現它的標籤的聲明中。它永遠不會那樣工作。您必須使用我在編輯中列出的方法。 – Randolpho 2010-05-28 20:33:15

+0

好吧,我解決了..如果我在控件(標籤,文字)內使用<%= ...%>,它會返回字符串。但是,如果單獨使用它,而不是「綁定」控件的文本屬性,它就可以工作。 Thorks Randolpho! – stighy 2010-05-28 20:36:52