我想更改中继器控件内的链接按钮的 CSS 类,在中继器 itemcommand 事件中,我正在使用此代码
if (e.CommandName == "read")
{
LinkButton btnread = (LinkButton)e.Item.FindControl("cmdread");
bool isread = true;
if (btnread.CssClass == "red-read")
{
isread = false;
}
if (isread)
{
btnread.CssClass = "";
btnread.CssClass = "red-read";
}
else
{
btnread.CssClass = "";
btnread.CssClass = "grey-read";
}
}
但它不起作用,我也试过这段代码
if (isread )
{
btnread.CssClass = btnread.CssClass.Replace("grey-read","red-read");
}
else
{
btnread.CssClass = btnread.CssClass.Replace("read-read","grey-read");
}
如何在文件后面的代码中更改 CSS 类这是我的转发器控件
<asp:Repeater ID="rptData" runat="server" DefaultItemsPerPage="15" ShowItemsPerPageOptions="true"
ShowPageControls="true"
PageControlsPlaceHolder="phFooterControls" NoResultsControl="lblNoResults" OnItemDataBound="rptData_ItemDataBound"
OnItemCommand="rptData_ItemCommand">
<HeaderTemplate>
<table width="100%" border="0" cellspacing="2" cellpadding="0" class="datatable">
<tr>
<th>Name</th>
<th>Location</th>
<th></th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<%#Eval("Firstname")%>
</td>
<td>
<%#Eval("Location")%>
</td>
<td>
<asp:LinkButton ID="cmdread" runat="server" CssClass="red-read" CommandName="read"
CommandArgument='<%#Eval(this.PKField) %>'></asp:LinkButton>
</td>
</tr>
</ItemTemplate>
<AlternatingItemTemplate>
<tr>
<td class="alt">
<%#Eval("Firstname")%>
</td>
<td class="alt">
<%#Eval("Location")%>
</td>
<td class="alt">
<asp:LinkButton ID="cmdread" runat="server" CssClass="red-read" CommandName="read"
CommandArgument='<%#Eval(this.PKField) %>'></asp:LinkButton>
</td>
</tr>
</AlternatingItemTemplate>
<FooterTemplate>
<tr>
<td colspan="9" class="tablefooter">
<div class="FloatLeft" style="width: 475px;">
<asp:PlaceHolder ID="phFooterControls" runat="server" />
</div>
<div class="FloatRight" style="width: 90px; text-align: right;">
<asp:PlaceHolder ID="phItemsPerPage" runat="server" />
</div>
<div class="FloatClear">
</div>
</td>
</tr>
</table>
</FooterTemplate>
</asp:Repeater>