c# – 如何为ASP Repeater中的名称列表创建大写字母行

c# – 如何为ASP Repeater中的名称列表创建大写字母行,第1张

概述我正在使用asp转发器来显示名称列表,我想将当前字母显示为一种分组标题,就像在索引页面中一样. 数据在绑定之前按字母顺序排序,但我发现在显示名称之前很难插入“A”和“B”. 将Panel控件添加到ItemTemplate,并将可见性设置为False.当您绑定转发器时(假设您正在订阅ItemDataBound事件),请运行检查以查看第一个字母是否已更改.如果有,请将面板的可见性设置为true并打印出 我正在使用asp转发器来显示名称列表,我想将当前字母显示为一种分组标题,就像在索引页面中一样.

数据在绑定之前按字母顺序排序,但我发现在显示名称之前很难插入“A”和“B”.

解决方法 将Panel控件添加到ItemTemplate,并将可见性设置为False.当您绑定转发器时(假设您正在订阅ItemDataBound事件),请运行检查以查看第一个字母是否已更改.如果有,请将面板的可见性设置为true并打印出该字母.

如果您需要一些示例代码,请告诉我.

编辑:示例代码

为了清楚起见,“Alphaheaders”就是我们想要显示的“A”,“B”,“C”字母

aspx代码

Repeater看起来像这样:

<table>    <asp:Repeater ID="rptRepeater" runat="server" OnItemDataBound="rptnames_OnItemDataBound">        <ItemTemplate>            <asp:Panel ID="pnlAlphaheader" runat="server" visible="False">               <tr><td><asp:Label ID="lblAlphaheader" runat="server" /></td></tr>            </asp:Panel>            <tr><td><asp:Label ID="lblname" runat="server" /></td></tr>        </ItemTemplate>    </asp:Repeater></table>

aspx.cs代码

首先,您需要一个包含当前Alphaheader的变量:

private string _AlphaheaderCurrent = String.Empty;

然后你将在转发器的OnItemDataBound事件上做你的工作:

protected voID rptnames_OnItemDataBound(object sender,System.Web.UI.WebControls.RepeaterItemEventArgs e){   if ((e.ItemType==ListItemType.Item) || (e.ItemType==ListItemType.AlternatingItem)) {      string name = e.Item.DataItem("name").ToString();      //check if the first letter of the current name is new. If it is new,we print out the header      if(!name.StartsWith(this._AlphaheaderCurrent)) {         this._AlphaheaderCurrent = name.SubString(1);                                        ((Panel)e.ItemFindControl("pnlAlphaheader")).Visible = true;         ((Label)e.Item.FindControl("lblAlphaheader")).Text = the._Alphaheader;      }      ((Label)e.Item.FindControl("lblname")).Text = name;             }}
总结

以上是内存溢出为你收集整理的c# – 如何为ASP Repeater中的名称列表创建大写字母行全部内容,希望文章能够帮你解决c# – 如何为ASP Repeater中的名称列表创建大写字母行所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1217927.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存