2013-02-14 38 views
0

我在我的頁面有一個gridview它允許分頁,但是當我點擊下一個數字時,它給了我這個錯誤:System.Web.HttpException:GridView'GridView1'觸發事件PageIndexChanging,它沒有處理。 我dontknow我怎麼能解決這個問題,我必須解決這個問題,直到明天我真的需要幫助爲什麼當我點擊gridview中的下一頁它給我錯誤?

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
       CellPadding="4" DataKeyNames="idFromUsers" AutoGenerateColumns="False" 
    ForeColor="#333333" GridLines="None" onrowcommand="GridView1_RowCommand" 
       ><AlternatingRowStyle BackColor="White" ForeColor="#284775" /> 
    <Columns> 
    <asp:BoundField HeaderText="order code" DataField="idfromIndex" /> 
    <asp:BoundField HeaderText="customer code" DataField="idFromUsers" /> 
    <asp:BoundField HeaderText="factor number" DataField="indexNO"/> 
    <asp:BoundField HeaderText="bank name" DataField="bankName" /> 
    <asp:BoundField HeaderText="order date" DataField="orderDate" DataFormatString="{0:yyyy/MM/dd}"/> 
     <asp:ButtonField CommandName="cart" Text="shopping cart detailsد" > 
     <ItemStyle HorizontalAlign="Center" /> 
     </asp:ButtonField> 
     <asp:ButtonField CommandName="customer" Text="customer details"> 
     <ItemStyle HorizontalAlign="Center" /> 
     </asp:ButtonField> 
     </Columns> 
    <EditRowStyle BackColor="#999999" /> 
    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> 
    <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> 
    <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" /> 
    <RowStyle BackColor="#F7F6F3" ForeColor="#333333" /> 
    <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" /> 
    <SortedAscendingCellStyle BackColor="#E9E7E2" /> 
    <SortedAscendingHeaderStyle BackColor="#506C8C" /> 
    <SortedDescendingCellStyle BackColor="#FFFDF8" /> 
    <SortedDescendingHeaderStyle BackColor="#6F8DAE" /> 
</asp:GridView> 

我的C#代碼:

protected void Page_Load(object sender, EventArgs e) 
{ 
    if (!IsPostBack) 
    { 

     initialGrid1(); 
    } 

} 

private void initialGrid1() 
{ 
    getDistinctOrderId(); 
    DataTable myDT = getTheData(); 

    GridView1.DataSource = myDT; 
    GridView1.DataBind(); 

} 

private void getDistinctOrderId() 
{ 
    string command = ""; 

    if (ViewState["searchFactor"] != null) 
    { 
     int index = Convert.ToInt32(ViewState["searchFactor"]); 

     switch (index) 
     { 

      case 1: 
       { 
        string fact = factorNO.Text.Trim(); 
        command = "SELECT min(id), [idfromIndex] FROM OrdersView WHERE [idfromIndex] IN(SELECT DISTINCT [idfromIndex] FROM OrdersView where IsObserve='True' and Ispay='False' and indexNO ='" + fact + "') GROUP BY [idfromIndex] "; 

       } 
       break; 
      case 2: 
       { 
        string name = TextBox3.Text.Trim(); 
        string familly = TextBox4.Text.Trim(); 
        command = "SELECT min(id), [idfromIndex] FROM OrdersView WHERE [idfromIndex] IN(SELECT DISTINCT [idfromIndex] FROM OrdersView where IsObserve='True' and Ispay='False' and user_name ='" + name + "' and user_familly ='" + familly + "') GROUP BY [idfromIndex] "; 
       } 
       break; 
      case 3: 
       { 
        command = getSearchDate(); 
        //string startdate = TextBox1.Text.Trim(); 
        //string enddate = TextBox2.Text.Trim(); 
        //command = "SELECT min(id), [idfromIndex] FROM OrdersView WHERE [idfromIndex] IN(SELECT DISTINCT [idfromIndex] FROM OrdersView where IsObserve='True' and Ispay='False' and orderDate >='" + startdate + "' and orderDate <='" + enddate + "') GROUP BY [idfromIndex] "; 
       } 
       break; 
      case 0: 
       { 
        command = "SELECT min(id), [idfromIndex] FROM OrdersView WHERE [idfromIndex] IN(SELECT DISTINCT [idfromIndex] FROM OrdersView where IsObserve='True' and Ispay='False') GROUP BY [idfromIndex] "; 

       } 
       break; 

      default: 
       { 
        command = "SELECT min(id), [idfromIndex] FROM OrdersView WHERE [idfromIndex] IN(SELECT DISTINCT [idfromIndex] FROM OrdersView where IsObserve='True' and Ispay='False') GROUP BY [idfromIndex] "; 

       } 
       break; 
     } 
    } 
    else 
    { 
     command = "SELECT min(id), [idfromIndex] FROM OrdersView WHERE [idfromIndex] IN(SELECT DISTINCT [idfromIndex] FROM OrdersView where IsObserve='True' and Ispay='False') GROUP BY [idfromIndex] "; 

    } 

    SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["mobile_storeConnectionString"].ConnectionString); 
    SqlCommand cmd = new SqlCommand(); 
    cmd.Connection = myConnection; 
    cmd.CommandType = CommandType.Text; 
    cmd.CommandText = command; 
    DataView MyDv = new DataView(); 

    try 
    { 
     myConnection.Open(); 
     SqlDataReader dr = cmd.ExecuteReader(); 
     DataTable dt = new DataTable(); 
     dt.Load(dr); 
     MyDv = dt.DefaultView; 
     for (int i = 0; i < MyDv.Count; i++) 
     { 
      if (i == 0) 
      { 
       distinctList = MyDv[i][0].ToString() + ","; 
      } 
      if (i == MyDv.Count - 1) 
      { 
       distinctList += MyDv[i][0].ToString(); 
      } 
      else if (i != 0 && i != MyDv.Count - 1) 
      { 
       distinctList += MyDv[i][0].ToString() + ","; 
      } 
     } 
     dr.Close(); 

     //Label1.Text = MyDv.Count.ToString(); 

    } 
    catch (Exception EXP) 
    { 


    } 
    finally 
    { myConnection.Close(); } 
} 


public DataTable getTheData() 
{ 

    DataSet DS = new DataSet(); 
    SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString1"].ConnectionString); 
    SqlCommand cmd = new SqlCommand(); 
    cmd.Connection = myConnection; 
    cmd.CommandType = CommandType.Text; 
    cmd.CommandText = "select idfromIndex,indexNO,bankName,Ispay,orderDate,idFromUsers,orderId from factors where orderId IN (" + distinctList + ") and IsObserve='True'"; 
    // SqlDataAdapter objSQLAdapter = new SqlDataAdapter("select idfromIndex,indexNO,bankName,Ispay,orderDate,idFromUsers,orderId from factors where orderId IN (" + distinctList + ") and IsObserve='False'", myConnection); objSQLAdapter.Fill(DS, "mobile_store"); 
    //ViewState["idfromindex"] = DS.Tables[0].Rows[0].ce 
    // DataTable dt = new DataTable(); 
    SqlDataAdapter da = new SqlDataAdapter(cmd); 
    da.Fill(DS); 
    try 
    { 
     myConnection.Open(); 
     cmd.ExecuteNonQuery(); 


    } 
    catch (Exception EXP) 
    { 


    } 
    finally 
    { myConnection.Close(); } 
    return DS.Tables[0]; 
} 

回答

0

因爲你沒有OnPageIndexChanging =「gridView_PageIndexChanging」事件。

protected void gridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) 
{ 
    gridView.PageIndex = e.NewPageIndex; 
    gridView.DataBind(); 
} 
1

首先,你需要處理的GridView PageIndexChanging事件,然後設置新的PageIndex到的GridView

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" CellPadding="4" 
    DataKeyNames="idFromUsers" AutoGenerateColumns="False" ForeColor="#333333" 
    GridLines="None" onrowcommand="GridView1_RowCommand" 
    OnPageIndexChanging="GridView1_PageIndexChanging"> 

在後面的代碼添加此事件

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) 
{ 
    GridView1.PageIndex = e.NewPageIndex; 
    GridView1.DataBind(); 
} 
相關問題