在gridview中更改页码时,未调用PageIndexChanging函数

时间:2014-10-02 02:45:36

标签: c# asp.net gridview

亲爱的Stack Overflowers,

我在前端页面有一个gridview,这里是asp.net代码:

 <asp:GridView ID="grdManufact" runat="server" AutoGenerateColumns="False" 
    BackColor="White" BorderColor="#336666" BorderStyle="Double" BorderWidth="3px" CellPadding="4" 
    GridLines="Horizontal" AllowPaging="True" OnRowDataBound="manufGridView_RowDataBound"                             EnableModelValidation="False" EnableSortingAndPagingCallbacks="True" HorizontalAlign="Center"   OnSelectedIndexChanged="grdManufact_SelectedIndexChanged" OnPageIndexChanging="grdManufact_PageIndexChanging">
  <Columns>
      <asp:BoundField DataField="SrNo" HeaderText="SrNo" />
     <asp:BoundField DataField="Manufacturer" HeaderText="Manufacturer" />
     <asp:BoundField DataField="Description" HeaderText="Description" />
     <asp:BoundField DataField="isModerated" HeaderText="Approved" />
     <asp:BoundField />

这是它的主要部分,但它正确显示并在页面加载时正确绑定。 每当我将页面更改为第2页或第3页或任何gridview时,我的gridview都会消失!我已经尝试在PageIndexChanging函数中设置断点但是没有到达断点,这告诉我事件甚至没有触发,但是gridview只是消失了。这是我的后端函数页面索引更改:

protected void grdManufact_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    grdManufact.PageIndex = e.NewPageIndex;
    BindGrid();
}

用于绑定网格的BindGrid()函数:

public void BindGrid()
{
    string strConnectionString =   ConfigurationManager.ConnectionStrings["ConnectionString2"].ToString();
    SqlConnection conn = new SqlConnection(strConnectionString);    // Connect to database
    conn.Open();                                                    // Open Connection
    string com = "select ManufacturerID as SrNo, ManufacturerName as Manufacturer,        ManufacturerDescription as Description,isModerated From VehicleManufacturer";
    SqlDataAdapter adpt = new SqlDataAdapter(com, conn); // Select all manufacturers in the table
    DataTable dt = new DataTable();                                 // Create a new Data Table
    adpt.Fill(dt);                                                  // Fill it with manufacturers
    grdManufact.DataSource = dt;    // Make the datasource of the manufacturer grid the new table
    grdManufact.DataBind();                                         // Bind data for the grid
    conn.Close();                                     // Close database connection. Disconnect
}

这是我的页面加载,以防您也想要:

protected void Page_Load(object sender,EventArgs e) {     if(!IsPostBack)//如果这是第一次通过回发加载页面         BindGrid(); //将制造商绑定到gridview     其他     {         ClientScript.GetPostBackEventReference(this,string.Empty);         if(Request.Form [“__ EVENTTARGET”] ==“Button2_Click”)         {             //调用方法             btnDelete_Click(this,new EventArgs());         }     } }

你能告诉我我做错了什么或者指出我正确的方向来解决这个问题吗?

2 个答案:

答案 0 :(得分:1)

我记得如果你设置EnableViewState = false就会发生这种情况。把它EnableViewState = true!如果网格在每个回发上消失,只需将绑定放在Page_Load方法的if(!IsPostBack)中。

答案 1 :(得分:1)

如果您需要执行服务器端页面索引更改事件或将其设置为EnableSortingAndPagingCallbacks="True"

,请删除false属性