change font style in c#

前端 未结 4 548
不思量自难忘°
不思量自难忘° 2020-12-12 00:42

hi there I want to change the font style of a label in an aspx page, through choosing options provided by a dorpdownlist. None of these methods work. I know I might be on th

相关标签:
4条回答
  • 2020-12-12 00:45

    here is my code

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace WebApplication2
    {
        public partial class _Default : System.Web.UI.Page
        {
        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
    
        protected void Button1_Click(object sender, EventArgs e)
        {
            Label1.Font.Name = "Verdana";
        }
    }
    

    }

    and it is working, i just need you to make sure that before you run the application you set a fontname to your label because the fontname is empty when you put it in your page,(it doesnt work if you dont set a fontname initially) you need to set it then yuse the code i wrte above.open the properties window click the label and click font then choose a name for font name open the properties window click the label and click font then choose a name for font name

    0 讨论(0)
  • 2020-12-12 00:50

    Do you need to make it this way? It's not a 'nice' solution anyway. It's much better to assign an css class. I haven't seen this way before... but I would say that it's comming from WinForms.

    Use CssClass instead:

    label1.CssClass = "SomeClass";
    

    And define styling in your stylesheet:

    .SomeClass { font-family: "Times New Roman"; font-size: 1.2em; }
    
    0 讨论(0)
  • 2020-12-12 01:00

    On web we do not create new Font this works for desktop programming and the new Font are made on server not on client.

    The Label contains the .Font but not to set a new font, but to actually create a new inline style, and the object is the FontInfo (not the Font).

    From MSDN FontInfo here is an example:

    // Note that myLabel.Font is a FontInfo object.
    
    myLabel.Font.Bold = true;
    myLabel.Font.Italic = false;
    myLabel.Font.Name = "verdana";
    myLabel.Font.Overline = false;
    myLabel.Font.Size = 10;
    myLabel.Font.Strikeout = false;
    myLabel.Font.Underline = true;
    
    // Write information on the FontInfo object to the myLabel label.
    myLabel.Text = myLabel.Font.ToString();
    

    The final render of this contains inline style to give this properties to the text that is inside a span or a div. Is better of course to give a global css class, but some times you need to interfere with inline style.

    0 讨论(0)
  • 2020-12-12 01:07

    You're better off using jquery

    Bind an event handler to the onchange event on the select dropdown and according to the value then change the css class. This has the benefits of a - not being server side and avoiding a hit on the server b - easier c - cleaner

    edit : Something like this could be adapted

    jQuery onchange/onfocus select box to display an image?

    0 讨论(0)
提交回复
热议问题