2

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 Label("Times New Roman");
    Label1.Font= new1;
  }
} 
3

4 Answers 4

3

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?

Sign up to request clarification or add additional context in comments.

3 Comments

i love jquery now. I've spent the last 2 years fighting with web-forms and have come to the conclusion that this is a hell of a lot easier. When you start messing with update panels and dynamic controls you're in for a world of pain. I see the light, halluleuah
LOL, I'm in pain now..must be cuz I'm not using jquery
Zen_2011 - not yet but when you try and do something complex you soon will be ... trust me on this...
2

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

5 Comments

It's one of the main gripes I have against Webforms that it allows for this type of code to exist. As you say, it's much better to apply a CSS class to it.
Its a WebForm. This is the way I went about doing it, which may not be good. But I don't want to do a CSS Class. I just want to change font on a button click event.
no, this is not the solution, surely it would mean a trip back to the server wouldn't it?
@Gordon: How is this not a solution? And why is everyone talking that much about server? He is in server side context since beginning so that's what I proposed. Of course that he can do this easily with jQuery. I am just not the person that response "jQuery" to every problem :)
My app is strictly server-side development guys. yheeshhh jquery, I'll have to learn that stuff. Is that really the only other option I have?
2

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

6 Comments

I didn't have a name set, so I did what you said,I set my label Font-Names= "name";. but it still doesn't work for me, I wonder if there is another error blocking this method from working. Thanks for your answer! :)
@Zen_2011 not at all. i think there is misunderstanding with names, what you should do is click the lable go to the properties window click font and click the whitepart in front of Font and choose a font type like Verdana or Times New Roman. If it still doesnt work try recreating the label and then the webpage.Good luck.
@EmreVeriyaz We seem to be in different environments, my properties don't look like yours, I don't have a font option to set. I have Font-Names:"where i can type in the name", Font-Bold:"set to true or false", etc.
So you did this Font-Names= "Times New Roman"; oh ok, well i am nearly sure the reason your code doesnt work is something like that, i hope you can solve it with time.
@EmreVeriyaz Great news!!!, I used ur method, with setting the Font-Names="...",Cleaned up my code, because I felt that other errors were affecting output, and it worked! Everything works beautifully. Thanks a Million :D
|
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.