简体   繁体   English

在C#中更改字体样式

[英]change font style in c#

hi there I want to change the font style of a label in an aspx page, through choosing options provided by a dorpdownlist. 嗨,我想通过选择dorpdownlist提供的选项来更改aspx页面中标签的字体样式。 None of these methods work. 这些方法均无效。 I know I might be on the right path, but just can't seem to get it right. 我知道我可能走在正确的道路上,但似乎做对了。 Please enlighten me. 请赐教。 Thanks In my code behind: 谢谢在我后面的代码中:

public void button1_Click(object sender, EventArgs e)
{
  var select= drop1.SelectedItem.Value;
  if(select == "Times New Roman")
  {
    // I tried doing all of these:
    label1.Font= new Font("Times New Roman", label1.Font.Size);
    //or
    label1.Font.Name ="Times New Roman";
    //or
    Label new1 = new Label("Times New Roman");
    Label1.Font= new1;
  }
} 

You're better off using jquery 你最好使用jQuery

Bind an event handler to the onchange event on the select dropdown and according to the value then change the css class. 在选择下拉列表上将事件处理程序绑定到onchange事件,然后根据值将其更改为CSS类。 This has the benefits of a - not being server side and avoiding a hit on the server b - easier c - cleaner 这样做的好处是-不属于服务器端并且避免对服务器造成冲击b-易于操作c-清洁器

edit : Something like this could be adapted 编辑:像这样的东西可以适应

jQuery onchange/onfocus select box to display an image? jQuery onchange / onfocus选择框以显示图像?

Do you need to make it this way? 您需要这样做吗? It's not a 'nice' solution anyway. 无论如何,这不是一个“不错”的解决方案。 It's much better to assign an css class. 最好分配一个CSS类。 I haven't seen this way before... but I would say that it's comming from WinForms. 我以前从未见过这种方式...但是我想说它来自WinForms。

Use CssClass instead: 改用CssClass:

label1.CssClass = "SomeClass";

And define styling in your stylesheet: 并在样式表中定义样式:

.SomeClass { font-family: "Times New Roman"; font-size: 1.2em; }

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 而且它正在工作,我只需要确保在运行应用程序之前将字体名称设置为标签,因为当您将其放在页面中时字体名称为空( 如果您最初不设置字体名称则无法使用 )您需要进行设置,然后使用上面写的代码。打开属性窗口,单击标签并单击字体,然后为字体名称选择一个名称 打开属性窗口,单击标签,然后单击字体,然后为字体名称选择一个名称

On web we do not create new Font this works for desktop programming and the new Font are made on server not on client. 在网络上,我们不会创建new Fontnew Font可用于桌面编程,并且新字体是在服务器而非客户端上创建的。

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). Label包含.Font但不设置新字体,而是实际创建新的内联样式,并且对象是FontInfo (不是Font)。

From MSDN FontInfo here is an example: MSDN FontInfo这里是一个示例:

// 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. 最终的渲染包含内联样式,以将此属性赋予跨度或div内的文本。 Is better of course to give a global css class, but some times you need to interfere with inline style. 当然,最好提供一个全局的CSS类,但是有时您需要干扰内联样式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM