简体   繁体   English

更改点击时链接按钮的颜色?

[英]Changing the color of a linkbutton on click?

I have a user control called courses that displays all the available courses. 我有一个名为课程的用户控件,该控件显示所有可用的课程。 In my user control file, I use a repeater control to display the courses. 在我的用户控件文件中,我使用中继器控件来显示课程。 All the course names are rendered as linkbuttons inside itemtemplate. 所有课程名称均在itemtemplate中呈现为链接按钮。 I use this user control in page called foo.aspx. 我在名为foo.aspx的页面中使用此用户控件。 I am using a javascript function to change the color of the value inside the itemtemplate when clicked. 我使用javascript函数来单击时更改itemtemplate内的值的颜色。 The color changes for a second when I click, but it goes back to its original color. 单击时颜色会改变一秒钟,但会恢复为原始颜色。 Does anyone know what I am doing wrong here? 有人知道我在这里做错了吗?

My javascript function. 我的JavaScript函数。

<script>
function changeColor(e) {
    e.style.color = "red";
}
</script>

<asp:LinkButton ID="LinkButton1" OnClientClick="return changeColor(this);" runat="server">LinkButton</asp:LinkButton>

As I said on your comment, be careful about page PostBack . 正如我对您的评论所说,请谨慎对待PostBack页面。

To prevent your LinkButton color reseted, I think you should save them inside ViewState 为了防止您的LinkButton颜色重置,我认为您应该将它们保存在ViewState中

I don't know whether it's the best solution or not, but at least I've try to help :) 我不知道这是否是最好的解决方案,但至少我已经尽力了。

Here is the aspx: 这是aspx:

<asp:Repeater ID="myRepeater" OnItemCommand="myRepeater_ItemCommand" runat="server">
     <ItemTemplate>
           <asp:LinkButton ID="myLinkButton" runat="server" CommandName="CHANGE_COLOR" ForeColor="<%# System.Drawing.Color.FromName(Container.DataItem.ToString()) %>">Your Text</asp:LinkButton>
     </ItemTemplate>
</asp:Repeater>

Here is the code behind (C#): 这是(C#)背后的代码:

First create the ViewState (this is the key to prevent data lost after PostBack ) 首先创建ViewState (这是防止PostBack之后数据丢失的关键)

List<string> listData
{
    set { ViewState["listData"] = value; }
    get
    {
        if (ViewState["listData"] == null)
            return new List<string>();
        else
            return (List<string>)ViewState["listData"];
    }
}

On the page Load: 在页面上加载:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            listData = new List<string>();
            listData.Add("Blue");
            listData.Add("Blue");
            listData.Add("Blue");
        }

        myRepeater.DataSource = listData;
        myRepeater.DataBind();
    }

When you click the LinkButton the repeater will trigger OnItemCommand 当您单击LinkButton ,转发器将触发OnItemCommand

protected void myRepeater_ItemCommand(object sender, RepeaterCommandEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            if (e.CommandName == "CHANGE_COLOR")
            {
                LinkButton linkButton = (LinkButton)e.Item.FindControl("myLinkButton");
                if (linkButton.ForeColor != System.Drawing.Color.Red)
                {
                    linkButton.ForeColor = System.Drawing.Color.FromName("Red");
                    listData[e.Item.ItemIndex] = "Red"; //This is the key! This will prevent your color reset, as we save them inside ViewState
                }
                else
                {
                    linkButton.ForeColor = System.Drawing.Color.FromName("Blue");
                    listData[e.Item.ItemIndex] = "Blue";
                }
            }
        }
    }

Create two CSS classes and check in your repeater if the link is enabled or disabled and show css class based on that condition, something like this: 创建两个CSS类,并检查中继器是否启用了链接,并根据该条件显示CSS类,如下所示:

.ActiveLinkBtn
{
    color:red;
    font:bold 12px Tahoma;
}

.InActiveLinkBtn
{
    color:blue;
}

Then change the repeater as following 然后按以下方式更改中继器

<asp:Repeater ID="rptPager" runat="server">
<ItemTemplate>
   <asp:LinkButton ID="lnkPage" runat="server" Text='<%#Eval("Text") %>' 
   CommandArgument='<%# Eval("Value") %>'
   Enabled='<%# Eval("Enabled") %>' OnClick="Page_Changed" CssClass='<%#      
   Convert.ToBoolean(Eval("Enabled")) ? "ActiveLinkBtn" :"InActiveLinkBtn"%>'>
   </asp:LinkButton>
</ItemTemplate>
</asp:Repeater>

Here is the reference link Change ASP.NET LinkButton color on click 这是参考链接, 在单击时更改ASP.NET LinkBut​​ton颜色

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM