2013-02-26 66 views
0

我剛剛看了一下paging gridview一些文章,但我不能讓它...分頁的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(); 
    } 
+0

你有沒有使用ASP.NET MVC? – 2013-02-26 19:14:04

+0

不...我是網絡編程的「noob」;對不起 – Ghaleon 2013-02-26 19:14:58

+0

我只是問,因爲在學習MVC之前,我也在Webforms中付出了很多努力。所有這些複雜的Gridview和Listview都是浪費時間。我建議至少用Razor視圖引擎來看看MVC。 – 2013-02-26 19:18:28

回答

1

更新2

現在我希望有基金會d您的尋呼機未被顯示的原因。您正在第一科拉姆無形的,即使在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爲真?

<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被啓用(默認)時,您還應該綁定GridView數據綁定if(!IsPostBack)

所以從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"); 
    } 
} 
+0

當然,我做到了,仍然沒有任何反應,沒有數字可以翻到下一個... – Ghaleon 2013-02-26 19:15:25

+0

@Ghaleon:但事件是否被觸發?你有沒有設置斷點?當你數據綁定'GridView'?你應該只做'if(!IsPostBack)'。 – 2013-02-26 19:18:02

+0

不,它沒有被觸發;我用我的gridview代碼更新了我的問題。 – Ghaleon 2013-02-26 19:19:09

0

您必須在.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

UPDATE

確保多數民衆贊成被綁定到GridView的數據是多於5自PageSize="5"。如果行數小於5,則不會顯示尋呼機。

+0

我剛剛添加了我的gridview代碼 – Ghaleon 2013-02-26 19:18:05

+0

更新了我的答案,有多少行由'ch.BuscaTodosChamados();'返回? – 2013-02-26 19:29:28

+0

它正在返回所有這些...但顯示由PageSize = NumberOfRowsToShow設置的數量。對 ?但沒有任何分頁...;我用我的完整CodeBehind更新了我的問題 – Ghaleon 2013-02-26 19:30:51