tags:

views:

2815

answers:

5

I have some link buttons in which I am dynamically adding a style to it. I am doing the following in a method:

LinkButton lb = new LinkButton();
lb.Style["font-weight"] = "bold";

When the another link is clicked, it should unbold the link button that is bold and bold the currently clicked one, so in the method that is doing this, I have tried:

lb.Style["font-weight"] = "none";

The above does not work though, the previously selected link stays bold.

I just realized the possible problem. I am creating multiple links and what it looks like is that since all the links are named lb, it never removes the bold. I am trying to think of a way for it to remember the previously selected link and to only unbold that one.

A: 

Try ListBox1.Attributes.Add("style","font-weight:bold"); and ListBox1.Attributes.Add("style","font-weight:normal");

or even better is

// css

.active {font-weight:bold}
.notactive {font-weight:normal}

//c#

ListBox1.CssClass = "active";
ListBox1.CssClass = "notactive ";
DrG
The bold works, it is when I want to clear the bold, that it does not work.
Xaisoft
+1  A: 

Try lb.Style.Remove("font-weight"). I didn't test it, but you can try it out.

Alternatively, have you tried settings the Font.Bold property?

lb.Font.Bold = true;
Max Schmeling
Thanks for pointing this out, I actually forgot that I could set the it bold this way.
Xaisoft
+5  A: 

Can I suggest an alternative approach?

Set a CSS Style:

.selected { font-style: bold; }

When a link is clicked set that link's CSS class to "selected" and the others to "";

EDIT: To accommodate for existing Css Class

const string MY_CLASS = "links";
lb1.CssClass = MY_CLASS + " selected"; // selected
lb.CssClass = MY_CLASS; // not selected

You can quickly get into trouble when defining inline styles, in that they're difficult to overwrite.

EDIT 2:

Something like this code should work. You may have to loop through all the LinkButtons in the list, but I don't think so. I'd just turn off ViewState on the LinkButtons.

// container for links. so you can reference them 
// outside of the creation method if you wish. I'd probably call this method in the  
// Page_Init Event.

List<LinkButton> listOfLinks = new List<LinkButton>();
const string MY_LB_CLASS = "linkButton"; // generic lb class


private void createSomeLinks() {

    for (int i = 0; i < 10; i++) {
        // create 10 links.
        LinkButton lb = new LinkButton() 
        { 
            ID = "lb" + i, 
            CssClass = MY_LB_CLASS 
        };
        lb.Click += new EventHandler(lb_Click); // Add the click event
    }

    // You can bind the List of LinkButtons here, or do something with them.
}

void lb_Click(Object sender, EventArgs e) {

    LinkButton lb = sender as LinkButton; // cast the sender as LinkButton
    if (lb != null) {
        // Make the link you clicked selected.
        lb.CssClass = MY_LB_CLASS + " selected"; 
    }
}
Atømix
Actually, I am doing this, but I already have a CssClass that is being used for that.
Xaisoft
What do you mean? Can you explain this further?
Atømix
I have a cssclass that I am setting on the links. That class is never removed. The only time the link is bolded, is when it is selected. I want to remove the bold style when another link is selected.
Xaisoft
You can use more than one CssClass on a link. So, you would just set it like so: lb.CssClass += " selected"; and that should add the both CssClasses
Atømix
Did it work for you?
Atømix
No, check out my addition to the original question. I am actually creating multiple links.
Xaisoft
Can you post your code. It's unclear what you mean by multiple links. Are you creating these links completely in code? Probably need some context to answer properly.
Atømix
If this answered your question, feel free to mark it as so.
Atømix
A: 

you could try lb.Style.Remove("font-weight");

M. Mather
A: 

set the font bold in the click event of the link button and set the enable view state property to false in the click event itself which will reset the link to the normal foam in the other click