2010-06-22 14 views
0

我在aspx頁面下面的代碼Asp.NET font-size和服務功能

<asp:Label ID="CittaLabel" runat="server" Text='<%# Eval("Citta") %>' Font-Size='<%# ReturnFontSize(Eval("Big")) %>'/>

,這背後是服務功能

Protected Function ReturnFontSize(ByVal Big As Boolean) As FontUnit 
    If Big Then 
     ReturnFontSize = FontSize.Medium 
    Else 
     ReturnFontSize = FontSize.Small 
    End If 
End Function 

我的代碼,但我得到一個總字體非常小。 所以我的問題是:爲了改變一個控件的「字體大小」proprety,從後面的代碼,我必須使用哪種返回類型,假設FontUnit不起作用?

謝謝

回答

0

你有數據綁定標籤的一個ContainerControl(f.e。一個GridView或完整的頁面)。然後,您可以從aspx頁面調用Codebehind功能。

因此f.e.在頁面加載:

Protected Function ReturnFontSize(ByVal fontSize As Object) As FontSize 
    Select Case fontSize.ToString.ToUpper 
     Case "BIG" 
      Return WebControls.FontSize.Large 
     Case Else 
      Return WebControls.FontSize.Medium 
    End Select 
End Function 

和ASPX頁面上:

Me.DataBind() 

和功能必須從類型字號返回一個對象

Font-Size='<%# ReturnFontSize(Eval("Big")) %>' 

但是你爲什麼不設置字體大小在Page.Load的Codebehind上?

Me.CittaLabel.FontSize= .... 
+0

我在一個列表視圖,所以我更喜歡避免在使用的ItemDataBound – stighy 2010-06-22 12:39:53

+0

PS:也返回字號類型的頁面渲染尺寸較小的字體(7PT) – stighy 2010-06-22 12:40:53

+0

FontSize.Medium是7PT ;-) 你有你的數據綁定列表視圖。 – 2010-06-22 12:44:12

0

我發現一個綁定的事件過程中進行評估和演示基於屬性的設置往往可以證明是有問題,除非你綁定到簡單的類型,如字符串和整數。在執行綁定事件(如Repeater數據綁定事件)後的代碼期間,通常更簡單並且簡單地執行復雜的綁定任務。

嘗試,而不是下面,假設你使用的是ASP:Repeater控件:

標記:

<asp:Repeater runat="server" ID="rpt" OnDataBinding="rpt_OnDataBinding"> 
<ItemTemplate> 
<asp:Label ID="CittaLabel" runat="server" Text='<%# Eval("Citta") %>' /> 
</li> 
</ItemTemplate> 
</asp:Repeater> 

代碼背後:

protected void rpt_DataBinding(object sender, RepeaterItemEventArgs e) 
{ 
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) 
    { 
    var data = (YourTypeThatIsDataBound)e.Item.DataItem; 
    var CittaLabel = (Label)e.Item.FindControl("CittaLabel"); 
    CittaLabel.FontSize = ReturnFontSize(data.Big); 
    } 
} 

這種方式爲每個項目中產生您的中繼器訪問服務器端數據綁定事件中的標籤,並簡單地將FontSize設置爲您已寫入的您的ReturnFontSize函數的輸出。你唯一需要做的就是將e.Item.DataItem對象轉換回中繼器綁定的原始對象類型,然後將其Big屬性傳遞給該函數。