本文介绍了ASP.NET:如何更改在&lt每行使用ItemTemplate中; ASP:直放站&GT ;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现有的code是使用 ASP:转发来建立一个HTML表。 (强调现有设计)。这是一些局部的伪code生成表:

 < ASP:直放站OnItemDataBound =的ItemDataBound...>   <&HeaderTemplate中GT;
      <表>
         <&THEAD GT;
            &所述; TR>
               < TD>日期和LT; / TD>
               < TD>类型< / TD>
               < TD>动作< / TD>
            < / TR>
         < /线程>
   < / HeaderTemplate中>   <&ItemTemplate中GT;
      &所述; TR>
         < TD><%#GetTextForThis((MyItem)的Container.DataItem)%GT;< / TD>
         < TD><%#GetTextForThat((MyItem)的Container.DataItem)%GT;< / TD>
         < TD><%#GetTextForTheOther((MyItem)的Container.DataItem)%GT;< / TD>
      < / TR>
   < / ItemTemplate中>   < footertemplate>
         < / TBODY>
      < /表>
   < / footertemplate>< / ASP:直放站>

但现在需要的(某些)项目显示方式不同,它需要的,它使用的是 COLSPAN = 3 完成,整个项目将为一个电池:

 <&ItemTemplate中GT;
      &所述; TR>
         &所述; TD列跨度=3>
            &所述; IMG SRC =&下;%#GetImageSrcForFoo((MyItem)的Container.DataItem)%GT;
            &所述; A HREF =&下;%#GetHrefForFoo((MyItem)的Container.DataItem)%GT;
         < / TD>
      < / TR>
   < / ItemTemplate中>

假设:


  • 页面使用一个中继器来建立一个HTML表格

  • 设计需要一些表行跨列

我怎样才能做到这一点?

我是的希望类似:

 < ItemTemplate中的id =thisThatTheOtherItemTemplate>
      &所述; TR>
         < TD><%#GetTextForThis((MyItem)的Container.DataItem)%GT;< / TD>
         < TD><%#GetTextForThat((MyItem)的Container.DataItem)%GT;< / TD>
         < TD><%#GetTextForTheOther((MyItem)的Container.DataItem)%GT;< / TD>
      < / TR>
   < / ItemTemplate中>
   < ItemTemplate中的id =fooItemTemplate>
      &所述; TR>
         &所述; TD列跨度=3>
            &所述; IMG SRC =&下;%#GetImageSrcForFoo((MyItem)的Container.DataItem)%GT;
            &所述; A HREF =&下;%#GetHrefForFoo((MyItem)的Container.DataItem)%GT;
         < / TD>
      < / TR>
   < / ItemTemplate中>

不知何故神奇地能够有OnDataBind事件告知要使用的模板的中继器。这是possble?

虽然这两个答案都是正确的和好,我宁愿使用第一个建议;其中涉及其在aspx文件显示code,而不是在code-背后手动构建HTML。 (我不希望曾经有重新建立code HTML)

 <&ItemTemplate中GT;
      < ASP:占位符ID =thing1=服务器>
         &所述; TR>
            < TD><%#GetTextForThis((MyItem)的Container.DataItem)%GT;< / TD>
            < TD><%#GetTextForThat((MyItem)的Container.DataItem)%GT;< / TD>
            < TD><%#GetTextForTheOther((MyItem)的Container.DataItem)%GT;< / TD>
         < / TR>
      < / ASP:占位符>      < ASP:占位符ID =thing2可见=假=服务器>
         &所述; TR>
            &所述; TD列跨度=3>
               &所述; IMG SRC =&下;%#GetImageSrcForFoo((MyItem)的Container.DataItem)%GT;
               &所述; A HREF =&下;%#GetHrefForFoo((MyItem)的Container.DataItem)%GT;
            < / TD>
         < / TR>
   < / ItemTemplate中>

和在code-背后:

 保护无效myRepeater_ItemDataBound(对象发件人,RepeaterItemEventArgs E)
{
   //如果数据绑定项目是一个项目或交替项(不是标题等)
   如果(e.Item.ItemType == || ListItemType.Item == e.Item.ItemType ListItemType.AlternatingItem)
   {
      //获取关联的对象
      对象项目= .Item.DataItem;
      如果(项目== NULL)
         返回;      控制thing1 = e.Item.FindControl(thing1);
      控制thing2 = e.Item.FindControl(thing2);      如果(产品MyClassThatICreated)
      {
         thing1.Visible = FALSE;
         thing2.Visible = TRUE;
      }
      其他
      {
         thing1.Visible = TRUE;
         thing2.Visible = FALSE;
      }
      ...
}


解决方案

将两个ASP:占位符控制在您的项目模板。在codebehind,在ItemDataBound事件,确定要显示哪些风格,隐藏一个占位符。

Existing code is using an asp:repeater to build an HTML table. (emphasis on the existing design). This is some partial pseudo-code to generate the table:

<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>

But now it is needed that (some) items display differently, and it is needed that it is done using a COLSPAN=3, and the entire item will be one cell:

   <itemtemplate>
      <tr>
         <td colspan="3">
            <img src="<%# GetImageSrcForFoo((MyItem)Container.DataItem) %>
            <a href="<%# GetHrefForFoo((MyItem)Container.DataItem) %>
         </td>
      </tr>
   </itemtemplate>

Given:

  • the page uses a repeater to build an HTML table
  • the design requires some table rows to span columns

How can i do this?

i was hoping for something like:

   <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>

and somehow magically be able to have the OnDataBind event tell the repeater which template to use. Is this possble?

Answer

Although both answers are right and good, i'd rather use the first suggestion; which involves having display code in the aspx file, rather than building HTML manually in a code-behind. (i don't want to ever have to build HTML in code again)

   <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>

and in the code-behind:

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;
      }
      ...
}
解决方案

Place two ASP:Placeholder controls in your item template. In the codebehind, on the ItemDataBound event, determine which style you want to show, and hide one placeholder.

这篇关于ASP.NET:如何更改在&lt每行使用ItemTemplate中; ASP:直放站&GT ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 00:26