ASP.NET 控制渲染管道

发布于 2024-07-25 04:03:55 字数 1279 浏览 13 评论 0原文

我正在构建的寻呼机控件有问题。 我的代码如下所示

protected override void CreateChildControls()
{
  base.CreateChildControls();

  pnl = new Panel { ID = "NewsPager", CssClass = "NewsPager" };
  Controls.Add(ddl);

  AddPagerControls();
}

AddPagerControls 添加了一堆使用相同事件处理程序的 LinkBut​​ton:

private void li_Click(object sender, EventArgs e)
{
  selectedValue = ((LinkButton) sender).CommandArgument;
  AddPagerControls();
}

现在发生的情况是,当我添加“Next”LinkBut​​ton 时,它会使用以下方法正确设置 CommandArgument:

var liNext = new LinkButton {ID = "NewsPagerLinkNext", Text = ">", CommandArgument = (int.Parse(value) + 1).ToString()};
liNext.Click += new EventHandler(li_Click);
pnl.Controls.Add(liNext);

即,如果当前页面为 2,则调试代码时,“下一步”按钮的 CommandArgument 将为 3。 但是,当页面呈现后,我单击下一个按钮,它将工作一次(从页面 1 到页面 2),但之后它始终为 2,即使在代码中将其设置为 3。因此,当控件被渲染。

我在这里有点不知所措。 我尝试将对 CreateChildControls 中的 AddPagerControls 的调用更改为 if(!Page.IsPostBack){AddPagerControls();} 但事件处理程序根本不会触发。

编辑: 当从事件处理程序调用 AddPagerControls 方法时,PSI 执行 pnl.Controls.Clear(),否则 LinkBut​​tons 将被添加两次。

编辑:PPS 我重新计算每次要添加的链接按钮,因为如果存在更多页面,我只需要显示 5 个链接,即

<< < 2 3 4 5 6 7 > >>>

I have an issue with a pager control i am building. My code is the following

protected override void CreateChildControls()
{
  base.CreateChildControls();

  pnl = new Panel { ID = "NewsPager", CssClass = "NewsPager" };
  Controls.Add(ddl);

  AddPagerControls();
}

AddPagerControls adds a bunch of LinkButtons that use the same event handler:

private void li_Click(object sender, EventArgs e)
{
  selectedValue = ((LinkButton) sender).CommandArgument;
  AddPagerControls();
}

What happens now is that when i add the "Next" LinkButton, it gets it's CommandArgument set correctly using:

var liNext = new LinkButton {ID = "NewsPagerLinkNext", Text = ">", CommandArgument = (int.Parse(value) + 1).ToString()};
liNext.Click += new EventHandler(li_Click);
pnl.Controls.Add(liNext);

i.e. if the current page is 2, then the "Next" button's CommandArgument will be 3 when debugging the code.
However, when the page has rendered and I click the next button it will work once (going from page 1 to 2) but then it will always be 2, even though in code it is set to 3. So something must happen when the control is rendered.

I am a bit at a loss here. I tried changing the call to AddPagerControls in CreateChildControls to if(!Page.IsPostBack){AddPagerControls();} but then the event handler won't fire at all.

Edit: P.S.I do a pnl.Controls.Clear() when the AddPagerControls method is called from event handler, otherwise the LinkButtons would be added twice.

Edit: P.P.S. I recaculate which Linkbuttons to add each time because i need to show only 5 links if more then pages exist i.e.

<< < 2 3 4 5 6 7 > >>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

他夏了夏天 2024-08-01 04:03:55

我不明白的是为什么你总是重新生成分页控件。 难道你不能只使用一种方法,让你的链接按钮“上一个”和“下一个”,并在控件的视图状态(作为单独的属性)中维护正在查看的当前页面和项目数量(即页面大小)?

然后,在事件处理程序中,您可以执行正确的操作,因为您知道当前页面将类似于 currentPageIndex+1 或类似的内容。 从我的角度来看,一直重新生成上一个/下一个按钮,尤其是在 li_Click 处理程序中并不是一个好方法,这也可能会导致您的问题。

What I don't understand is why you regenerate your paging controls all the time. Couldn't you just use an approach where you have your link-buttons "prev" and "next" and you maintain in the viewstate of your control (as separate properties) the current page that is being viewed and the amount of items (i.e. the page-size)?

In the event handler you then do the proper actions since you know that your current page will be something like currentPageIndex+1 or something similar. Regenerating the prev/next buttons all the time, especially in the li_Click handler isn't a good approach from my point of view and this is also what may cause your problems.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文