如何获取之前在Repeater控件中单击的LinkBut​​ton的ID?

时间:2012-11-22 14:37:19

标签: asp.net c#-4.0

我有一个转发器控件,里面有LinkButton。使用命令参数,我在代码隐藏中执行必要的功能。我想点击LinkButton的前景色为红色,如果在LinkButton中点击了另一个Repeater,我想关闭之前点击的LinkButton黑色,最近点击一个红色。我怎么做?这是我的代码:

<asp:Repeater ID="Repeater1" runat="server">
    <HeaderTemplate>
        <ul>
    </HeaderTemplate>
    <ItemTemplate>
            <li>
                <asp:LinkButton ID="LinkButton1" runat="server" Text='<%#Eval("divisionlabel") %>'
                                OnCommand="LinkButton1_Command" CommandName="MyPendingApps" CommandArgument='<%#Eval("divisionid") %>'>
                </asp:LinkButton>
            </li>
    </ItemTemplate>
    <FooterTemplate>
            <li>
                <asp:LinkButton ID="LinkButton_InsReqList" runat="server" OnCommand="LinkButton_InsReqList_Command"
                                CommandName="InspectionRequests">Inspection Requests List</asp:LinkButton>
            </li>
        </ul>
    </FooterTemplate>
</asp:Repeater>

2 个答案:

答案 0 :(得分:1)

在表单

上的每个链接按钮中添加OnClick="LinkButtons_Click"
<asp:Repeater ID="Repeater1" runat="server">
    <HeaderTemplate>
        <ul>
    </HeaderTemplate>
    <ItemTemplate>
            <li>
                <asp:LinkButton ID="LinkButton1" runat="server" Text='<%#Eval("divisionlabel") %>'
                                OnCommand="LinkButton1_Command" CommandName="MyPendingApps" CommandArgument='<%#Eval("divisionid") %>' OnClick="LinkButtons_Click">
                </asp:LinkButton>
            </li>
    </ItemTemplate>
    <FooterTemplate>
            <li>
                <asp:LinkButton ID="LinkButton_InsReqList" runat="server" OnCommand="LinkButton_InsReqList_Command"
                                CommandName="InspectionRequests" OnClick="LinkButtons_Click">Inspection Requests List</asp:LinkButton>
            </li>
        </ul>
    </FooterTemplate>
</asp:Repeater>

并且该函数的代码是:

protected void LinkButtons_Click(object sender, EventArgs e)
{
    LinkButton link=(LinkButton)sender;
    if (link.ID == "yourDesiredID1")
    {
           link.ForeColor=System.Drawing.Color.Red; 
    }
    else
    {
           link.ForeColor=System.Drawing.Color.YourDefaultdColor; 
    }
    if (link.ID == "yourDesiredID2")
    {
           link.ForeColor=System.Drawing.Color.Yellow; 
    }
    else
    {
           link.ForeColor=System.Drawing.Color.YourDefaultColor; 
    }
    if (link.ID == "yourDesiredID3")
    {
           link.ForeColor=System.Drawing.Color.Blue; 
    }
    else
    {
           link.ForeColor=System.Drawing.Color.YourDefaultColor; 
    }
}

答案 1 :(得分:0)

我将在ViewState中的ItemCommand事件服务器端存储唯一标识符(value,Id,ItemIndex等),然后在ItemDataBound事件中检查唯一标识符是否等于ViewState中值的值,如果它确实应用了类将文本颜色更改为红色,如果不进行绑定。

显然,这里有一些你需要思考但却是一个开始的结果。