在自定义页面中查找所选项目
本文关键字:选项 项目 查找 自定义 | 更新日期: 2023-09-27 17:58:36
我为中继器编写了自定义分页。下面是代码。当我点击一个链接按钮时,如何设置选中的或者如何设置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" />
试试这个
将您的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();
}
也许你可以尝试这样的方法,当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);
}
希望这将有助于