改变特定链接按钮的颜色

时间:2010-08-06 07:24:49

标签: asp.net linkbutton

我正在使用一系列链接按钮A-z动态创建我想要点击其每个文本颜色更改为其他东西,使其与其他人不同我正在做什么

protected void Page_Init(object sender, EventArgs e)
    {
        // Adding Dynamically linkbuttons for all alphabets(i.e. A-Z)
        for (char asciiValue = 'A'; asciiValue <= 'Z'; asciiValue++)
        {
            LinkButton lbtnCharacter = new LinkButton();
            lbtnCharacter.ID = "lbtnCharacter" + asciiValue;
            divAlphabets.Controls.Add(lbtnCharacter);

            lbtnCharacter.Text = Convert.ToString(asciiValue);
            lbtnCharacter.CssClass = "firstCharacter";
            lbtnCharacter.ToolTip = "Show users whose name starts with '" + Convert.ToString(asciiValue) + "'";
            lbtnCharacter.CommandArgument = Convert.ToString(asciiValue);
            lbtnCharacter.Command += new CommandEventHandler(lbtnCharacter_Command);
        }
    }
void lbtnCharacter_Command(object sender, CommandEventArgs e)
    {
        ViewState["Selected_Character"] = e.CommandArgument;
        LinkButton lbtn = (LinkButton)divAlphabets.FindControl("lbtnCharacter" + e.CommandArgument);
        lbtn.ForeColor = System.Drawing.Color.Orange;
        txtNameFilter.Text = string.Empty;
        BindUserList();
    }

它工作正常,但点击多个按钮所有点击的按钮将其颜色更改为橙​​色但我想要的是无论哪个按钮我只点击该按钮颜色应该更改点击下一个按钮上一个按钮应该去默认状态是这种方法是正确的还是告诉我它是否可以通过css实现

1 个答案:

答案 0 :(得分:2)

您的问题是链接按钮的ViewState正在呈现控件之前保存,包括更新的样式。然后,在回发后,在Page_Init之后,将ViewState重新应用于每个控件,并使用橙色样式。这将覆盖您在Page_Init中添加的设置。因此,在Page_Load中,您需要重置每个控件的样式。

在样式表中添加另一种样式

.highlighted { color:orange; }

在lbtnCharacter_Command中,替换

lbtn.ForeColor = System.Drawing.Color.Orange;

lbtn.CssClass = "firstCharacter highlighted ";

在Page_Load中,添加:

foreach (var ctrl in divAlphabets.Controls)
{
    if (ctrl is LinkButton)
        ((LinkButton)ctrl).CssClass = "firstCharacter";
}

在每个Page_Load上,所有链接按钮css类都将重置为默认值。这是在ViewState应用于它们之后(在PageInit和PageLoad之间)。然后在Command事件中,单击的按钮将附加新样式。此样式中的颜色设置将覆盖firstCharacter样式中的任何颜色设置。

<强>更新

    protected void Page_Init(object sender, EventArgs e) {
        for (char asciiValue = 'A'; asciiValue <= 'Z'; asciiValue++) {
            var lbtnCharacter = new LinkButton {
                ID = "lbtnCharacter" + asciiValue,
                Text = Convert.ToString(asciiValue),
                ToolTip = "Show users whose name starts with '" + Convert.ToString(asciiValue) + "'", 
                CommandArgument = Convert.ToString(asciiValue)
            };
            lbtnCharacter.Command += lbtnCharacter_Command;
            divAlphabets.Controls.Add(lbtnCharacter);
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["CurrentLetter"] != null) {
            foreach (var ctrl in divAlphabets.Controls) {
                if (ctrl is LinkButton) {
                    if (((LinkButton) ctrl).Text == Session["CurrentLetter"].ToString()) {
                        ((LinkButton) ctrl).CssClass = "firstCharacter highlighted";
                    }
                }
            }
        }
    }

    void lbtnCharacter_Command(object sender, CommandEventArgs e) {
        //Reset all of the other buttons only when clicking a new one
        foreach (var ctrl in divAlphabets.Controls) {
            if (ctrl is LinkButton) {
                ((LinkButton) ctrl).CssClass = "firstCharacter";
            }
        }
        //Set the clicked button and save the Session state
        var lbtn = (LinkButton)divAlphabets.FindControl("lbtnCharacter" + e.CommandArgument);
        lbtn.CssClass = "firstCharacter highlighted";
        Session["CurrentLetter"] = lbtn.Text;
    }