如何突出分页中的当前数字 - GridView PagerTemplate?

时间:2018-04-26 02:33:25

标签: c# asp.net

这是我在GridView中的当前设计,悬停时单元格变为橙色,这是通过使用css实现的。但我的问题是,如果它是活动的那个,我怎么能改变单元格背景颜色?

图片:Current Design

HTML代码:(仅限GridView)

 <asp:GridView ID="GridView1" runat="server" PageSize="20" OnPageIndexChanging="GridView1_PageIndexChanging"
    AllowPaging="True" OnDataBound="GridView1_DataBound"
    Style="position: static">
    <PagerSettings Position="Top" Mode="Numeric" />
    <PagerStyle ForeColor="Black" HorizontalAlign="Center" Font-Underline="False" CssClass="pager" />
    <PagerTemplate>
        <table id="tablePager">
            <tr style="background-color: #F8F8F8;">
                <td>
                    <asp:LinkButton ID="First" CommandName="Page" CommandArgument="First" runat="server"
                        Text="&lt;&lt; First" Style="color: black" ForeColor="Black" Font-Underline="False"></asp:LinkButton>
                </td>
                <td>
                    <asp:LinkButton ID="Previous" CommandName="Page" CommandArgument="Prev" runat="server"
                        Text="&lt; Previous" Style="color: black" ForeColor="Black" Font-Underline="False"></asp:LinkButton>
                </td>
                <asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater_ItemCommand" OnItemDataBound="Repeater_ItemDataBound" OnLoad="Repeater_Load">
                    <ItemTemplate>
                        <td id="cell">
                            <asp:LinkButton ID="lnkPageNumber" CommandName="Page" runat="server" ForeColor="Black" Font-Underline="False" />

                        </td>
                    </ItemTemplate>
                </asp:Repeater>
                <td>
                    <asp:LinkButton ID="Next" CommandName="Page" CommandArgument="Next" runat="server"
                        Text="Next &gt;" ForeColor="Black" Font-Underline="False"></asp:LinkButton>
                </td>
                <td>
                    <asp:LinkButton ID="Last" CommandName="Page" CommandArgument="Last" runat="server"
                        Text="Last &gt;&gt;" Style="color: black" ForeColor="Black" Font-Underline="False"></asp:LinkButton>
                </td>
            </tr>
        </table>
    </PagerTemplate>
</asp:GridView>

代码背后:

页面加载

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        PopulateGridView();
    }
}

PopulateGridView

public void PopulateGridView()
{
    String constring = System.Configuration.ConfigurationManager.ConnectionStrings["sqlconnect"].ToString();
    MySqlConnection con = new MySqlConnection(constring);
    using (MySqlCommand cmd = new MySqlCommand("SELECT * FROM tabletester"))
    {
        cmd.CommandType = CommandType.Text;
        cmd.Connection = con;
        con.Open();
        MySqlDataReader reader = cmd.ExecuteReader();
        DataTable dt = new DataTable();
        dt.Load(reader);

        con.Close();

        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
}

Repeater_ItemCommand

protected void Repeater_ItemCommand(object source, RepeaterCommandEventArgs e)
{
    if (e.CommandName == "Page")
    {
        GridView1.PageIndex = Convert.ToInt32(e.CommandArgument);
        GridView1.DataBind();
        PopulateGridView();
    }
}

Repeater_ItemDataBound

protected void Repeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    LinkButton lnkPageNumber = new LinkButton();
    System.Int32 pageNumber = (System.Int32)e.Item.DataItem;

    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        lnkPageNumber = (LinkButton)e.Item.FindControl("lnkPageNumber");
        lnkPageNumber.Text = pageNumber.ToString();
        lnkPageNumber.CommandArgument = (pageNumber - 1).ToString();
    }
}

Repeater_Load

protected void Repeater_Load(object sender, EventArgs e)
{
    Repeater Repeater = (Repeater)sender;
    Repeater.DataSource = Enumerable.Range(1, GridView1.PageCount);
    Repeater.DataBind();
}

1 个答案:

答案 0 :(得分:1)

您可以在Repeater_ItemDataBound

中执行此类操作
if (e.Item.ItemIndex == GridView1.PageIndex)
{
    lnkPageNumber.ForeColor = Color.Red;
}

那就是说,你现在有一个GridView有一个内置的分页系统吗?你正在做的过于复杂的事情要容易得多。以下是所需的所有代码:

ASPX

<asp:GridView ID="GridView1" runat="server" AllowPaging="true" PageSize="5" 
   OnPageIndexChanging="GridView1_PageIndexChanging"></asp:GridView>

CODE

protected void Page_Load(object sender, EventArgs e)
{
    if (IsPostBack == false)
    {
        BindDataToGridView();
    }
}

private void BindDataToGridView()
{
    GridView1.DataSource = source;
    GridView1.DataBind();
}

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    GridView1.PageIndex = e.NewPageIndex;
    BindDataToGridView();
}

这是IT。功能寻呼系统所需的所有代码。