UpdatePanel中GridView的分页问题

简介: 项目中在UpdatePanel范围内加入了一个GridView控件。如果此页面初始化有数据时,分页效果一切正常。但是当初始化时没有数据,页面中点击按钮加载数据后,再点分页数字时报异常:“Microsoft JScript 运行时错误: 缺少对象”经多方请教,几经周折后,终于搞定这个问题。
项目中在UpdatePanel范围内加入了一个GridView控件。
如果此页面初始化有数据时,分页效果一切正常。
但是当初始化时没有数据,页面中点击按钮加载数据后,再点分页数字时报异常:“Microsoft JScript 运行时错误: 缺少对象”

经多方请教,几经周折后,终于搞定这个问题。

病因:
加入UpdatePanel后,gridview绑定数据属于局部刷新,有些js便没有注册到页面对象中,所以会出现js错误。

解决办法:
1 设置GridView的OnPageIndexChanging事件,在C#后台写入获取新页面索引,绑定数据的代码。
例如:

img_a6339ee3e57d1d52bc7d02b338e15a60.gif      protected   void  GridView1_PageIndexChanging( object  sender, GridViewPageEventArgs e)
img_405b18b4b6584ae338e0f6ecaf736533.gifimg_1c53668bcee393edac0d7b3b3daff1ae.gif    
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif {
img_33d02437d135341f0800e3d415312ae8.gif        GridView1.PageIndex 
= e.NewPageIndex;
img_33d02437d135341f0800e3d415312ae8.gif
img_33d02437d135341f0800e3d415312ae8.gif        GridView1.DataSource 
= getDt();
img_33d02437d135341f0800e3d415312ae8.gif
img_33d02437d135341f0800e3d415312ae8.gif        GridView1.DataBind();
img_05dd8d549cff04457a6366b0a7c9352a.gif    }

2 前台UpdatePanel内给Gridview绑定数据的按钮中,设置属性:PostBackUrl="~/Default.aspx"  。后面的网页写入当前网页的名称
img_a6339ee3e57d1d52bc7d02b338e15a60.gif < asp:Button  ID ="Button1"  runat ="server"  OnClick ="Button1_Click"  Text ="Button"  PostBackUrl ="~/Default.aspx"    />

示例代码
前台ASPX:

img_405b18b4b6584ae338e0f6ecaf736533.gif img_1c53668bcee393edac0d7b3b3daff1ae.gif <% img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif @ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default"  %>
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
img_405b18b4b6584ae338e0f6ecaf736533.gifimg_1c53668bcee393edac0d7b3b3daff1ae.gif
<% img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif @ Register Assembly="C1.Web.C1WebGrid.2" Namespace="C1.Web.C1WebGrid" TagPrefix="C1WebGrid"  %>
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
img_405b18b4b6584ae338e0f6ecaf736533.gifimg_1c53668bcee393edac0d7b3b3daff1ae.gif
<% img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif @ Register Assembly="Microsoft.Web.Atlas" Namespace="Microsoft.Web.UI" TagPrefix="cc1"  %>
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
< html  xmlns ="http://www.w3.org/1999/xhtml"   >
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
< head  runat ="server" >
img_a6339ee3e57d1d52bc7d02b338e15a60.gif    
< title > 无标题页 </ title >
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
</ head >
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
< body >
img_a6339ee3e57d1d52bc7d02b338e15a60.gif    
< form  id ="form1"  runat ="server" >
img_a6339ee3e57d1d52bc7d02b338e15a60.gif    
< div >
img_a6339ee3e57d1d52bc7d02b338e15a60.gif        
< cc1:ScriptManager  ID ="ScriptManager1"  runat ="server"  EnablePartialRendering ="True" >
img_a6339ee3e57d1d52bc7d02b338e15a60.gif            
< ErrorTemplate >
img_a6339ee3e57d1d52bc7d02b338e15a60.gif                
< div  style ="padding: 12px; width: 400px; height: 140px; border: #000000 1px solid;
img_a6339ee3e57d1d52bc7d02b338e15a60.gif                    background-color: white; text-align: left"
>
img_a6339ee3e57d1d52bc7d02b338e15a60.gif                    An error has occurred:
< br  />
img_a6339ee3e57d1d52bc7d02b338e15a60.gif                    
< span  id ="errorMessageLabel"  runat ="server" ></ span >
img_a6339ee3e57d1d52bc7d02b338e15a60.gif                    
< br  />
img_a6339ee3e57d1d52bc7d02b338e15a60.gif                    
< br  />
img_a6339ee3e57d1d52bc7d02b338e15a60.gif                    
< input  id ="okButton"  runat ="server"  type ="button"  value ="OK"   />
img_a6339ee3e57d1d52bc7d02b338e15a60.gif                
img_a6339ee3e57d1d52bc7d02b338e15a60.gif            
</ ErrorTemplate >
img_a6339ee3e57d1d52bc7d02b338e15a60.gif        
</ cc1:ScriptManager >
img_a6339ee3e57d1d52bc7d02b338e15a60.gif        
&nbsp; </ div >
img_a6339ee3e57d1d52bc7d02b338e15a60.gif        
< cc1:UpdatePanel  ID ="UpdatePanel1"  runat ="server" >
img_a6339ee3e57d1d52bc7d02b338e15a60.gif            
< ContentTemplate >
img_a6339ee3e57d1d52bc7d02b338e15a60.gif        
< asp:Button  ID ="Button1"  runat ="server"  OnClick ="Button1_Click"  Text ="Button"  PostBackUrl ="~/Default.aspx"    />
img_a6339ee3e57d1d52bc7d02b338e15a60.gif        
< asp:GridView  ID ="GridView1"  runat ="server"  AllowPaging ="True"  AllowSorting ="True"  AutoGenerateColumns ="False"  OnPageIndexChanging ="GridView1_PageIndexChanging" >
img_a6339ee3e57d1d52bc7d02b338e15a60.gif            
< Columns >
img_a6339ee3e57d1d52bc7d02b338e15a60.gif                
< asp:TemplateField >
img_a6339ee3e57d1d52bc7d02b338e15a60.gif                    
< ItemTemplate >
img_a6339ee3e57d1d52bc7d02b338e15a60.gif                        
< asp:TextBox  ID ="TextBox1"  runat ="server"  Text =<%#  Eval("id") % > > </ asp:TextBox >
img_a6339ee3e57d1d52bc7d02b338e15a60.gif                    
</ ItemTemplate >
img_a6339ee3e57d1d52bc7d02b338e15a60.gif                
</ asp:TemplateField >
img_a6339ee3e57d1d52bc7d02b338e15a60.gif                
< asp:TemplateField >
img_a6339ee3e57d1d52bc7d02b338e15a60.gif                    
< ItemTemplate >
img_a6339ee3e57d1d52bc7d02b338e15a60.gif                        
< asp:TextBox  ID ="TextBox2"  runat ="server"  Text =<%#  Eval("name") % > > </ asp:TextBox >
img_a6339ee3e57d1d52bc7d02b338e15a60.gif                    
</ ItemTemplate >
img_a6339ee3e57d1d52bc7d02b338e15a60.gif                
</ asp:TemplateField >
img_a6339ee3e57d1d52bc7d02b338e15a60.gif                
< asp:BoundField  DataField ="name"   />
img_a6339ee3e57d1d52bc7d02b338e15a60.gif            
</ Columns >
img_a6339ee3e57d1d52bc7d02b338e15a60.gif        
</ asp:GridView >
img_a6339ee3e57d1d52bc7d02b338e15a60.gif            
</ ContentTemplate >
img_a6339ee3e57d1d52bc7d02b338e15a60.gif        
</ cc1:UpdatePanel >
img_a6339ee3e57d1d52bc7d02b338e15a60.gif        
</ div >
img_a6339ee3e57d1d52bc7d02b338e15a60.gif    
</ form >
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
</ body >
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
</ html >
img_a6339ee3e57d1d52bc7d02b338e15a60.gif

后台CS:
img_a6339ee3e57d1d52bc7d02b338e15a60.gif using  System;
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
using  System.Data;
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
using  System.Configuration;
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
using  System.Web;
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
using  System.Web.Security;
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
using  System.Web.UI;
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
using  System.Web.UI.WebControls;
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
using  System.Web.UI.WebControls.WebParts;
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
using  System.Web.UI.HtmlControls;
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
public  partial  class  _Default : System.Web.UI.Page 
img_405b18b4b6584ae338e0f6ecaf736533.gifimg_1c53668bcee393edac0d7b3b3daff1ae.gif
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif {
img_33d02437d135341f0800e3d415312ae8.gif    
protected void Page_Load(object sender, EventArgs e)
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif    
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif        
if (!IsPostBack)
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif        
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif            
//this.GridView1.DataSource = this.getDt();
img_33d02437d135341f0800e3d415312ae8.gif            
//this.GridView1.DataBind();
img_105a1e124122b2abcee4ea8e9f5108f3.gif
        }

img_105a1e124122b2abcee4ea8e9f5108f3.gif    }

img_33d02437d135341f0800e3d415312ae8.gif
img_33d02437d135341f0800e3d415312ae8.gif    
protected void Button1_Click(object sender, EventArgs e)
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif    
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif        
this.GridView1.DataSource = this.getDt(); 
img_33d02437d135341f0800e3d415312ae8.gif        
this.GridView1.DataBind();
img_105a1e124122b2abcee4ea8e9f5108f3.gif    }

img_33d02437d135341f0800e3d415312ae8.gif
img_33d02437d135341f0800e3d415312ae8.gif    
private DataTable getDt()
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif    
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif        DataTable dt 
= new DataTable();
img_33d02437d135341f0800e3d415312ae8.gif        dt.Columns.Add(
"id");
img_33d02437d135341f0800e3d415312ae8.gif        dt.Columns.Add(
"name");
img_33d02437d135341f0800e3d415312ae8.gif
img_33d02437d135341f0800e3d415312ae8.gif        DataRow dr 
= null;
img_33d02437d135341f0800e3d415312ae8.gif        
for (int i = 0; i < 20; i++)
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif        
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif            dr 
= dt.NewRow();
img_33d02437d135341f0800e3d415312ae8.gif            dr[
"id"= i.ToString();
img_33d02437d135341f0800e3d415312ae8.gif            dr[
"name"= "名字" + i.ToString();
img_33d02437d135341f0800e3d415312ae8.gif
img_33d02437d135341f0800e3d415312ae8.gif            dt.Rows.Add(dr);
img_105a1e124122b2abcee4ea8e9f5108f3.gif        }

img_33d02437d135341f0800e3d415312ae8.gif
img_33d02437d135341f0800e3d415312ae8.gif        
return dt;
img_105a1e124122b2abcee4ea8e9f5108f3.gif    }

img_33d02437d135341f0800e3d415312ae8.gif    
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif    
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif        GridView1.PageIndex 
= e.NewPageIndex;
img_33d02437d135341f0800e3d415312ae8.gif
img_33d02437d135341f0800e3d415312ae8.gif        GridView1.DataSource 
= getDt();
img_33d02437d135341f0800e3d415312ae8.gif
img_33d02437d135341f0800e3d415312ae8.gif        GridView1.DataBind();
img_105a1e124122b2abcee4ea8e9f5108f3.gif    }

img_05dd8d549cff04457a6366b0a7c9352a.gif}

img_a6339ee3e57d1d52bc7d02b338e15a60.gif
目录
相关文章
|
前端开发 JavaScript Java
Element-UI中Select选择器讲解(el-select详解)
案例详解Element-UI中Select选择器讲解,手把手教学!
1116 0
Element-UI中Select选择器讲解(el-select详解)
|
XML 数据格式 容器
翻页类视图 ViewPager
翻页类视图 ViewPager一、ViewPage简单介绍
|
JavaScript 前端开发
Element-ui(el-table、el-pagination)实现表格分页
Element-ui(el-table、el-pagination)实现表格分页
1053 0
|
JavaScript 前端开发 CDN
一个还算实用的分页组件:xy-pagination
一个还算实用的分页组件:xy-pagination
一个还算实用的分页组件:xy-pagination
|
前端开发
前端案例:基于el-table和el-pagination实现数据的分页效果
前端案例:基于el-table和el-pagination实现数据的分页效果
884 0
前端案例:基于el-table和el-pagination实现数据的分页效果
Element-UI. el-select 选中对象
Element-UI. el-select 选中对象
236 0

热门文章

最新文章