asp.net Gridview RowDataBound禁用网格分页

时间:2016-02-29 07:07:31

标签: c# asp.net gridview rowdatabound

我从GridView填充了SQL。网格的页面大小为7.我正在使用RowDataBound事件来替换“<”和“>”带有“`”的字符按预期工作。

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

我的代码

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("<", "`");
            }
    }

我的GridView

    <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>

2 个答案:

答案 0 :(得分: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)

答案 1 :(得分: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;

        }
    }