PageIndexChanging错误

时间:2017-04-21 06:38:19

标签: asp.net .net

我在我的代码中使用Paging并且它抛出错误“GridView'GraceView1'触发了未处理的事件PageIndexChanging。”

以下是我的代码(仅相关部分):

START TRANSACTION;

INSERT INTO Cene_Prenocevanja VALUES (6, 2, 10.00, 22.39, str_to_date('01-01-1990', '%d-%m-%Y'), str_to_date('01-01-1989', '%d-%m-%Y'));

CREATE VIEW zacasni AS
SELECT Zacetek_Veljavnosti FROM Cene_Prenocevanja WHERE IDCene_Prenocevanja=6
INTO @zacetek;
SELECT Konec_Veljavnosti FROM Cene_Prenocevanja WHERE IDCene_Prenocevanja=6
INTO @konec;

IF @zacetek < @konec THEN
    COMMIT;
ELSE
    ROLLBACK;
END IF;

代码背后:

<asp:GridView ID="GridView1" runat="server" AllowPaging="true" PageSize="2" 
              OnPageIndexChanging="SubmitAppraisalGrid_PageIndexChanging">
</asp:GridView>

2 个答案:

答案 0 :(得分:1)

您好Aarthi,您的代码中有一些变化: -

protected void btnSubmit_Click(object sender, EventArgs e)
{
    BindGridView();
}
protected void SubmitAppraisalGrid_PageIndexChanging(objectsender,GridViewPageEventArgs e)
{
    GridView1.PageIndex = e.NewPageIndex;
    BindGridView();
}
protected void BindGridView()
{
    string constr = ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlCommand cmd = new SqlCommand(@"select DB_monitor.name, 
                DB_monitor.priority, DB_alert.creationtime, DB_message.message from DB_alert
                INNER JOIN DB_monitor on DB_alert.monitor = DB_monitor.ID
                INNER JOIN DB_message on DB_alert.errmess = DB_message.ID
                where DB_monitor.application = @strApplication and DB_monitor.instance = 
                @strInstances and DB_monitor.priority = @Priority and
                DB_alert.creationtime between @FromDate and @ToDate and DB_alert.errmess != '1'"))
        {
            cmd.Parameters.Add("@strApplication", System.Data.SqlDbType.VarChar);// Set SqlDbType based on your DB column Data-Type
            cmd.Parameters.Add("@strInstances", System.Data.SqlDbType.VarChar);
            cmd.Parameters.Add("@Priority", System.Data.SqlDbType.Int);
            cmd.Parameters.Add("@FromDate", System.Data.SqlDbType.DateTime);
            cmd.Parameters.Add("@ToDate", System.Data.SqlDbType.DateTime);
            cmd.Parameters["@strApplication"].Value = ddlApplication.SelectedValue;
            cmd.Parameters["@strInstances"].Value = ddlInstances.SelectedValue;
            cmd.Parameters["@Priority"].Value = ddlPriority.SelectedValue;
            //2017-04-01 00:00:00.000
            cmd.Parameters["@FromDate"].Value = Calendar1.SelectedDate.ToString("yyyy-MM-dd HH:mm:ss.fff");
            cmd.Parameters["@ToDate"].Value = Calendar2.SelectedDate.ToString("yyyy-MM-dd HH:mm:ss.fff");


            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                cmd.Connection = con;
                sda.SelectCommand = cmd;
                using (DataTable dt = new DataTable())
                {
                    sda.Fill(dt);
                    GridView1.DataSource = dt;
                    GridView1.DataBind();
                }
            }
        }
    }
}

答案 1 :(得分:0)

您刚刚在aspx中声明了Page index changing事件,因为OnPageIndexChanging =&#34; SubmitAppraisalGrid_PageIndexChanging&#34;但是在你的代码中没有定义相同的

只需在页面后面的代码中定义类似下面的事件处理程序

protected void GridView1_SubmitAppraisalGrid_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    GridView1.PageIndex = e.NewPageIndex;
    GridView1.DataSource = getGridData(); //get datasource for your grid in that method
    GridView1.DataBind(); //bind the data
}

GridView.PageIndexChanging Event

了解详情

使用代码获取getGridData()方法,以获取按钮单击并返回数据表中写入的数据,以便将其分配给Gridview的数据源。

public DataTable getGridData()
{
  string constr = ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString;
  using (SqlConnection con = new SqlConnection(constr))
  {
    using (SqlCommand cmd = new SqlCommand(@"select DB_monitor.name, 
            DB_monitor.priority, DB_alert.creationtime, DB_message.message from DB_alert
            INNER JOIN DB_monitor on DB_alert.monitor = DB_monitor.ID
            INNER JOIN DB_message on DB_alert.errmess = DB_message.ID
            where DB_monitor.application = @strApplication and DB_monitor.instance = 
            @strInstances and DB_monitor.priority = @Priority and
            DB_alert.creationtime between @FromDate and @ToDate and DB_alert.errmess != '1'"))
    {
        cmd.Parameters.Add("@strApplication", System.Data.SqlDbType.VarChar);// Set SqlDbType based on your DB column Data-Type
        cmd.Parameters.Add("@strInstances", System.Data.SqlDbType.VarChar);
        cmd.Parameters.Add("@Priority", System.Data.SqlDbType.Int);
        cmd.Parameters.Add("@FromDate", System.Data.SqlDbType.DateTime);
        cmd.Parameters.Add("@ToDate", System.Data.SqlDbType.DateTime);
        cmd.Parameters["@strApplication"].Value = ddlApplication.SelectedValue;
        cmd.Parameters["@strInstances"].Value = ddlInstances.SelectedValue;
        cmd.Parameters["@Priority"].Value = ddlPriority.SelectedValue;
        //2017-04-01 00:00:00.000
        cmd.Parameters["@FromDate"].Value = Calendar1.SelectedDate.ToString("yyyy-MM-dd HH:mm:ss.fff");
        cmd.Parameters["@ToDate"].Value = Calendar2.SelectedDate.ToString("yyyy-MM-dd HH:mm:ss.fff");


        using (SqlDataAdapter sda = new SqlDataAdapter())
        {
            cmd.Connection = con;
            sda.SelectCommand = cmd;
            using (DataTable dt = new DataTable())
            {
                sda.Fill(dt);
            }
        }
    }
    return dt;
}