我的页面结构是这样的:
<div style="BORDER-BOTTOM: black 1px solid; BORDER-LEFT: black 1px solid; OVERFLOW-Y: auto; WIDTH: 100%; ; HEIGHT: expression(document.body.clientHeight-270); BORDER-TOP: black 1px solid; BORDER-RIGHT: black 1px solid"id="divGrid">
<asp:datalist id="dlResults" runat="server" Width="100%" CellSpacing="0" CellPadding="0" RepeatDirection="Vertical">
...
</asp:datalist>
</div>
<table class="bodytext8pt" border="0" width="100%">
<tr>
<td><asp:panel id="pnlPager" Runat="server" CssClass="GridFooter"></asp:panel></td>
</tr>
</table>
在代码隐藏的分页器中是由 BuildPager 函数构建的:
private void BuildPager(DataTable dt)
{
pnlPager.Controls.Clear();
Label l = new Label();
l.Text = " (" + pgResults.PageCount.ToString("#,##0") + " pages, " + dt.Rows.Count.ToString("#,##0") + " records ) ";
pnlPager.Controls.Add(l);
for (int i = 0; i < pnlPager.Controls.Count; i++)
{
if (pnlPager.Controls[i].ToString() == "System.Web.UI.WebControls.DataGridLinkButton")
{
try
{
LinkButton c = (LinkButton) pnlPager.Controls[i];
c.CssClass = "GridFooter";
}
catch (Exception ex) { }
}
}
pnlPager.Attributes.Add("class", "GridFooter");
l = new Label();
l.Width = Unit.Pixel(5);
pnlPager.Controls.AddAt(0, l);
LinkButton lb = null;
lb = new LinkButton();
lb.Text = "Previous";
lb.Attributes.Add("OnClick", "goPage(" + Convert.ToString(Convert.ToInt32(pgResults.CurrentPageIndex) - 1) + ");");
lb.CommandArgument = Convert.ToString(Convert.ToInt32(pgResults.CurrentPageIndex) - 1);
if (pgResults.CurrentPageIndex == 0)
{
lb.Enabled = false;
lb.Style.Add("cursor", "default");
}
else
{
lb.Attributes.Add("class", "GridFooter");
}
**lb.Click += new EventHandler(Pager_Click);
pnlPager.Controls.AddAt(0, lb);**
l = new Label();
l.Width = Unit.Pixel(5);
pnlPager.Controls.AddAt(0, l);
lb = new LinkButton();
lb.Text = "First";
lb.Attributes.Add("OnClick", "goPage(0);");
lb.CommandArgument = Convert.ToString(0);
if (pgResults.CurrentPageIndex == 0)
{
lb.Enabled = false;
lb.Style.Add("cursor", "default");
}
else
{
lb.Attributes.Add("class", "GridFooter");
}
**lb.Click += new EventHandler(Pager_Click);
pnlPager.Controls.AddAt(0, lb);**
l = new Label();
l.Width = Unit.Pixel(5);
pnlPager.Controls.AddAt(0, l);
//Build the numeric links..
for(int i=0; i< pgResults.PageCount; i++)
{
l = new Label();
l.Width = Unit.Pixel(5);
pnlPager.Controls.AddAt(pnlPager.Controls.Count - 1, l);
lb = new LinkButton();
lb.Text = Convert.ToString(i+1);
lb.Attributes.Add("OnClick", "goPage(" + Convert.ToString(i) + ");");
lb.CommandArgument = Convert.ToString(i);
if (pgResults.CurrentPageIndex == i)
{
lb.Enabled = false;
lb.Style.Add("cursor", "default");
}
else
{
lb.Attributes.Add("class", "GridFooter");
**lb.Click += new EventHandler(Pager_Click);**
}
pnlPager.Controls.AddAt(pnlPager.Controls.Count - 1, lb);
}
//End of numeric links
l = new Label();
l.Width = Unit.Pixel(5);
pnlPager.Controls.AddAt(pnlPager.Controls.Count - 1, l);
lb = new LinkButton();
lb.Text = "Next";
lb.Attributes.Add("OnClick", "goPage(" + Convert.ToString(Convert.ToInt32(pgResults.CurrentPageIndex) + 1) + ");");
lb.CommandArgument = Convert.ToString(Convert.ToInt32(pgResults.CurrentPageIndex) + 1);
if (pgResults.CurrentPageIndex == pgResults.PageCount - 1)
{
lb.Enabled = false;
lb.Style.Add("cursor", "default");
}
else
{
lb.Attributes.Add("class", "GridFooter");
}
**lb.Click += new EventHandler(Pager_Click);**
pnlPager.Controls.AddAt(pnlPager.Controls.Count - 1, lb);
l = new Label();
l.Width = Unit.Pixel(5);
pnlPager.Controls.AddAt(pnlPager.Controls.Count - 1, l);
lb = new LinkButton();
lb.Text = "Last";
lb.Attributes.Add("OnClick", "goPage(" + Convert.ToString(Convert.ToInt32(pgResults.PageCount) - 1) + ");");
lb.CommandArgument = Convert.ToString(Convert.ToInt32(pgResults.PageCount) - 1);
if (pgResults.CurrentPageIndex == pgResults.PageCount - 1)
{
lb.Enabled = false;
lb.Style.Add("cursor", "default");
}
else
{
lb.Attributes.Add("class", "GridFooter");
}
**lb.Click += new EventHandler(Pager_Click);**
pnlPager.Controls.AddAt(pnlPager.Controls.Count - 1, lb);
l = new Label();
l.Width = Unit.Pixel(5);
pnlPager.Controls.AddAt(pnlPager.Controls.Count - 1, l);
}
数字标签随点击事件一起添加:
private void Pager_Click(object sender, EventArgs e)
{
LinkButton lb = (LinkButton) sender;
pgResults.CurrentPageIndex = Convert.ToInt32(lb.CommandArgument);
BindList(false);
}
但是当我单击这些数字或"下一个","上一个"时,此pager_click不会触发。我是否以正确的方式添加了事件处理程序。请建议
谢谢
找到了安慰。动态创建的控件在页面加载时丢失,因此,我们需要在回发中再次加载它们。