【问题标题】:Paging GridView Issues分页 GridView 问题
【发布时间】:2013-02-12 09:11:06
【问题描述】:

我刚读了一些关于paging gridview 的文章,但我没能成功...

这是我完整的gridview的代码:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" style="font-family: Verdana, Arial, Sans-Serif;" 

            CssClass="gridview" OnSorting="GridView_Sorting"
            DataKeyNames="id"
            AllowSorting ="True" BackColor="#CCCCCC"
            BorderStyle="Inset" BorderWidth="2px" BorderColor="GrayText"
            CellPadding="1"
            CellSpacing="5"
            HeaderStyle-HorizontalAlign="Center"
            OnRowDataBound="GridView1_RowDataBound"
            ForeColor = "Black" RowStyle-CssClass="gridview" 
            OnRowCommand="GridView1_RowCommand" AllowPaging="True" 
            OnPageIndexChanging="GridView1_PageIndexChanging" PageSize="5">            
            <AlternatingRowStyle BackColor="#CCCCCC" />
                <columns>

                    <asp:BoundField HeaderText="ID" DataField="id"  />
                    <asp:BoundField HeaderText="PRIORIDADE" DataField="prioridade" 
                        ItemStyle-HorizontalAlign="Center">
                    <ItemStyle HorizontalAlign="Center" />
                    </asp:BoundField>
                    <asp:BoundField  HeaderText="SITUAÇÃO"  DataField="situacao" ItemStyle-HorizontalAlign="Center" >
                    <ItemStyle HorizontalAlign="Center" />
                    </asp:BoundField>
                    <asp:BoundField HeaderText="RESPONSAVEL" DataField="responsavel" HeaderStyle-Width="65px" ItemStyle-HorizontalAlign="Center">
                    <HeaderStyle Width="65px" />
                    <ItemStyle HorizontalAlign="Center" />
                    </asp:BoundField>
                    <asp:BoundField HeaderText="DATA DE CADASTRO" DataField="dt_cadastro" DataFormatString="{0:dd/MM/yyyy}" HeaderStyle-Width="60px" SortExpression="dt_cadastro"
                    ItemStyle-HorizontalAlign="Center" >
                    <HeaderStyle Width="60px" />
                    <ItemStyle HorizontalAlign="Center" />
                    </asp:BoundField>
                    <asp:BoundField HeaderText="PREVISÃO DE TÉRMINO" DataField="previsao_termino" DataFormatString="{0:dd/MM/yyyy}" HeaderStyle-Width="60px"
                    ItemStyle-HorizontalAlign="Center">
                    <HeaderStyle Width="60px" />
                    <ItemStyle HorizontalAlign="Center" />
                    </asp:BoundField>
                    <asp:BoundField HeaderText="PROJETO" DataField="projeto"  ItemStyle-HorizontalAlign="Center">
                    <ItemStyle HorizontalAlign="Center" />
                    </asp:BoundField>
                    <asp:BoundField HeaderText="FUNCIONALIDADE" DataField="funcionalidade" 
                        ItemStyle-HorizontalAlign="Center" >
                    <ItemStyle HorizontalAlign="Center" />
                    </asp:BoundField>
                    <asp:BoundField HeaderText="CLUBE" DataField="clube"  
                        ItemStyle-HorizontalAlign="Center" >
                    <ItemStyle HorizontalAlign="Center" />
                    </asp:BoundField>

                    <asp:TemplateField HeaderStyle-Width="70px" HeaderText="VISUALIZAR" >
                        <ItemTemplate>
                            <asp:Button ID="Btn_Visualizar" runat="server" Text="VISUALIZAR" CssClass="Btn_Grid"  Font-Size="7pt" Font-Names="Verdana, Arial"  OnClick="Btn_Visualizar_Click"
                            CommandName="visualizar" CommandArgument="<%# ((GridViewRow)Container).RowIndex %>" />                            
                        </ItemTemplate>
                        <HeaderStyle Width="70px" />
                    </asp:TemplateField>
                    <asp:TemplateField HeaderStyle-Width="66px" HeaderText="ALTERAR">
                        <ItemTemplate>
                            <asp:Button ID="Btn_Alterar" runat="server" Text="ALTERAR" CssClass="Btn_Grid" Font-Size="7pt" Font-Names="Verdana, Arial"
                            CommandName="editar" CommandArgument="<%# ((GridViewRow)Container).RowIndex %>" />
                        </ItemTemplate>
                        <HeaderStyle Width="66px" />
                    </asp:TemplateField>
                    <asp:TemplateField HeaderStyle-Width="66px" HeaderText="FEEDBACK">
                        <ItemTemplate>
                            <asp:Button ID="Btn_Feedback" runat="server" Text="ADICIONAR" CssClass="Btn_Grid" Font-Size="7pt" Font-Names="Verdana,Arial"
                            CommandName="feedback" CommandArgument="<%# ((GridViewRow)Container).RowIndex %>" />
                        </ItemTemplate>
                        <HeaderStyle Width="66px" />
                    </asp:TemplateField>

                </columns>
            <EditRowStyle ForeColor="Black" CssClass="GridViewEditRow" />
            <FooterStyle BackColor="#CCCCCC" />
            <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" BorderColor="White" BorderStyle="Solid" BorderWidth="1px" />
            <PagerSettings Mode="NumericFirstLast" FirstPageText="First" LastPageText="Last" PageButtonCount="5" />
            <PagerStyle BackColor="#CCCCCC" ForeColor="Black" HorizontalAlign="Left" />
            <RowStyle BackColor="White" />
            <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
            </asp:GridView>    

代码隐藏:

public partial class TodosChamados : System.Web.UI.Page
  {
    BDUsuarios usr = new BDUsuarios();
    BDFuncionalidades func = new BDFuncionalidades();
    BDChamados ch = new BDChamados();

protected void Page_Load(object sender, EventArgs e)
  {
    if (!IsPostBack)
       {
          GridView1.DataSource = ch.BuscaTodosChamados();
          GridView1.DataBind();
        }
  }

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.Cells[1].Text == "ALTA")
            {
                e.Row.Cells[1].BackColor = System.Drawing.Color.Red;
                e.Row.ControlStyle.Font.Bold = true;
            }
        }
        if (e.Row.Cells[0].Visible = e.Row.RowType == DataControlRowType.Pager)
        {
            e.Row.Cells[0].Visible = false;
        }
    }

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
 {
    if (e.CommandName != "Sort")
       {
          if (e.CommandName == "visualizar")
                {
                 Session["id"] = GridView1.Rows[Convert.ToInt32(e.CommandArgument.ToString())].Cells[0].Text;
                    Session["editar"] = null;
                }
                else if (e.CommandName == "editar")
                {
                    Session["id"] = GridView1.Rows[Convert.ToInt32(e.CommandArgument.ToString())].Cells[0].Text; //Grava o ID do chamado da linha correspondente.
                    Session["editar"] = 1; // 1 - Editar   2 - Não Editar .
                }

                else if (e.CommandName == "feedback")
                {
                    Session["id"] = GridView1.Rows[Convert.ToInt32(e.CommandArgument.ToString())].Cells[0].Text; //Grava o ID do chamado da linha correspondente.                
                    Response.Redirect("~/Adm/Feedback.aspx");
                }
                Response.Redirect("~/Adm/DetalhesChamado.aspx");
            }
        }

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
  {
      GridView1.PageIndex = e.NewPageIndex;
      GridView1.DataSource = ch.BuscaTodosChamados();
      GridView1.DataBind();
  }

【问题讨论】:

  • 你用过 ASP.NET MVC 吗?
  • 不...我是网络编程的“菜鸟”;对不起
  • 我只是在问,因为在我学习 MVC 之前,我也在 Webforms 上投入了很多精力。所有这些复杂的 Gridview 和 Listview 都是浪费时间。我建议至少看看带有 Razor 视图引擎的 MVC。
  • @Nick 是的,我工作的朋友也这么告诉我...我想我会开始学习 MVC :s 但现在我需要完成这个。快完成了 ;s 其实我很喜欢用 winforms 工作,但现在已经不怎么用了... ;\我必须学习网络 ;P
  • 我也喜欢 winforms 的开发风格,但这种风格在 web 上不太适用。 Webforms 调试起来很麻烦,而且您对输出没有足够的控制权。我非常讨厌视图状态和页面生命周期事件祝你好运。

标签: asp.net gridview pagination


【解决方案1】:

更新 2

现在我希望找到您的寻呼机未显示的原因。您正在使第一列不可见,即使对于 RowDataBound 中的寻呼机行也是如此,但寻呼机默认位于第一个单元格中:

e.Row.Cells[0].Visible = false;

你必须检查正确的RowType

e.Row.Cells[0].Visible = e.Row.RowType == DataControlRowType.Pager;

在 aspx 标记上甚至更好:

<asp:BoundField HeaderText="ID" DataField="id" Visible="False" />

一个愚蠢的问题,您是否将AllowPaging 设置为true?

<asp:GridView Id="GridView1" runat="server" AllowPaging="True" >
    <PagerSettings Mode="NumericFirstLast" FirstPageText="First" LastPageText="Last" PageButtonCount="5" />
</asp:gridview>

GridView.AllowPaging Property

true 如果启用了分页功能;否则,false。默认为 false

您还应该在启用 viwstate(默认)时仅对 if(!IsPostBack) 数据绑定 GridView

因此,将Page_Load(我猜)中的数据绑定代码包装到此检查中:

protected void Page_Load(Object sender, EventArgs e)
{
    if(!IsPostBack)
    {
         GridView1.DataSource = ch.BuscaTodosChamados();
         GridView1.DataBind();
    }
}

否则将不会触发事件,并且在您在回发时重新加载数据时会覆盖更改。

更新根据您上次的编辑:

我认为您的RowCommand 是导致此问题的原因。它在PageIndexChanging 事件之前调用,并且在页面更改时也会触发(如果我没记错的话)。所以看看你的Response.Redirect,当没有if/else-if 捕捉到它时会发生这种情况。

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName != "Sort")
    {
        if (e.CommandName == "visualizar")
        {
            // ..
        }
        else if (e.CommandName == "editar")
        {
           //..
        }

        else if (e.CommandName == "feedback")
        {
           //..
        }
        Response.Redirect("~/Adm/DetalhesChamado.aspx");
    }
}

【讨论】:

  • 当然我做了,仍然没有任何反应,没有数字可以翻到下一个...
  • @Ghaleon:但是事件被触发了?你有在那里设置断点吗?当您对GridView 进行数据绑定时?你应该只这样做if(!IsPostBack)
  • 不,它没有被触发;我用 gridview 的代码更新了我的问题。
  • @Ghaleon:编辑了我的答案。
  • 我正在这样做,并且查看状态是默认的(继承)。现在我设置了ViewStateMode: Enabled,但还是一样。没有分页。我不知道这是否重要,但是,在Designe View 我可以看到分页的数字,但是当我调试它时,什么都没有......
【解决方案2】:

您必须在 .aspx 中为 OnPageIndexChanging 指定事件。应该是:

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
        OnPageIndexChanging = "GridView1_PageIndexChanging" 
        AutoGenerateColumns="False" DataKeyNames="LastName,FirstName"
        EnablePersistedSelection="True" SelectedRowStyle-BackColor="Yellow" 
        DataSourceID="SqlDataSource1" AllowSorting="True">

你可能有同样的problem here

更新

确保自PageSize="5" 以来绑定到GridView 的数据大于5。如果行数小于 5,则不显示分页器。

【讨论】:

  • 更新了我的答案,ch.BuscaTodosChamados();返回了多少行?
  • 它正在返回所有这些......但显示了我通过 PageSize = NumberOfRowsToShow 设置的数量。正确的 ?但是没有任何分页...;我用完整的 CodeBehind 更新了我的问题
  • “全部”有多少条记录?同时,您可能还想尝试删除任何 CSS 样式,以确保没有任何内容被隐藏。
  • 我的数据库表上有 15 条记录/行。我知道它不多,我的意思是它检索表上的每一条记录,就像它应该是的那样。我会尝试删除我的 gridview 的所有 CSS。
  • 还是一样.. 即使没有 CSS ;s
猜你喜欢
  • 2011-10-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-09
相关资源
最近更新 更多