PageIndexChanging错误 [英] PageIndexChanging error
本文介绍了PageIndexChanging错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在代码中使用Paging,它引发错误未处理的GridView'GridView1'触发了事件PageIndexChanging."
I am using Paging in my code and it throws error "The GridView 'GridView1' fired event PageIndexChanging which wasn't handled."
以下是我的代码(仅相关部分):
Following is my code (only the relevant part):
<asp:GridView ID="GridView1" runat="server" AllowPaging="true" PageSize="2"
OnPageIndexChanging="SubmitAppraisalGrid_PageIndexChanging">
</asp:GridView>
后面的代码:
protected void btnSubmit_Click(object sender, EventArgs e)
{
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();
}
}
}
}
}
} // [Edit: sic!]
}
推荐答案
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();
}
}
}
}
}
这篇关于PageIndexChanging错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文