ASP.NET:如何更改每行中使用的itemTemplate?
发布时间:2020-12-16 07:20:56 所属栏目:asp.Net 来源:网络整理
导读:现有代码使用asp:repeater来构建 HTML表. (强调现有设计).这是生成表的一些部分伪代码: asp:repeater OnItemDataBound="ItemDataBound" ... headertemplate table thead tr tdDate/td tdType/td tdAction/td /tr /thread /headertemplate itemtemplate tr
现有代码使用asp:repeater来构建
HTML表. (强调现有设计).这是生成表的一些部分伪代码:
<asp:repeater OnItemDataBound="ItemDataBound" ... > <headertemplate> <table> <thead> <tr> <td>Date</td> <td>Type</td> <td>Action</td> </tr> </thread> </headertemplate> <itemtemplate> <tr> <td><%# GetTextForThis((MyItem)Container.DataItem) %></td> <td><%# GetTextForThat((MyItem)Container.DataItem) %></td> <td><%# GetTextForTheOther((MyItem)Container.DataItem) %></td> </tr> </itemtemplate> <footertemplate> </tbody> </table> </footertemplate> </asp:repeater> 但现在需要(某些)项目以不同方式显示,并且需要使用COLSPAN = 3完成,并且整个项目将是一个单元格: <itemtemplate> <tr> <td colspan="3"> <img src="<%# GetImageSrcForFoo((MyItem)Container.DataItem) %> <a href="<%# GetHrefForFoo((MyItem)Container.DataItem) %> </td> </tr> </itemtemplate> 鉴于: >该页面使用转发器来构建HTML表 我怎样才能做到这一点? 我希望有类似的东西: <itemtemplate id="thisThatTheOtherItemTemplate"> <tr> <td><%# GetTextForThis((MyItem)Container.DataItem) %></td> <td><%# GetTextForThat((MyItem)Container.DataItem) %></td> <td><%# GetTextForTheOther((MyItem)Container.DataItem) %></td> </tr> </itemtemplate> <itemtemplate id="fooItemTemplate"> <tr> <td colspan="3"> <img src="<%# GetImageSrcForFoo((MyItem)Container.DataItem) %> <a href="<%# GetHrefForFoo((MyItem)Container.DataItem) %> </td> </tr> </itemtemplate> 并以某种方式神奇地能够让OnDataBind事件告诉转发器使用哪个模板.这可能吗? 回答 虽然两个答案都是正确的,但我宁愿使用第一个建议;这涉及在aspx文件中显示代码,而不是在代码隐藏中手动构建HTML. (我不想再次在代码中构建HTML) <itemtemplate> <asp:PlaceHolder ID="thing1" runat="server"> <tr> <td><%# GetTextForThis((MyItem)Container.DataItem) %></td> <td><%# GetTextForThat((MyItem)Container.DataItem) %></td> <td><%# GetTextForTheOther((MyItem)Container.DataItem) %></td> </tr> </asp:PlaceHolder> <asp:PlaceHolder ID="thing2" visible="false" runat="server"> <tr> <td colspan="3"> <img src="<%# GetImageSrcForFoo((MyItem)Container.DataItem) %> <a href="<%# GetHrefForFoo((MyItem)Container.DataItem) %> </td> </tr> </itemtemplate> 并在代码隐藏中: protected void myRepeater_ItemDataBound(object sender,RepeaterItemEventArgs e) { // if the data bound item is an item or alternating item (not the header etc) if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { // get the associated object Object item = .Item.DataItem; if (item == null) return; Control thing1 = e.Item.FindControl("thing1"); Control thing2 = e.Item.FindControl("thing2"); if (item is MyClassThatICreated) { thing1.Visible = false; thing2.Visible = true; } else { thing1.Visible = true; thing2.Visible = false; } ... } 解决方法
在项目模板中放置两个ASP:占位符控件.在代码隐藏中,在ItemDataBound事件上,确定要显示的样式,并隐藏一个占位符.
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- ASP.NET:在URL中隐藏查询字符串
- asp.net-mvc – 如何在asp.net mvc3项目中开始使用openID?
- asp.net – 如何在转发器内为用户控件提供“数据”?
- asp.net – 为什么不能在发布模式下构建网站?
- asp.net-mvc-3 – 视图或控制器中的if或else语句
- asp.net – Repeater的SeparatorTemplate与Eval
- asp.net – 在web.config文件中设置重定向
- 剃刀 – Asp.net Core如何呈现视图
- IIS和ASP.Net Web开发服务器之间的行为差??异?
- ASP.NET Core 2.0 Web API响应缓存
推荐文章
站长推荐
- asp.net-mvc – 即使我设置CustomError =“On”,
- asp.net-mvc – asp.net mvc复选框不一致
- asp.net – “DataView未在System.data中标记为可
- asp.net mvc 之旅—— 第一站 从简单的razor入手
- ASP.NET Core中调整HTTP请求大小的几种方法详解
- ASP.NET MVC:如何在MVC应用程序中使用静态HTML页
- 使用ASP.NET/SQL Server丢失会话状态
- asp.net-mvc – DataAnnotations动态附加属性
- asp.net – 多租户架构的实体框架 – 按租户ID过
- 将ASP.NET源代码与已编译的Web应用程序匹配
热点阅读