如何控制datalist控件中,要求显示数据规定数量,而且是默认的竖着显示的?

时间:2021-11-24 04:28:55
如何控制datalist控件中,要求显示数据规定数量,而且是默认的竖着显示的?
规定datalist 控件中ItemTemplate 中以table展示  而table竖着只可以显示规定的几个.. 

16 个解决方案

#1


效果图

#2


<datelist>
<ItemTemplate >
  <table></table>
  <table></table>
  <table></table>
</ItemTemplate >
</datelist>

#3


贴点带数据的效果图

#4


<asp:DataList ID="dlSueByToday" runat="server" Height="90px" Width="518px"  RepeatDirection ="Vertical" >
    <HeaderTemplate >
        <h1 ><%= string .Format ("{0:yyyy-MM-dd}",DateTime.Now ) %> 旅行日 </h1> 
    </HeaderTemplate>
        <ItemTemplate>
        <li>
            <table style="border-bottom:#bbbbbb 1px dashed ; widows :518px;  border-bottom-style:dashed" >
                <tr>
                    <td rowspan ="3" width="102" height="102">
                       <img src ="../images/img02.jpg" alt ="" border="0" />
                    </td>
                    <td>
                        <h3><a class="link-pink" href="#" title="目的地指南"><%# GetTop(Eval("typeId"))%></a> </h3>
                    </td>
                </tr>
                <tr>
                    <td >
                     <a href ="#"><span class="span-green" ><%# Eval("Title")%> </span></a>- <a href ="#"><span class="span-yellow"><%# Eval("User.NickName")%> </span> </a>
                    </td>
                </tr>
                <tr>
                    <td style="line-height: 20px">
                      <%# Eval("Content")%>
  
                    </td>
                </tr>
                <tr>
                    <td  colspan ="2">
                     <div class="more"><a class="link-pink" href="#" title="阅读更多">阅读全部&raquo;</a></div>
                    </td>
                </tr>
            </table></li>
           
  
            <br />
        </ItemTemplate>
  </asp:DataList>

#5


引用 2 楼 mmerry 的回复:
<datelist>
<ItemTemplate >
  <table> </table>
  <table> </table>
  <table> </table>
</ItemTemplate >
</datelist>

你这样的table不就是竖着叠放的吗?

#6


是啊 我知道啊 默认的也就是竖着的啊 可是我现在要限制 table 显示的数量 

#7


你的数据是什么样的

#8


数据? 就是查了数据库中所有的数据 是个list 了  
哦 你的意思是我查数据的时候给他直接限制了 select top 5 * from table 是不是这样啊? 

#9


引用 6 楼 mmerry 的回复:
是啊 我知道啊 默认的也就是竖着的啊 可是我现在要限制 table 显示的数量

将table设置为runat="server" 并设置id,如:
<table id="t1" runat="server">...</table>
<table id="t2" runat="server">...</table>
<table id="t3" runat="server">...</table>

后台根据条件显示或隐藏某些table,大致代码,如在ItemDataBound事件中的代码:
    protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        HtmlTable tab1 = (HtmlTable)e.Item.FindControl("t1");
        HtmlTable tab2 = (HtmlTable)e.Item.FindControl("t2");
        HtmlTable tab3 = (HtmlTable)e.Item.FindControl("t3");

        if(某个条件1)
            tab1.Attributes.Add("style", "display:none");//隐藏t1

        if(某个条件2)
            tab1.Attributes.Add("style", "display:block");//显示t2
        
        ...........
    }

当然先要using一下:
using System.Web.UI.HtmlControls;

#10


被你们整晕了,很简单的,他要实现的是像网上商城那种,一行显示几个商品,
<asp:DataList ID="DataList1" runat="server" RepeatColumns="4" DataKeyField="productID"
                                OnItemDataBound="DataList1_ItemDataBound" OnItemCommand="DataList1_ItemCommand">
                                <ItemTemplate>
                                   <!-- 在这里构造你想要的任何显示方式-->


RepeatColumns="4" 指定一行有几列 DataKeyField不用说了吧,然后不要忘了绑定,祝贺成功!


#11


我的意思 是你提供一些测试数据 和你想要的效果图

#12


更全的源码,你照着改改就知道原理了,看见table就改,然后改下 DataKeyField="你的绑定ID"
<asp:DataList ID="DataList1" runat="server" RepeatColumns="4" DataKeyField="productID"
                                OnItemDataBound="DataList1_ItemDataBound" OnItemCommand="DataList1_ItemCommand">
                                <ItemTemplate>
                                    <table border="0" cellpadding="0" cellspacing="0" style="width: 157px; height: 1px">
                                        <tr>
                                            <td style="width: 99px; height: 131px;" align="center">
                                               <a href='ShowShop.aspx?pid=<%# Eval("productID") %>'> <asp:Image ID="Image1" runat="server" ImageUrl='<%#"~/Images/SmallImages/"+Eval("productImg")%>' /></a></td>
                                        </tr>
                                        <tr>
                                            <td style="width: 99px; height: 18px; font-size: 12pt; color: black; font-family: 宋体;"
                                                align="right">
                                                &nbsp;
                                                <asp:Label ID="Label3" runat="server" Text='<%# Eval("productName") %>' Font-Size="Smaller"></asp:Label></td>
                                        </tr>
                                        <tr>
                                            <td style="width: 99px; height: 4px;" colspan="1" align="center">
                                                <table border="0" cellpadding="0" cellspacing="0" style="width:145px;font-size:12pt;color:black;font-style:normal;font-family:宋体;text-align:center; height:17px;" id="table2">
                                                    <tr>
                                                        <td style="width: 291px; font-size: 12pt; color:black;font-style:normal;font-family:宋体;"
                                                            align="right" colspan="1">
                                                            <asp:Label ID="Label4" runat="server"  Text="市场价:" style="display:inline-block;font-size:Smaller;height:11px;"></asp:Label></td>
                                                        <td style="font-size: 12pt; color: black; font-family: 宋体; width: 82px;" align="left">
                                                            <asp:Label ID="Label1" runat="server" Text='<%# Eval("productUnitPrice") %>' Font-Size="Smaller"
                                                                Height="11px" Width="1px"></asp:Label></td>
                                                        <td style="width: 100px; height: 19px;" align="right" colspan="3">
                                                            &nbsp;<img src="../Images/logo/but_bbogi.gif" alt="查看详细"style="cursor:hand;" onclick="javascript:window.location='ShowShop.aspx?pid=<%#Eval("productID") %>';" id="IMG1"/>
                                                            <%--<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/Images/logo/but_bbogi.gif"
                                                                CommandName="bu1" />--%></td>
                                                    </tr>
                                                    <tr>
                                                        <td style="width: 291px; height: 18px; font-size: 12pt; color: black; font-family: 宋体;
                                                            font-weight: bolder;" align="right">
                                                            <asp:Label ID="Label5" runat="server" Font-Size="Smaller" Text="会员价:"></asp:Label></td>
                                                        <td style="height: 18px; font-size: 12pt; color:Olive; font-family: 宋体; width: 82px;
                                                            font-weight: bold;" align="left">
                                                            <asp:Label ID="Label2" runat="server" Text='<%# Eval("productUnitPrice") %>' Font-Size="Smaller"
                                                                Width="1px" style="color: #ff0000"></asp:Label></td>
                                                        <td style="width: 95px; height: 18px" align="right">
                                                            <img src="../Images/logo/but_bbogi2.gif" alt="查看详细"style="cursor:hand;" onclick="javascript:window.location.replace('BuyThing.aspx?pid=<%#Eval("productID") %>');"/>
                                                            <%--<asp:ImageButton ID="ImageButton2" CommandName="bu2" ImageUrl="~/Images/logo/but_bbogi2.gif"
                                                                runat="server" OnClientClick='javascript:window.location=\"BuyThing.aspx?pid=\" +<%#Eval("productID") %>;' />--%></td>
                                                    </tr>
                                                </table>
                                            </td>
                                        </tr>
                                    </table>
                                </ItemTemplate>
                            </asp:DataList>

#13


RepeatColumns="4" 设置了后就会改成所有的 table 横着显示了 

#14


上例中,你只要做一个table显示模板,他就会根据你设定的RepeatColumns="4"而每行显示4列,数据超过4个则换行,你要做的就是指定每行显示列数,显示模板,绑定数据。成功了我等接分

#15


毒辣点,参数改成1,不知道是不是你想要的效果!

#16


想的美 我做的效果和你说的不一样 你那样做的话 我的datalist的显示的数据就都成了横着的了
我要的是数据时竖着的 
你的那种做法 我早就知道了 
只是一直没想起来,其实只要我在sql存储过程语句给个限定就行了 
不过 还是要谢谢你们的提议 。。。。。

#1


效果图

#2


<datelist>
<ItemTemplate >
  <table></table>
  <table></table>
  <table></table>
</ItemTemplate >
</datelist>

#3


贴点带数据的效果图

#4


<asp:DataList ID="dlSueByToday" runat="server" Height="90px" Width="518px"  RepeatDirection ="Vertical" >
    <HeaderTemplate >
        <h1 ><%= string .Format ("{0:yyyy-MM-dd}",DateTime.Now ) %> 旅行日 </h1> 
    </HeaderTemplate>
        <ItemTemplate>
        <li>
            <table style="border-bottom:#bbbbbb 1px dashed ; widows :518px;  border-bottom-style:dashed" >
                <tr>
                    <td rowspan ="3" width="102" height="102">
                       <img src ="../images/img02.jpg" alt ="" border="0" />
                    </td>
                    <td>
                        <h3><a class="link-pink" href="#" title="目的地指南"><%# GetTop(Eval("typeId"))%></a> </h3>
                    </td>
                </tr>
                <tr>
                    <td >
                     <a href ="#"><span class="span-green" ><%# Eval("Title")%> </span></a>- <a href ="#"><span class="span-yellow"><%# Eval("User.NickName")%> </span> </a>
                    </td>
                </tr>
                <tr>
                    <td style="line-height: 20px">
                      <%# Eval("Content")%>
  
                    </td>
                </tr>
                <tr>
                    <td  colspan ="2">
                     <div class="more"><a class="link-pink" href="#" title="阅读更多">阅读全部&raquo;</a></div>
                    </td>
                </tr>
            </table></li>
           
  
            <br />
        </ItemTemplate>
  </asp:DataList>

#5


引用 2 楼 mmerry 的回复:
<datelist>
<ItemTemplate >
  <table> </table>
  <table> </table>
  <table> </table>
</ItemTemplate >
</datelist>

你这样的table不就是竖着叠放的吗?

#6


是啊 我知道啊 默认的也就是竖着的啊 可是我现在要限制 table 显示的数量 

#7


你的数据是什么样的

#8


数据? 就是查了数据库中所有的数据 是个list 了  
哦 你的意思是我查数据的时候给他直接限制了 select top 5 * from table 是不是这样啊? 

#9


引用 6 楼 mmerry 的回复:
是啊 我知道啊 默认的也就是竖着的啊 可是我现在要限制 table 显示的数量

将table设置为runat="server" 并设置id,如:
<table id="t1" runat="server">...</table>
<table id="t2" runat="server">...</table>
<table id="t3" runat="server">...</table>

后台根据条件显示或隐藏某些table,大致代码,如在ItemDataBound事件中的代码:
    protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        HtmlTable tab1 = (HtmlTable)e.Item.FindControl("t1");
        HtmlTable tab2 = (HtmlTable)e.Item.FindControl("t2");
        HtmlTable tab3 = (HtmlTable)e.Item.FindControl("t3");

        if(某个条件1)
            tab1.Attributes.Add("style", "display:none");//隐藏t1

        if(某个条件2)
            tab1.Attributes.Add("style", "display:block");//显示t2
        
        ...........
    }

当然先要using一下:
using System.Web.UI.HtmlControls;

#10


被你们整晕了,很简单的,他要实现的是像网上商城那种,一行显示几个商品,
<asp:DataList ID="DataList1" runat="server" RepeatColumns="4" DataKeyField="productID"
                                OnItemDataBound="DataList1_ItemDataBound" OnItemCommand="DataList1_ItemCommand">
                                <ItemTemplate>
                                   <!-- 在这里构造你想要的任何显示方式-->


RepeatColumns="4" 指定一行有几列 DataKeyField不用说了吧,然后不要忘了绑定,祝贺成功!


#11


我的意思 是你提供一些测试数据 和你想要的效果图

#12


更全的源码,你照着改改就知道原理了,看见table就改,然后改下 DataKeyField="你的绑定ID"
<asp:DataList ID="DataList1" runat="server" RepeatColumns="4" DataKeyField="productID"
                                OnItemDataBound="DataList1_ItemDataBound" OnItemCommand="DataList1_ItemCommand">
                                <ItemTemplate>
                                    <table border="0" cellpadding="0" cellspacing="0" style="width: 157px; height: 1px">
                                        <tr>
                                            <td style="width: 99px; height: 131px;" align="center">
                                               <a href='ShowShop.aspx?pid=<%# Eval("productID") %>'> <asp:Image ID="Image1" runat="server" ImageUrl='<%#"~/Images/SmallImages/"+Eval("productImg")%>' /></a></td>
                                        </tr>
                                        <tr>
                                            <td style="width: 99px; height: 18px; font-size: 12pt; color: black; font-family: 宋体;"
                                                align="right">
                                                &nbsp;
                                                <asp:Label ID="Label3" runat="server" Text='<%# Eval("productName") %>' Font-Size="Smaller"></asp:Label></td>
                                        </tr>
                                        <tr>
                                            <td style="width: 99px; height: 4px;" colspan="1" align="center">
                                                <table border="0" cellpadding="0" cellspacing="0" style="width:145px;font-size:12pt;color:black;font-style:normal;font-family:宋体;text-align:center; height:17px;" id="table2">
                                                    <tr>
                                                        <td style="width: 291px; font-size: 12pt; color:black;font-style:normal;font-family:宋体;"
                                                            align="right" colspan="1">
                                                            <asp:Label ID="Label4" runat="server"  Text="市场价:" style="display:inline-block;font-size:Smaller;height:11px;"></asp:Label></td>
                                                        <td style="font-size: 12pt; color: black; font-family: 宋体; width: 82px;" align="left">
                                                            <asp:Label ID="Label1" runat="server" Text='<%# Eval("productUnitPrice") %>' Font-Size="Smaller"
                                                                Height="11px" Width="1px"></asp:Label></td>
                                                        <td style="width: 100px; height: 19px;" align="right" colspan="3">
                                                            &nbsp;<img src="../Images/logo/but_bbogi.gif" alt="查看详细"style="cursor:hand;" onclick="javascript:window.location='ShowShop.aspx?pid=<%#Eval("productID") %>';" id="IMG1"/>
                                                            <%--<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/Images/logo/but_bbogi.gif"
                                                                CommandName="bu1" />--%></td>
                                                    </tr>
                                                    <tr>
                                                        <td style="width: 291px; height: 18px; font-size: 12pt; color: black; font-family: 宋体;
                                                            font-weight: bolder;" align="right">
                                                            <asp:Label ID="Label5" runat="server" Font-Size="Smaller" Text="会员价:"></asp:Label></td>
                                                        <td style="height: 18px; font-size: 12pt; color:Olive; font-family: 宋体; width: 82px;
                                                            font-weight: bold;" align="left">
                                                            <asp:Label ID="Label2" runat="server" Text='<%# Eval("productUnitPrice") %>' Font-Size="Smaller"
                                                                Width="1px" style="color: #ff0000"></asp:Label></td>
                                                        <td style="width: 95px; height: 18px" align="right">
                                                            <img src="../Images/logo/but_bbogi2.gif" alt="查看详细"style="cursor:hand;" onclick="javascript:window.location.replace('BuyThing.aspx?pid=<%#Eval("productID") %>');"/>
                                                            <%--<asp:ImageButton ID="ImageButton2" CommandName="bu2" ImageUrl="~/Images/logo/but_bbogi2.gif"
                                                                runat="server" OnClientClick='javascript:window.location=\"BuyThing.aspx?pid=\" +<%#Eval("productID") %>;' />--%></td>
                                                    </tr>
                                                </table>
                                            </td>
                                        </tr>
                                    </table>
                                </ItemTemplate>
                            </asp:DataList>

#13


RepeatColumns="4" 设置了后就会改成所有的 table 横着显示了 

#14


上例中,你只要做一个table显示模板,他就会根据你设定的RepeatColumns="4"而每行显示4列,数据超过4个则换行,你要做的就是指定每行显示列数,显示模板,绑定数据。成功了我等接分

#15


毒辣点,参数改成1,不知道是不是你想要的效果!

#16


想的美 我做的效果和你说的不一样 你那样做的话 我的datalist的显示的数据就都成了横着的了
我要的是数据时竖着的 
你的那种做法 我早就知道了 
只是一直没想起来,其实只要我在sql存储过程语句给个限定就行了 
不过 还是要谢谢你们的提议 。。。。。