如何在属性中使用AllowPaging?

时间:2014-02-28 16:36:23

标签: asp.net gridview

我有一个gridview,我在gridview中给了allowpaging true ..现在我在gridview的底部看到了pagenumber ..现在,当我点击页面编号时,点击不起作用..它显示了一些错误,如“ GridView的'GridView1'触发了未处理的事件PageIndexChanging。“..如何逃脱它?

我的代码

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" BackColor="LightGoldenrodYellow" BorderColor="Tan" BorderWidth="1px" CellPadding="2" ForeColor="Black" GridLines="None" PageSize="1" Width="544px" >
        <AlternatingRowStyle BackColor="PaleGoldenrod" />
        <Columns>
            <asp:TemplateField HeaderText="cdd">
                <ItemTemplate>
                    <asp:CheckBox ID="CheckBox2" runat="server" />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
        <FooterStyle BackColor="Tan" />
        <HeaderStyle BackColor="Tan" Font-Bold="True" />
        <PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue" HorizontalAlign="Center" />
        <SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
        <SortedAscendingCellStyle BackColor="#FAFAE7" />
        <SortedAscendingHeaderStyle BackColor="#DAC09E" />
        <SortedDescendingCellStyle BackColor="#E1DB9C" />
        <SortedDescendingHeaderStyle BackColor="#C2A47B" />
    </asp:GridView>

我的代码隐藏

public partial class usersshow : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

    ArrayList myArrayList = ConvertDataSetToArrayList();

        // Display each item of ArrayList
        DataTable dt = new DataTable();
        dt.Columns.Add("User Id");
        dt.Columns.Add("Problem Name");
        dt.Columns.Add("Status");

       foreach (Object row in myArrayList)
        {
            dt.Rows.Add();
            dt.Rows[dt.Rows.Count - 1]["User Id"] = ((DataRow)row)["userid"].ToString();
            dt.Rows[dt.Rows.Count - 1]["Problem Name"] = ((DataRow)row)["problemname"].ToString();
            dt.Rows[dt.Rows.Count - 1]["Status"] = ((DataRow)row)["status"].ToString();

        }

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

 }

 public ArrayList ConvertDataSetToArrayList()
 {

    string con = " ";
    con = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
    SqlConnection objsqlconn = new SqlConnection(con);
    objsqlconn.Open();
    SqlCommand cmd = new SqlCommand("SELECT userid,problemname,status FROM problemtable", objsqlconn);
    cmd.ExecuteNonQuery();
    cmd.CommandType = CommandType.Text;
    SqlDataAdapter myAdapter = new SqlDataAdapter();
    myAdapter.SelectCommand = cmd;

    DataSet myDataSet = new DataSet();
    myAdapter.Fill(myDataSet);

    ArrayList myArrayList = new ArrayList();
    foreach (DataRow dtRow in myDataSet.Tables[0].Rows)
    {
        myArrayList.Add(dtRow);
    }
    objsqlconn.Close();
    return myArrayList;
 }

1 个答案:

答案 0 :(得分:0)

在页面中添加onpageindexchanging事件,并检查您是否遗漏了该内容。

所以aspx代码是

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" BackColor="LightGoldenrodYellow" 
 BorderColor="Tan" BorderWidth="1px" CellPadding="2" ForeColor="Black" GridLines="None" PageSize="1" 
 Width="544px" AllowPaging="true" onpageindexchanging = "GridView1_PageIndexChanging"  >

代码方

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

 }
 public System.Data.DataTable GetDataTable()
 {
    ArrayList myArrayList = ConvertDataSetToArrayList();

    // Display each item of ArrayList
    DataTable dt = new DataTable();
    dt.Columns.Add("User Id");
    dt.Columns.Add("Problem Name");
    dt.Columns.Add("Status");

   foreach (Object row in myArrayList)
    {
        dt.Rows.Add();
        dt.Rows[dt.Rows.Count - 1]["User Id"] = ((DataRow)row)["userid"].ToString();
        dt.Rows[dt.Rows.Count - 1]["Problem Name"] = ((DataRow)row)["problemname"].ToString();
        dt.Rows[dt.Rows.Count - 1]["Status"] = ((DataRow)row)["status"].ToString();

    }

    Return dt;
    }


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