【问题标题】:asp.net Gridview RowDataBound disables grid pagingasp.net Gridview RowDataBound 禁用网格分页
【发布时间】:2016-02-29 07:07:31
【问题描述】:

我有一个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("<", "`");
            }
    }

我的网格视图

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

【问题讨论】:

    标签: c# asp.net gridview rowdatabound


    【解决方案1】:

    在这里你遍历每一行

    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)
    

    【讨论】:

      【解决方案2】:

      谢谢,我是这样做的,而且不禁用分页效果很好

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

      【讨论】:

        猜你喜欢
        • 2014-04-06
        • 1970-01-01
        • 2010-12-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多