在Repeater中更改链接按钮颜色

时间:2015-09-05 19:19:30

标签: c# asp.net datarepeater

我在Data Repeater内使用Link Buttonrepeater。我想更改点击button的颜色,但它不起作用。这是我的代码。

<asp:Repeater  ID="rptPager" runat="server">
                             <ItemTemplate>
                         <asp:LinkButton Font-Size="Larger" BackColor="Orange" ForeColor="White"  ID="lnkPage" runat="server" Text='<%#Eval("Text") %>' CommandArgument='<%# Eval("Value") %>'
                            CssClass=" btn"
                            OnClick="Page_Changed"></asp:LinkButton>
                             </ItemTemplate>
                             </asp:Repeater>


protected void Page_Changed(object sender, EventArgs e)
{

    int pageIndex = int.Parse((sender as LinkButton).CommandArgument);
    LinkButton lnk = (LinkButton)sender;
    lnk.ForeColor = System.Drawing.Color.Red;
    lnk.BackColor = System.Drawing.Color.Red;

    this.GetImagesPageWise(pageIndex);

}

2 个答案:

答案 0 :(得分:0)

这样做。为Current Page LinkBut​​ton创建类

.Active
{

    color:red;
     font:bold 12px Tahoma;

    }

然后按以下步骤更改Repeater

<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")) == true ? "LBR" : "Active" %>'></asp:LinkButton>
                          </ItemTemplate>
                      </asp:Repeater>

答案 1 :(得分:0)

<ul class="pagination">
    <asp:Repeater ID="rptPager" runat="server">
        <ItemTemplate>
            <li class="<%#GetStatus(Eval("Enabled").ToString(),Eval("Text").ToString())%>">
                <asp:LinkButton ID="lnkPage" runat="server" Text='<%#Eval("Text") %>' CommandArgument='<%# Eval("Value") %>'
                                    Enabled='<%# Eval("Enabled") %>' OnClick="Page_Changed">
                </asp:LinkButton>
            </li>
        </ItemTemplate>
    </asp:Repeater>
</ul>


Protected Function GetStatus(ByVal Enable As Boolean, ByVal linkName As String) As String
        Try
            If Enable = 0 And linkName <> "First" And linkName <> "Last" Then
                Return "active"
            Else
                Return ""
            End If
        Catch ex As Exception
            Return ""
        End Try
    End Function