PageIndexChanging错误(PageIndexChanging error)

编程入门 行业动态 更新时间:2024-10-28 11:31:45
PageIndexChanging错误(PageIndexChanging error)

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

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

<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!] }

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>

Code behind:

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(); } } } } }

Hi Aarthi, Do some changes in your code :-

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(); } } } } }

更多推荐

cmd,GridView,using,Parameters,电脑培训,计算机培训,IT培训"/> <meta name="

本文发布于:2023-08-07 22:00:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1466473.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   PageIndexChanging   error

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!