更改特定链接按钮的颜色
我正在使用一系列链接按钮 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的问题是,链接按钮的 ViewState 在呈现控件之前保存,包括更新的样式。然后,在回发时,在 Page_Init 之后,ViewState 将使用橙色样式重新应用于每个控件。这会覆盖您在 Page_Init 中添加的设置。因此,在 Page_Load 中,您需要重置每个控件的样式。
在样式表中添加另一种样式
在 lbtnCharacter_Command 中,替换
为
在 Page_Load 中,添加:
在每个 Page_Load 上,所有链接按钮 css 类都将重置为默认值。这是在 ViewState 应用于它们之后(在 PageInit 和 PageLoad 之间)。然后在命令事件中,单击的按钮将附加新样式。此样式中的颜色设置将覆盖firstCharacter 样式中的任何颜色设置。
更新
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
In lbtnCharacter_Command, replace
with
In Page_Load, add:
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