动态添加的 LinkButton CommandName/CommandArgument 设置被删除
我正在尝试在表格上方实现分页栏。我的页面上有一个静态
元素:<div><center><ul id="topnav" runat="server"></ul></center></div>
在我的 Page_Load 事件中,我尝试使用页码加载它:
foreach ( int linkpage in linkpages ) {
HtmlGenericControl li;
LinkButton pbut;
li = new HtmlGenericControl( "li" );
pbut = new LinkButton();
pbut.CommandName = "page";
pbut.CommandArgument = linkpage.ToString();
pbut.Text = linkpage.ToString();
li.Controls.Add( pbut );
topnav.Controls.Add( li );
}
但是,当控件实际呈现时,它会以名称“ctlXX”发布(其中 XX 是一个递增的数字)并且没有参数,所以我无法获取页码。然而,.Text 属性渲染得很好。我做错了什么?这是完全错误的做法吗?
I am attempting to implement a pagination bar above a table. I have a static <ul>
element on my page:
<div><center><ul id="topnav" runat="server"></ul></center></div>
In my Page_Load event, I attempt to load it with page numbers:
foreach ( int linkpage in linkpages ) {
HtmlGenericControl li;
LinkButton pbut;
li = new HtmlGenericControl( "li" );
pbut = new LinkButton();
pbut.CommandName = "page";
pbut.CommandArgument = linkpage.ToString();
pbut.Text = linkpage.ToString();
li.Controls.Add( pbut );
topnav.Controls.Add( li );
}
However, when the control is actually rendered it posts as name 'ctlXX' (where XX is an incrementing number) and no argument, so I am unable to get the page number. The .Text attribute renders fine, however. What am I doing wrong? Is this entirely the wrong approach?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果要动态添加按钮,则需要在
Page_Init
方法中执行此操作,否则控件将不会参与 ViewState。这可能是问题所在。当然不会有帮助。此外,该名称是 .Net 动态分配给按钮的名称,因为您自己没有指定 ID。如果您正在查找的话,该 ID 将在
Request.Form
集合中可见。但是 - 老实说,我认为你的做法是错误的。如果您想要分页,那么您应该查看诸如 GridView 或 ListView 控件之类的控件,它们内置了分页功能。这将是更标准的做事方式。 GridView 分页解释此处以及 ListView 的一般教程,包括分页此处。
一般来说,分页是一个在 ASP.Net 中已经解决了很多次的问题,您真的不想重新发明轮子 - 好吧,我不想。此外,通过使用这些类型的网格控件,您将获得诸如开箱即用的排序之类的东西 - 另一个非常常见的要求。
If you are dynamically adding buttons you need to do it in the
Page_Init
method otherwise the control won't participate in ViewState. The could be the problem. Certainly it won't help.Also the name is what .Net dynamically assigns to the button because you haven't specified an ID yourself. It will be that ID that is visible in the
Request.Form
collection if that is where you are looking.But - i think your approach is wrong to be honest. If you want pagination then you should be looking at something like a GridView or ListView control which has pagination built into the control. That would be the more standard what of doing things. GridView paging explained here and a general tutorial for ListViews including pagination here.
Generally pagination is a problem that has been solved a number of times in ASP.Net and you really wouldn't want to reinvent the wheel - well I wouldn't. Also by using these type of grid controls you will get things like sorting out of the box - another very common requirement.