0

我有一个GridView从我的SQL. 网格的页面大小为 7。我正在利用该RowDataBound事件将“<”和“>”字符替换为“`”,这可以按预期工作。

但是,问题在于它Paging在网格上禁用。RowDataBound当网格有超过 7 个项目时,应该启用分页,以便我可以转到第 2 页,但是一旦我使用该事件,分页就不起作用。

我的代码

protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["UserID"] != null)
        {
            GrdOpsBook.DataSource = OperationalEmployees.getEmpbookedBySpecificDate(DateTime.Today);
            GrdOpsBook.DataBind();

            if (GrdOpsBook.Rows.Count == 0)
                lblNoOpsBooking.Visible = true;

            lblWelcome.Text = "Welcome " + Session["UserLoggedInName"] + " to the Energy Insight Booking Application";
        }
        else
            Response.Redirect("LogIn.aspx");
    }

protected void GrdOpsBook_RowDataBound1(object sender, GridViewRowEventArgs e)
    {
        foreach (TableCell cell in e.Row.Cells)
            {
                cell.Text = cell.Text.Replace(">", "`");
                cell.Text = cell.Text.Replace("<", "`");
            }
    }

我的网格视图

    <asp:GridView ID="GrdOpsBook" runat="server" BackColor="White" BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" 
                    CellPadding="4" AllowPaging="True" EnableSortingAndPagingCallbacks="True" 
                    PageSize="7"
                    ForeColor="Black" GridLines="Vertical" Width="100%" AutoGenerateColumns="False" >
                    <AlternatingRowStyle BackColor="LightGray" />
                    <FooterStyle BackColor="#CCCC99" />
                    <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
                    <PagerStyle ForeColor="Black" HorizontalAlign="Right" BackColor="#F7F7DE" />
                    <RowStyle BackColor="#F7F7DE" />
                    <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
                    <SortedAscendingCellStyle BackColor="#FBFBF2" />
                    <SortedAscendingHeaderStyle BackColor="#848384" />
                    <SortedDescendingCellStyle BackColor="#EAEAD3" />
                    <SortedDescendingHeaderStyle BackColor="#575357" />

                    <Columns>
                        <asp:BoundField DataField="Operator" HeaderText="Operator" ItemStyle-Width="10%" ItemStyle-Wrap="false"  ItemStyle-HorizontalAlign="Center"/>
                        <asp:BoundField DataField="Destination" HeaderText="Destination" ItemStyle-Width="58%" HtmlEncode="false" ItemStyle-Wrap="true"/>
                        <asp:BoundField DataField="Start Time" HeaderText="Start Time" ItemStyle-Width="6%" HtmlEncode="false" ItemStyle-HorizontalAlign="Center" DataFormatString="{0:hh\:mm}" />
                        <asp:BoundField DataField="End Time" HeaderText="End Time" ItemStyle-Width="6%" HtmlEncode="false" ItemStyle-HorizontalAlign="Center" DataFormatString="{0:hh\:mm}" />
                        <asp:BoundField DataField="Booked By" HeaderText="Booked By" ItemStyle-Width="10%" ItemStyle-HorizontalAlign="Center"/>
                        <asp:BoundField DataField="Number of Days Booked" HeaderText="Number of Days Booked" ItemStyle-Width="10%" ItemStyle-HorizontalAlign="Center" />
                    </Columns>
                </asp:GridView>
4

2 回答 2

0

在这里你迭代每一行

protected void GrdOpsBook_RowDataBound1(object sender, GridViewRowEventArgs e) 
{
    foreach (TableCell cell in e.Row.Cells)
    { 
    cell.Text = cell.Text.Replace(">", "");
    cell.Text = cell.Text.Replace("<", ""); 
    } 
}

你应该限制你的迭代DataRows

if (e.Row.RowType == DataControlRowType.DataRow && e.Row.RowState != DataControlRowState.Edit)
于 2016-02-29T07:28:52.650 回答
0

谢谢,我是这样做的,并且在不禁用分页的情况下效果很好

 protected void GrdOpsBook_RowDataBound1(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            // Replace the "<" tag with "`".
            string sDestination = e.Row.Cells[1].Text.Replace("<", "`");
            e.Row.Cells[1].Text = sDestination;

        }
    }
于 2016-02-29T07:45:23.373 回答