我为转发器编写了自定义分页。以下是代码。当我点击一个LinkButton,如何设置选中它或如何设置css类?
首先,我尝试在lbl_Click函数中设置css类,如此lnk.CssClass = "classname";
每次尝试时,它都会设置css类所有单击的链接按钮。
private void CreatePagingControl()
{
for (int i = 0; i < pages; i++)
{
LinkButton lnk = new LinkButton();
lnk.Click += new EventHandler(lbl_Click);
lnk.ID = "lnkPage" + (i + 1).ToString();
lnk.Text = (i + 1).ToString();
plcPaging.Controls.Add(lnk);
Label spacer = new Label();
spacer.Text = " | ";
plcPaging.Controls.Add(spacer);
}
}
void lbl_Click(object sender, EventArgs e)
{
LinkButton lnk = sender as LinkButton;
int currentPage = int.Parse(lnk.Text);
int take = currentPage * 15;
int skip = currentPage == 1 ? 0 : take - 15;
FetchData(take, skip);
}
ASPX
<asp:PlaceHolder ID="plcPaging" runat="server" />
答案 0 :(得分:0)
试试这个
将您的aspx更改为此
<div id="pageingDiv">
<asp:PlaceHolder ID="plcPaging" runat="server" />
<asp:HiddenField id="currentId" runatserver" value="1" />
</div>
将此Jquery脚本调用$(document).ready()
$("#pageingDiv a").removeClass();
var hvId = $('[id$=currentId]').val();
$("#lnkPage" _ hvId).AddClass('ClassName');
然后在你的代码隐藏中添加css
void lbl_Click(object sender, EventArgs e)
{
LinkButton lnk = sender as LinkButton;
int currentPage = int.Parse(lnk.Text);
int take = currentPage * 15;
int skip = currentPage == 1 ? 0 : take - 15;
FetchData(take, skip);
//add css here
currentId.Value=currentPage.ToString();
}
答案 1 :(得分:0)
也许你可以尝试这样的东西,当CssClass包含多个类时,这也应该有效。
// removing a cssClass
protected String removeCssClass(string cssClass, string toRemove)
{
return String.Join(" ", cssClass.Split(' ').Except(new[] {toRemove}).ToArray());
}
void lbl_Click(object sender, EventArgs e)
{
// resetting all selected items
plcPaging.Controls
.OfType<Label>()
.Where(l => l.ID.StartsWith("lnkPage") && l.CssClass.Split(' ').Contains("selectedClassName")).ToList()
.ForEach(l => l.CssClass = removeCssClass(l.CssClass, "classname"));
LinkButton lnk = sender as LinkButton;
// setting selected item
lnk.CssClass = "classname";
int currentPage = int.Parse(lnk.Text);
int take = currentPage * 15;
int skip = currentPage == 1 ? 0 : take - 15;
FetchData(take, skip);
}
希望这会有所帮助