项目中在UpdatePanel范围内加入了一个GridView控件。
如果此页面初始化有数据时,分页效果一切正常。
但是当初始化时没有数据,页面中点击按钮加载数据后,再点分页数字时报异常:“Microsoft JScript 运行时错误: 缺少对象”
经多方请教,几经周折后,终于搞定这个问题。
病因:
加入UpdatePanel后,gridview绑定数据属于局部刷新,有些js便没有注册到页面对象中,所以会出现js错误。
解决办法:
1 设置GridView的OnPageIndexChanging事件,在C#后台写入获取新页面索引,绑定数据的代码。
例如:
2 前台UpdatePanel内给Gridview绑定数据的按钮中,设置属性:PostBackUrl="~/Default.aspx" 。后面的网页写入当前网页的名称
示例代码
前台ASPX:
后台CS:
如果此页面初始化有数据时,分页效果一切正常。
但是当初始化时没有数据,页面中点击按钮加载数据后,再点分页数字时报异常:“Microsoft JScript 运行时错误: 缺少对象”
经多方请教,几经周折后,终于搞定这个问题。
病因:
加入UpdatePanel后,gridview绑定数据属于局部刷新,有些js便没有注册到页面对象中,所以会出现js错误。
解决办法:
1 设置GridView的OnPageIndexChanging事件,在C#后台写入获取新页面索引,绑定数据的代码。
例如:
protected
void
GridView1_PageIndexChanging(
object
sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataSource = getDt();
GridView1.DataBind();
}
{
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataSource = getDt();
GridView1.DataBind();
}
2 前台UpdatePanel内给Gridview绑定数据的按钮中,设置属性:PostBackUrl="~/Default.aspx" 。后面的网页写入当前网页的名称
<
asp:Button
ID
="Button1"
runat
="server"
OnClick
="Button1_Click"
Text
="Button"
PostBackUrl
="~/Default.aspx"
/>
示例代码
前台ASPX:
<%
@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default"
%>
<% @ Register Assembly="C1.Web.C1WebGrid.2" Namespace="C1.Web.C1WebGrid" TagPrefix="C1WebGrid" %>
<% @ Register Assembly="Microsoft.Web.Atlas" Namespace="Microsoft.Web.UI" TagPrefix="cc1" %>
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
< html xmlns ="http://www.w3.org/1999/xhtml" >
< head runat ="server" >
< title > 无标题页 </ title >
</ head >
< body >
< form id ="form1" runat ="server" >
< div >
< cc1:ScriptManager ID ="ScriptManager1" runat ="server" EnablePartialRendering ="True" >
< ErrorTemplate >
< div style ="padding: 12px; width: 400px; height: 140px; border: #000000 1px solid;
background-color: white; text-align: left" >
An error has occurred: < br />
< span id ="errorMessageLabel" runat ="server" ></ span >
< br />
< br />
< input id ="okButton" runat ="server" type ="button" value ="OK" />
</ ErrorTemplate >
</ cc1:ScriptManager >
</ div >
< cc1:UpdatePanel ID ="UpdatePanel1" runat ="server" >
< ContentTemplate >
< asp:Button ID ="Button1" runat ="server" OnClick ="Button1_Click" Text ="Button" PostBackUrl ="~/Default.aspx" />
< asp:GridView ID ="GridView1" runat ="server" AllowPaging ="True" AllowSorting ="True" AutoGenerateColumns ="False" OnPageIndexChanging ="GridView1_PageIndexChanging" >
< Columns >
< asp:TemplateField >
< ItemTemplate >
< asp:TextBox ID ="TextBox1" runat ="server" Text =<%# Eval("id") % > > </ asp:TextBox >
</ ItemTemplate >
</ asp:TemplateField >
< asp:TemplateField >
< ItemTemplate >
< asp:TextBox ID ="TextBox2" runat ="server" Text =<%# Eval("name") % > > </ asp:TextBox >
</ ItemTemplate >
</ asp:TemplateField >
< asp:BoundField DataField ="name" />
</ Columns >
</ asp:GridView >
</ ContentTemplate >
</ cc1:UpdatePanel >
</ div >
</ form >
</ body >
</ html >
<% @ Register Assembly="C1.Web.C1WebGrid.2" Namespace="C1.Web.C1WebGrid" TagPrefix="C1WebGrid" %>
<% @ Register Assembly="Microsoft.Web.Atlas" Namespace="Microsoft.Web.UI" TagPrefix="cc1" %>
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
< html xmlns ="http://www.w3.org/1999/xhtml" >
< head runat ="server" >
< title > 无标题页 </ title >
</ head >
< body >
< form id ="form1" runat ="server" >
< div >
< cc1:ScriptManager ID ="ScriptManager1" runat ="server" EnablePartialRendering ="True" >
< ErrorTemplate >
< div style ="padding: 12px; width: 400px; height: 140px; border: #000000 1px solid;
background-color: white; text-align: left" >
An error has occurred: < br />
< span id ="errorMessageLabel" runat ="server" ></ span >
< br />
< br />
< input id ="okButton" runat ="server" type ="button" value ="OK" />
</ ErrorTemplate >
</ cc1:ScriptManager >
</ div >
< cc1:UpdatePanel ID ="UpdatePanel1" runat ="server" >
< ContentTemplate >
< asp:Button ID ="Button1" runat ="server" OnClick ="Button1_Click" Text ="Button" PostBackUrl ="~/Default.aspx" />
< asp:GridView ID ="GridView1" runat ="server" AllowPaging ="True" AllowSorting ="True" AutoGenerateColumns ="False" OnPageIndexChanging ="GridView1_PageIndexChanging" >
< Columns >
< asp:TemplateField >
< ItemTemplate >
< asp:TextBox ID ="TextBox1" runat ="server" Text =<%# Eval("id") % > > </ asp:TextBox >
</ ItemTemplate >
</ asp:TemplateField >
< asp:TemplateField >
< ItemTemplate >
< asp:TextBox ID ="TextBox2" runat ="server" Text =<%# Eval("name") % > > </ asp:TextBox >
</ ItemTemplate >
</ asp:TemplateField >
< asp:BoundField DataField ="name" />
</ Columns >
</ asp:GridView >
</ ContentTemplate >
</ cc1:UpdatePanel >
</ div >
</ form >
</ body >
</ html >
后台CS:
using
System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//this.GridView1.DataSource = this.getDt();
//this.GridView1.DataBind();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
this.GridView1.DataSource = this.getDt();
this.GridView1.DataBind();
}
private DataTable getDt()
{
DataTable dt = new DataTable();
dt.Columns.Add("id");
dt.Columns.Add("name");
DataRow dr = null;
for (int i = 0; i < 20; i++)
{
dr = dt.NewRow();
dr["id"] = i.ToString();
dr["name"] = "名字" + i.ToString();
dt.Rows.Add(dr);
}
return dt;
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataSource = getDt();
GridView1.DataBind();
}
}
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//this.GridView1.DataSource = this.getDt();
//this.GridView1.DataBind();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
this.GridView1.DataSource = this.getDt();
this.GridView1.DataBind();
}
private DataTable getDt()
{
DataTable dt = new DataTable();
dt.Columns.Add("id");
dt.Columns.Add("name");
DataRow dr = null;
for (int i = 0; i < 20; i++)
{
dr = dt.NewRow();
dr["id"] = i.ToString();
dr["name"] = "名字" + i.ToString();
dt.Rows.Add(dr);
}
return dt;
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataSource = getDt();
GridView1.DataBind();
}
}