嗨,大家好:
我在网上down了个模仿petshop4.0中customlist的自定义控件,继承了datalist。
生成没有错误。但是在页面中使用的时候,在<itemtemplate>中无法添加任何东西,包括空格如果有了就会报错--“呈现该控件时出错,请检查并确保所有属性都有效。”如果<itemtemplate>中没有任何东西是没有错的。
百思不得其解。
望高人指点。
下面是该控件的代码。基本就是petshop中的customlist的代码,就修改了几处。using System;
using System.Collections;
using System.Collections.Specialized;
using System.Text;
using System.Text.RegularExpressions;
using System.Web.UI;
using System.Web.UI.WebControls;namespace Control
{
public class CustomList :DataList{
//Static constants
private string HTML1 = "<table cellpadding=0 cellspacing=0 ><tr><td colspan=13>";
protected const string HTML2 = "</td></tr><tr>";
protected const string HTML4 = "</tr></table>";
private static readonly Regex RX = new Regex(@"^&page=d+", RegexOptions.Compiled);
private const string LINK_PREV = "<td><a href=?page={0}> 上一页</a></td>";
private const string LINK_MORE = "<td><a href=?page={0}>下一页 </a></td>";
private const string LINK_DISPLAY = "<td><a href=?page={0}> {1} </a></td>";
private const string KEY_PAGE = "page";
private const string COMMA = "?";
private const string AMP = "&";
protected string emptyText;
private IList dataSource;
private int pageSize = 10;
private int currentPageIndex;
private int itemCount;
override public object DataSource {
set {
//This try catch block is to avoid issues with the VS.NET designer
//The designer will try and bind a datasource which does not derive from ILIST
try {
dataSource = (IList)value;
ItemCount = dataSource.Count;
}
catch {
dataSource = null;
ItemCount = 0;
}
}
} public int PageSize {
get { return pageSize; }
set { pageSize = value; }
} protected int PageCount {
get { return (ItemCount - 1) / pageSize; }
} virtual protected int ItemCount {
get { return itemCount; }
set { itemCount = value; }
} virtual public int CurrentPageIndex {
get { return currentPageIndex; }
set { currentPageIndex = value; }
} public string EmptyText {
set { emptyText = value; }
} public void SetPage(int index) {
OnPageIndexChanged(new DataGridPageChangedEventArgs(null, index));
} override protected void OnLoad(EventArgs e) {
if (Visible) {
string page = Context.Request[KEY_PAGE];
int index = (page != null) ? int.Parse(page)-1 : 0;
SetPage(index);
}
}
/**//// <summary>
/// Overriden method to control how the page is rendered
/// </summary>
/// <param name="writer"></param>
override protected void Render(HtmlTextWriter writer) { //Check there is some data attached
if (ItemCount == 0) {
writer.Write(emptyText);
return;
} //Mask the query
string query = Context.Request.Url.Query.Replace(COMMA, AMP);
query = RX.Replace(query, string.Empty);
// Write out the first part of the control, the table header
writer.Write(HTML1); // Call the inherited method
base.Render(writer);
// Write out a table row closure
writer.Write(HTML2); //列表信息
writer.Write("<td>共 " + PageCount + " 页 " + itemCount + " 项</td>");
//导航到上一页
if (currentPageIndex > 0)
writer.Write(string.Format(LINK_PREV, (currentPageIndex ) + query));
else
{
writer.Write("<td> 上一页 </td>");
} //得到页码导航
for (int i =0; i <= 9 && i <= PageCount && (currentPageIndex - currentPageIndex % 10 + i)<=PageCount ; i++)
{
if (currentPageIndex - currentPageIndex % 10 + i == currentPageIndex)
{
writer.Write("<td> " + (currentPageIndex+1) + " </td>");
}
else
{
writer.Write(string.Format(LINK_DISPLAY, (currentPageIndex - currentPageIndex % 10 + i+1) + query,(currentPageIndex - currentPageIndex % 10 + i+1) )); }
}
//导航到下一页
if (currentPageIndex < PageCount)
writer.Write(string.Format(LINK_MORE, (currentPageIndex + 2) + query));
else
{
writer.Write("<td> 下一页 </td>");
} //Close the table
writer.Write(HTML4);
} override protected void OnDataBinding(EventArgs e) { //Work out which items we want to render to the page
int start = CurrentPageIndex * pageSize;
int size = Math.Min(pageSize, ItemCount - start); IList page = new ArrayList(); //Add the relevant items from the datasource
for (int i = 0; i < size; i++)
page.Add(dataSource[start + i]); //set the base objects datasource
base.DataSource = page;
base.OnDataBinding(e); } public event DataGridPageChangedEventHandler PageIndexChanged; virtual protected void OnPageIndexChanged(DataGridPageChangedEventArgs e) {
if (PageIndexChanged != null)
PageIndexChanged(this, e);
}
}}
我在网上down了个模仿petshop4.0中customlist的自定义控件,继承了datalist。
生成没有错误。但是在页面中使用的时候,在<itemtemplate>中无法添加任何东西,包括空格如果有了就会报错--“呈现该控件时出错,请检查并确保所有属性都有效。”如果<itemtemplate>中没有任何东西是没有错的。
百思不得其解。
望高人指点。
下面是该控件的代码。基本就是petshop中的customlist的代码,就修改了几处。using System;
using System.Collections;
using System.Collections.Specialized;
using System.Text;
using System.Text.RegularExpressions;
using System.Web.UI;
using System.Web.UI.WebControls;namespace Control
{
public class CustomList :DataList{
//Static constants
private string HTML1 = "<table cellpadding=0 cellspacing=0 ><tr><td colspan=13>";
protected const string HTML2 = "</td></tr><tr>";
protected const string HTML4 = "</tr></table>";
private static readonly Regex RX = new Regex(@"^&page=d+", RegexOptions.Compiled);
private const string LINK_PREV = "<td><a href=?page={0}> 上一页</a></td>";
private const string LINK_MORE = "<td><a href=?page={0}>下一页 </a></td>";
private const string LINK_DISPLAY = "<td><a href=?page={0}> {1} </a></td>";
private const string KEY_PAGE = "page";
private const string COMMA = "?";
private const string AMP = "&";
protected string emptyText;
private IList dataSource;
private int pageSize = 10;
private int currentPageIndex;
private int itemCount;
override public object DataSource {
set {
//This try catch block is to avoid issues with the VS.NET designer
//The designer will try and bind a datasource which does not derive from ILIST
try {
dataSource = (IList)value;
ItemCount = dataSource.Count;
}
catch {
dataSource = null;
ItemCount = 0;
}
}
} public int PageSize {
get { return pageSize; }
set { pageSize = value; }
} protected int PageCount {
get { return (ItemCount - 1) / pageSize; }
} virtual protected int ItemCount {
get { return itemCount; }
set { itemCount = value; }
} virtual public int CurrentPageIndex {
get { return currentPageIndex; }
set { currentPageIndex = value; }
} public string EmptyText {
set { emptyText = value; }
} public void SetPage(int index) {
OnPageIndexChanged(new DataGridPageChangedEventArgs(null, index));
} override protected void OnLoad(EventArgs e) {
if (Visible) {
string page = Context.Request[KEY_PAGE];
int index = (page != null) ? int.Parse(page)-1 : 0;
SetPage(index);
}
}
/**//// <summary>
/// Overriden method to control how the page is rendered
/// </summary>
/// <param name="writer"></param>
override protected void Render(HtmlTextWriter writer) { //Check there is some data attached
if (ItemCount == 0) {
writer.Write(emptyText);
return;
} //Mask the query
string query = Context.Request.Url.Query.Replace(COMMA, AMP);
query = RX.Replace(query, string.Empty);
// Write out the first part of the control, the table header
writer.Write(HTML1); // Call the inherited method
base.Render(writer);
// Write out a table row closure
writer.Write(HTML2); //列表信息
writer.Write("<td>共 " + PageCount + " 页 " + itemCount + " 项</td>");
//导航到上一页
if (currentPageIndex > 0)
writer.Write(string.Format(LINK_PREV, (currentPageIndex ) + query));
else
{
writer.Write("<td> 上一页 </td>");
} //得到页码导航
for (int i =0; i <= 9 && i <= PageCount && (currentPageIndex - currentPageIndex % 10 + i)<=PageCount ; i++)
{
if (currentPageIndex - currentPageIndex % 10 + i == currentPageIndex)
{
writer.Write("<td> " + (currentPageIndex+1) + " </td>");
}
else
{
writer.Write(string.Format(LINK_DISPLAY, (currentPageIndex - currentPageIndex % 10 + i+1) + query,(currentPageIndex - currentPageIndex % 10 + i+1) )); }
}
//导航到下一页
if (currentPageIndex < PageCount)
writer.Write(string.Format(LINK_MORE, (currentPageIndex + 2) + query));
else
{
writer.Write("<td> 下一页 </td>");
} //Close the table
writer.Write(HTML4);
} override protected void OnDataBinding(EventArgs e) { //Work out which items we want to render to the page
int start = CurrentPageIndex * pageSize;
int size = Math.Min(pageSize, ItemCount - start); IList page = new ArrayList(); //Add the relevant items from the datasource
for (int i = 0; i < size; i++)
page.Add(dataSource[start + i]); //set the base objects datasource
base.DataSource = page;
base.OnDataBinding(e); } public event DataGridPageChangedEventHandler PageIndexChanged; virtual protected void OnPageIndexChanged(DataGridPageChangedEventArgs e) {
if (PageIndexChanged != null)
PageIndexChanged(this, e);
}
}}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货