1

我将自定义分页写入转发器。下面是代码。当我单击链接按钮时,如何设置选中它或如何设置 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 = "&nbsp;|&nbsp;";
            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" />
4

2 回答 2

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);
}

希望这会有所帮助

于 2013-02-11T15:49:03.793 回答
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();
}
于 2013-02-11T15:26:58.830 回答