更改特定链接按钮的颜色

发布于 2024-09-13 11:22:50 字数 1362 浏览 8 评论 0 原文

我正在使用一系列链接按钮 Az,它们是动态创建的,单击每个按钮时,其文本颜色会更改为其他内容,以使其与其他按钮不同,我正在做的事情

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实现

i am using a series of linkbuttons A-z which are dynamically created what i want on the click of each its text color change to something else to make it different from others what i am doing

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

it is working fine but on clicking more then one buttons all the buttons which are clicked changes their color to orange but what i want is whichever button i click only that button color should change on click of next button previous button should go to default state is this approach right or tell me if it can be achieved by css

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

牛↙奶布丁 2024-09-20 11:22:50

您的问题是,链接按钮的 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 之间)。然后在命令事件中,单击的按钮将附加新样式。此样式中的颜色设置将覆盖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;
    }

Your problem is that the ViewState of the linkbuttons is being saved just before the controls are rendered, including the updated styling. Then, on your postback, after Page_Init, the ViewState is re-applied to each control, with the orange styling. This overrides the setting that you add in Page_Init. So in Page_Load, you need to reset the styling on each of the controls.

Add another style to your stylesheet

.highlighted { color:orange; }

In lbtnCharacter_Command, replace

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

with

lbtn.CssClass = "firstCharacter highlighted ";

In Page_Load, add:

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

On each Page_Load, all of the linkbuttons css class will be reset to the default. This is after the ViewState has been applied to them (between PageInit and PageLoad). Then on the Command event, the clicked button will have the new style appended. The color setting in this style will override whatever color setting is in the firstCharacter style.

UPDATE

    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;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文