//CommonPaginationList.ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="CommonPaginationList.ascx.cs" Inherits="Seek.Web.Controls_CommonPaginationList" %>
<asp:Repeater ID="rep" runat="server" OnItemCommand="rep_ItemCommand" OnItemDataBound="rep_ItemDataBound"></asp:Repeater>
<asp:HiddenField ID="keyWords" runat="server" />
<asp:HiddenField ID="pageIndex" runat="server" Value="1"/>
<asp:HiddenField ID="pageSize" runat="server" Value="10"/>
<asp:HiddenField ID="pageAmount" runat="server" Value="0"/>
<div class="Pagination">
<ul>
<li>第<%=pageIndex.Value%>页 / 共<%=pageAmount.Value%>页</li>
<li>
<asp:LinkButton ID="lbnFirst" runat="server" OnClick="lbnFirst_Click">首页</asp:LinkButton>
<asp:LinkButton ID="lbnPrev" runat="server" OnClick="lbnPrev_Click">上一页</asp:LinkButton>
<asp:LinkButton ID="lbnNext" runat="server" OnClick="lbnNext_Click">下一页</asp:LinkButton>
<asp:LinkButton ID="lbnLast" runat="server" OnClick="lbnLast_Click">尾页</asp:LinkButton>
</li>
<li><asp:LinkButton ID="lbnGo" runat="server" OnClick="lbnGo_Click">转到</asp:LinkButton> 第<asp:TextBox ID="txtPage" runat="server" Width="30px" CssClass="TextBox"></asp:TextBox>页</li>
</ul>
</div>//CommonPaginationList.ascx.cs
using System;
using System.Text.RegularExpressions;
using System.Web.UI.WebControls;namespace Seek.Web
{
public partial class Controls_CommonPaginationList : System.Web.UI.UserControl
{
public delegate object SearchMethod(string searchKeyWord, int pageIndex, int pageSize, out int pageAmount);
public event RepeaterItemEventHandler ItemDataBound;
public event RepeaterCommandEventHandler ItemCommand;
public void InitPagination(string words, string index, string size)
{
keyWords.Value = words;
pageIndex.Value = index;
pageSize.Value = size;
} public string TemplatePath
{
get { return (string)Session[this.ID + "TemplatePath"]; }
set { Session[this.ID + "TemplatePath"] = value; }
} public SearchMethod Method
{
get { return (SearchMethod)Session[this.ID + "Method"]; }
set { Session[this.ID + "Method"] = value; }
} public Repeater Source
{
get { return rep;}
} protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
DataBind();
} public new void DataBind()
{
int index = Convert.ToInt32(pageIndex.Value);
int size = Convert.ToInt32(pageSize.Value);
int amount; rep.ItemTemplate = Page.LoadTemplate(TemplatePath);
rep.DataSource = Method(keyWords.Value, index, size, out amount);
rep.DataBind(); pageAmount.Value = amount.ToString();
txtPage.Text = pageIndex.Value;
} protected void lbnFirst_Click(object sender, EventArgs e)
{
if (Convert.ToInt32(pageAmount.Value) > 1)
{
pageIndex.Value = "1";
DataBind();
}
} protected void lbnPrev_Click(object sender, EventArgs e)
{
if (Convert.ToInt32(pageIndex.Value) > 1)
{
pageIndex.Value = (Convert.ToInt32(pageIndex.Value) - 1).ToString();
DataBind();
}
} protected void lbnNext_Click(object sender, EventArgs e)
{
if (Convert.ToInt32(pageIndex.Value) < Convert.ToInt32(pageAmount.Value))
{
pageIndex.Value = (Convert.ToInt32(pageIndex.Value) + 1).ToString();
DataBind();
}
} protected void lbnLast_Click(object sender, EventArgs e)
{
if (Convert.ToInt32(pageAmount.Value) > 1)
{
pageIndex.Value = pageAmount.Value;
DataBind();
}
} protected void lbnGo_Click(object sender, EventArgs e)
{
if (Regex.IsMatch(txtPage.Text, @"^\d{1,8}$"))
{
int index = Convert.ToInt32(txtPage.Text); if (index <= Convert.ToInt32(pageAmount.Value) && index >= 1)
{
pageIndex.Value = index.ToString();
DataBind();
}
}
} protected void OnItemDataBound(object sender,RepeaterItemEventArgs e)
{
RepeaterItemEventHandler handler = ItemDataBound; if (handler != null)
handler(sender, e);
} protected void rep_ItemDataBound(object sender,RepeaterItemEventArgs e)
{
OnItemDataBound(sender,e);
} protected void OnItemCommand(object source, RepeaterCommandEventArgs e)
{
RepeaterCommandEventHandler handler = ItemCommand;
if (handler != null)
handler(source, e);
}
protected void rep_ItemCommand(object source, RepeaterCommandEventArgs e)
{
OnItemCommand(source, e);
}
}
}//测试模版
<table>
<tr>
<td><asp:CheckBox ID="CheckBox1" runat="server" /></td>
<td><asp:Button ID="Button1" runat="server" Text="Button" CommandName="test" /></td>
</tr>
</table>//测试代码protected void Button1_Click(object sender, EventArgs e)
{
Repeater rep = test.Source; foreach (RepeaterItem item in rep.Items)
{
Label1.Text += item.Controls.Count.ToString() + " "; CheckBox cb = (CheckBox)item.FindControl("CheckBox1"); if (cb!=null)
{
Label1.Text += cb.Checked.ToString();
}
}
}
无法FindControl到控件,Postback的时候动态加载模板的Repeater控件无法保存模板的状态,
请高手帮助解决一下,谢谢。
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="CommonPaginationList.ascx.cs" Inherits="Seek.Web.Controls_CommonPaginationList" %>
<asp:Repeater ID="rep" runat="server" OnItemCommand="rep_ItemCommand" OnItemDataBound="rep_ItemDataBound"></asp:Repeater>
<asp:HiddenField ID="keyWords" runat="server" />
<asp:HiddenField ID="pageIndex" runat="server" Value="1"/>
<asp:HiddenField ID="pageSize" runat="server" Value="10"/>
<asp:HiddenField ID="pageAmount" runat="server" Value="0"/>
<div class="Pagination">
<ul>
<li>第<%=pageIndex.Value%>页 / 共<%=pageAmount.Value%>页</li>
<li>
<asp:LinkButton ID="lbnFirst" runat="server" OnClick="lbnFirst_Click">首页</asp:LinkButton>
<asp:LinkButton ID="lbnPrev" runat="server" OnClick="lbnPrev_Click">上一页</asp:LinkButton>
<asp:LinkButton ID="lbnNext" runat="server" OnClick="lbnNext_Click">下一页</asp:LinkButton>
<asp:LinkButton ID="lbnLast" runat="server" OnClick="lbnLast_Click">尾页</asp:LinkButton>
</li>
<li><asp:LinkButton ID="lbnGo" runat="server" OnClick="lbnGo_Click">转到</asp:LinkButton> 第<asp:TextBox ID="txtPage" runat="server" Width="30px" CssClass="TextBox"></asp:TextBox>页</li>
</ul>
</div>//CommonPaginationList.ascx.cs
using System;
using System.Text.RegularExpressions;
using System.Web.UI.WebControls;namespace Seek.Web
{
public partial class Controls_CommonPaginationList : System.Web.UI.UserControl
{
public delegate object SearchMethod(string searchKeyWord, int pageIndex, int pageSize, out int pageAmount);
public event RepeaterItemEventHandler ItemDataBound;
public event RepeaterCommandEventHandler ItemCommand;
public void InitPagination(string words, string index, string size)
{
keyWords.Value = words;
pageIndex.Value = index;
pageSize.Value = size;
} public string TemplatePath
{
get { return (string)Session[this.ID + "TemplatePath"]; }
set { Session[this.ID + "TemplatePath"] = value; }
} public SearchMethod Method
{
get { return (SearchMethod)Session[this.ID + "Method"]; }
set { Session[this.ID + "Method"] = value; }
} public Repeater Source
{
get { return rep;}
} protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
DataBind();
} public new void DataBind()
{
int index = Convert.ToInt32(pageIndex.Value);
int size = Convert.ToInt32(pageSize.Value);
int amount; rep.ItemTemplate = Page.LoadTemplate(TemplatePath);
rep.DataSource = Method(keyWords.Value, index, size, out amount);
rep.DataBind(); pageAmount.Value = amount.ToString();
txtPage.Text = pageIndex.Value;
} protected void lbnFirst_Click(object sender, EventArgs e)
{
if (Convert.ToInt32(pageAmount.Value) > 1)
{
pageIndex.Value = "1";
DataBind();
}
} protected void lbnPrev_Click(object sender, EventArgs e)
{
if (Convert.ToInt32(pageIndex.Value) > 1)
{
pageIndex.Value = (Convert.ToInt32(pageIndex.Value) - 1).ToString();
DataBind();
}
} protected void lbnNext_Click(object sender, EventArgs e)
{
if (Convert.ToInt32(pageIndex.Value) < Convert.ToInt32(pageAmount.Value))
{
pageIndex.Value = (Convert.ToInt32(pageIndex.Value) + 1).ToString();
DataBind();
}
} protected void lbnLast_Click(object sender, EventArgs e)
{
if (Convert.ToInt32(pageAmount.Value) > 1)
{
pageIndex.Value = pageAmount.Value;
DataBind();
}
} protected void lbnGo_Click(object sender, EventArgs e)
{
if (Regex.IsMatch(txtPage.Text, @"^\d{1,8}$"))
{
int index = Convert.ToInt32(txtPage.Text); if (index <= Convert.ToInt32(pageAmount.Value) && index >= 1)
{
pageIndex.Value = index.ToString();
DataBind();
}
}
} protected void OnItemDataBound(object sender,RepeaterItemEventArgs e)
{
RepeaterItemEventHandler handler = ItemDataBound; if (handler != null)
handler(sender, e);
} protected void rep_ItemDataBound(object sender,RepeaterItemEventArgs e)
{
OnItemDataBound(sender,e);
} protected void OnItemCommand(object source, RepeaterCommandEventArgs e)
{
RepeaterCommandEventHandler handler = ItemCommand;
if (handler != null)
handler(source, e);
}
protected void rep_ItemCommand(object source, RepeaterCommandEventArgs e)
{
OnItemCommand(source, e);
}
}
}//测试模版
<table>
<tr>
<td><asp:CheckBox ID="CheckBox1" runat="server" /></td>
<td><asp:Button ID="Button1" runat="server" Text="Button" CommandName="test" /></td>
</tr>
</table>//测试代码protected void Button1_Click(object sender, EventArgs e)
{
Repeater rep = test.Source; foreach (RepeaterItem item in rep.Items)
{
Label1.Text += item.Controls.Count.ToString() + " "; CheckBox cb = (CheckBox)item.FindControl("CheckBox1"); if (cb!=null)
{
Label1.Text += cb.Checked.ToString();
}
}
}
无法FindControl到控件,Postback的时候动态加载模板的Repeater控件无法保存模板的状态,
请高手帮助解决一下,谢谢。
repeater用法详解