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="
发布评论