开发者

Asp.NET Font-Size and service function

i've the following code in aspx page

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

and this is my code behind service function

Protected Function ReturnFontSize(ByVal Big As Boolean) As Fon开发者_如何转开发tUnit
    If Big Then
        ReturnFontSize = FontSize.Medium
    Else
        ReturnFontSize = FontSize.Small
    End If
End Function

But i get always a font very very small. So my question is : for changing "Font-Size" proprety of a control, from code behind, which return type i have to use, assuming that FontUnit not work ?

Thank you


You have to DataBind the Containercontrol of the label(f.e. a GridView or the complete page). Then you can call a Codebehind Function from the aspx-page.

Hence f.e. in Page-Load:

Me.DataBind()

and the function must return an object from type FontSize:

 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

and on aspx-page:

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

But why dont you set the Fontsize in Codebehind on Page.Load?

Me.CittaLabel.FontSize= ....


I find that performing Eval based property setting during a databinding event can often prove problematic unless you are binding to simple types such as strings and ints. It's generally easier and to simply perform your complex binding tasks during the code behind implementation of binding events such as the Repeater databind event.

Try the following instead, assuming you are using an ASP:Repeater control:

Markup:

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

Code Behind:

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);
    }
}

This way for every item being generated in your repeater you access the label in the server side databinding event and simply set the FontSize to be the output of your ReturnFontSize function that you have alread written. The only thing you have to do is cast the e.Item.DataItem object back to the original type of object the repeater was bound to and then pass its Big property into the function.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜