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. 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开发者_StackOverflow中文版 Label("Times New Roman");
Label1.Font= new1;
}
}
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?
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; }
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.
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.
精彩评论