首页 新闻 赞助 找找看

datalist中嵌套DetailsView DetailsView 数据只能出来一行

0
悬赏园豆:10 [已解决问题] 解决于 2012-05-17 09:40
前台
  1 <%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Content.master.cs" Inherits="MyBBS.Controls.Content" %>
  2 
  3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4 
  5 <html xmlns="http://www.w3.org/1999/xhtml">
  6 <head runat="server">
  7     <title></title>
  8     <asp:ContentPlaceHolder ID="head" runat="server">
  9     </asp:ContentPlaceHolder>
 10     <style type="text/css">
 11         .style1
 12         {
 13             width: 12px;
 14             height: 100%;
 15             float: right;
 16             vertical-align:bottom;
 17             
 18         }
 19         .styleLabelSectionName
 20         {
 21            text-align: left;
 22            
 23         }
 24         .style2
 25         {
 26             height: 50px;
 27         }
 28         .style3
 29         {
 30             height: 20px;
 31             width:20%;
 32             text-align: left;
 33         }
 34     </style>
 35 </head>
 36 <body style="height: 111px">
 37     <form id="form1" runat="server">
 38         <div>
 39         </div>
 40         <div id="Module" align="center">
 41             <asp:DataList ID="DataListSection" runat="server" 
 42                 onitemdatabound="DataListSection_ItemDataBound">
 43                 <ItemTemplate>
 44                     <table style="width:100%;">
 45                         <tr>
 46                             <td bgcolor="#CCCCFF" >
 47                                 <div style="width:820px; height:30px">
 48                                 <table style="width:100%;">
 49                                 
 50                        <tr>
 51                            <td style="text-align: left">
 52                                     &nbsp;<asp:Label ID="LabelSectionName" runat="server" class="styleLabelSectionName" 
 53                                         Font-Size="X-Large" Text='<%# Eval("SectionName") %>'></asp:Label>
 54                               </td>
 55                                     <td style="text-align: right">
 56                                         <img alt="闭合\展开" class="style1" src="../Images/collapsed_no.gif" />
 57                                         </td>
 58                                             </tr>
 59                                          </table>
 60                                 </div>
 61                             </td>
 62                         </tr>
 63                         <tr>
 64                             <td class="style2">
 65                                 <asp:DetailsView ID="DetailsViewSection" runat="server" Height="50px" Width="820px" 
 66                                     AutoGenerateRows="False">
 67                                     <Fields>
 68                                         <asp:TemplateField ShowHeader="False">
 69                                             <ItemTemplate>
 70                                               
 71                                                 <table style="width:100%;">
 72                                                     <tr>
 73                                                         <td rowspan="4" style="width:5%">
 74                                                             <div>
 75                                                                 <asp:Image ID="Image1" runat="server" 
 76                                                                     ImageUrl="~/Images/topicicon_default.png" />
 77                                                             </div>
 78                                                         </td>
 79                                                         <td class="style3">
 80                                                             <asp:HyperLink ID="HyperLinkSection" runat="server" 
 81                                                                 Text='<%# Eval("SecondSectionName") %>'></asp:HyperLink>
 82                                                         </td>
 83                                                         <td class="style3">
 84                                                         </td>
 85                                                         <td class="style3">
 86                                                         </td>
 87                                                     </tr>
 88                                                     <tr>
 89                                                         <td rowspan="2" style="height:40px; text-align: left;">
 90                                                             <asp:Label ID="Label1" runat="server" Text='<%# Eval("Introduction") %>'></asp:Label>
 91                                                         </td>
 92                                                         <td rowspan="2">
 93                                                             <asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>
 94                                                             /<asp:Label ID="Label4" runat="server" Text="Label"></asp:Label>
 95                                                         </td>
 96                                                         <td style="text-align: left">
 97                                                             <asp:HyperLink ID="HyperLinkPoster" runat="server">最新帖子</asp:HyperLink>
 98                                                             <br />
 99                                                         </td>
100                                                     </tr>
101                                                     <tr>
102                                                         <td style="text-align: left">
103                                                             &nbsp;<asp:HyperLink ID="HyperLink1" runat="server">HyperLink</asp:HyperLink>
104                                                             &nbsp;更新于 
105                                                             <asp:Label ID="LabelTime" runat="server" Text="发表时间"></asp:Label>
106                                                         </td>
107                                                     </tr>
108                                                     <tr>
109                                                         <td style="text-align: left">
110                                                             版主:<asp:Label ID="LabelModerator" runat="server" 
111                                                                 Text='<%# Eval("Moderator") %>'></asp:Label>
112                                                         </td>
113                                                         <td>
114                                                             &nbsp;</td>
115                                                         <td>
116                                                         </td>
117                                                     </tr>
118                                                 </table>
119                                               
120                                             </ItemTemplate>
121                                             <ItemStyle BorderStyle="None" />
122                                         </asp:TemplateField>
123                                     </Fields>
124                                 </asp:DetailsView>
125                             </td>
126                         </tr>
127                     </table>
128                 </ItemTemplate>
129             </asp:DataList>
130         </div>
131         </form>
132 </body>
133 </html>
后台
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 using System.Web.UI;
 6 using System.Web.UI.WebControls;
 7 using System.Data;
 8 
 9 namespace MyBBS.Controls
10 {
11     public partial class Content : System.Web.UI.MasterPage
12     {
13         protected void Page_Load(object sender, EventArgs e)
14         {
15            if (!IsPostBack)
16            {
17                 SectionBind();      
18             }
19     
20         }
21         private void SectionBind()
22         {
23             BBS.DAL.Section Section = new BBS.DAL.Section();
24             DataListSection.DataSource = Section.GetSectionName().Tables[0];
25             DataListSection.DataBind();   
26         }
27 
28         protected void DataListSection_ItemDataBound(object sender, DataListItemEventArgs e)
29         {
30             BBS.DAL.Section Section = new BBS.DAL.Section();
31             DataSet dsSecondSecionInfo = Section.SecondSectionInfo(((Label)e.Item.FindControl("LabelSectionName")).Text.ToString());
32             DetailsView dvSecondSection = e.Item.FindControl("DetailsViewSection") as DetailsView;
33             dvSecondSection.DataSource = dsSecondSecionInfo.Tables[0];
34             dvSecondSection.DataBind();      
35         }
36     }
37 }
BBS.DLL.Section
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Data.SqlClient;
 6 using System.Data;
 7 
 8 namespace BBS.DAL
 9 {
10     public class Section:Connection
11     {
12       
13         /// <summary>
14         /// 获取版块名称
15         /// </summary>
16         /// <returns>版块名称集合</returns>
17         public DataSet GetSectionName()
18         {
19             SqlConnection conn = getConn();
20             SqlDataAdapter AdapterSectionName = new SqlDataAdapter("select SectionName from [Section]", conn);
21             SqlCommandBuilder thisBuilder = new SqlCommandBuilder(AdapterSectionName);
22             DataSet thisDataSet = new DataSet();
23             AdapterSectionName.Fill(thisDataSet, "[Section]");
24             return thisDataSet;
25         }
26         /// <summary>
27         /// 获取子版块名称
28         /// </summary>
29         /// <param name="sectionName">主版块名</param>
30         /// <returns>子版块名称集合</returns>
31         public DataSet SecondSectionInfo(string sectionName)
32         {
33             SqlConnection conn = getConn();
34             SqlDataAdapter AdapterSectionName = new SqlDataAdapter("select * from [SecondSection] where Section = @sectionname", conn);
35             AdapterSectionName.SelectCommand.Parameters.Add("sectionname", SqlDbType.Char).Value = sectionName;
36             DataSet thisDataSet = new DataSet();
37             AdapterSectionName.Fill(thisDataSet, "[SecondSection]");
38             return thisDataSet;  
39         }
40 
41     
42     }
43 }

 

弄个BBS  datalist做 主版块  嵌套的DetailsView做分版块  可是当某个主版块里有2个或以上分版块时 只显示一个分版块  部分代码已贴出

雪槐1124的主页 雪槐1124 | 初学一级 | 园豆:195
提问于:2012-05-16 20:22
< >
分享
最佳答案
0

你是希望两个板块的子信息都显示吗?我看你的代码是已经实现了的。

收获园豆:10
无之无 | 大侠五级 |园豆:5095 | 2012-05-16 20:36

不是的,  我是要用DetailsView显示每个板块的所有子板块 可是运行下来  只能显示一个

雪槐1124 | 园豆:195 (初学一级) | 2012-05-16 20:55

@雪槐1124: 你的程序代码应该是正确的,没调试,不能确认,但至少就表面看来,没发现问题。

无之无 | 园豆:5095 (大侠五级) | 2012-05-16 22:40

@笨笨蜗牛: 调试结果应该是 版块1下有分版1 分版2        版块2下有分版3  分版4  可现在出来是这样的

 

雪槐1124 | 园豆:195 (初学一级) | 2012-05-17 09:06

@雪槐1124: 我明白你的意思了。你的这个错误是说每个子视图里只正确的显示了一个数据内容,而你的定义里却是有多个吧?

你在这里不应该使用DetailView这个对象,而应该继续嵌套一个DataList或别的类似的控件(如Repeater\DataGrid\DataGridView)等,因为DetailView是不存在重复这个功能的(我没用过估计是这样的)。

无之无 | 园豆:5095 (大侠五级) | 2012-05-17 09:18

@笨笨蜗牛: OK了,谢谢啊 ~ 自己把自己绕进去了 呵呵

雪槐1124 | 园豆:195 (初学一级) | 2012-05-17 09:39

@雪槐1124: 不客气。另外,给你个建议,你的取数操作可以再次优化,就是当你取一级板块的时候,就同时把二级板块数据也取出来,这样,就不需要多次的连接数据库进行查询了,性能会提高很多。

无之无 | 园豆:5095 (大侠五级) | 2012-05-17 09:48

@笨笨蜗牛: 暂时还没实现的思路   我是分在2个表中的啊  一级版块一个表   二级版块一个表    不好弄

雪槐1124 | 园豆:195 (初学一级) | 2012-05-18 14:52

@雪槐1124: 两个表也可以一次性把数据读取进来的,或者在第一次读取的时候读取进来。这样会性能好点。不过,如果压力不是很大的话也不是什么大问题。

无之无 | 园豆:5095 (大侠五级) | 2012-05-18 14:54

@笨笨蜗牛: 有个思路:取2个表数据放在一个dataset里的2个表里  然后添加relations?   可读表的时候要2次用Adapter然后Fill啊?  还是2次数据连接。    怎么一次性读2个表呢?

雪槐1124 | 园豆:195 (初学一级) | 2012-05-18 15:43

@雪槐1124: 当第一次使用二级板块数据表的时候就查询所有的二级板块数据出来。

每次使用的时候,可以通过建立一个视图(DATAVIEW)来实现,DATAVIEW有ROWFILTER的属性过滤数据。

无之无 | 园豆:5095 (大侠五级) | 2012-05-18 15:51

@笨笨蜗牛: 取二级版块在ItemDataBound事件中。。。。。   “第一次使用二级板块数据表的时候就查询所有的二级板块数据出来”    这个事件放在哪啊。。。。。 -

雪槐1124 | 园豆:195 (初学一级) | 2012-05-18 16:21

@雪槐1124: 

在类里定义一个成员变量DataSet,首先把所有的一级板块放进去,当一级板块绑定需要使用二级板块的时候,检查这个二级板块的数据是否有,没有就调用查询语句获取所有的二级板块数据。

说白了,就是使用Lazy加载数据的方式。当然,你也可以在一开始就把这个数据加载好。

无之无 | 园豆:5095 (大侠五级) | 2012-05-18 16:46

@笨笨蜗牛: 通过一个对2级版块的数据是否存在判定   实现在第一次使用二级版块的时候查询所有的2级版块出来 放在dataset中  是这个意思吧  我回去试试  太谢谢你了 呵呵~

雪槐1124 | 园豆:195 (初学一级) | 2012-05-18 17:14

@笨笨蜗牛: 能给我一条鱼么  我自己研究下怎么弄的  搞不出 上面2个事件  一个在Page_Load事件里 一个在ItemDataBound  o(︶︿︶)o 唉。。。。。。

雪槐1124 | 园豆:195 (初学一级) | 2012-05-18 23:37

@雪槐1124: 

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Web;
 using System.Web.UI;
 using System.Web.UI.WebControls;
 using System.Data;
 
 namespace MyBBS.Controls
 {
     public partial class Content : System.Web.UI.MasterPage
     {
         protected void Page_Load(object sender, EventArgs e)

         {
            if (!IsPostBack)
            {
                 SectionBind();      
             }
     
         }
         DataSet _data;
         private void SectionBind()
         {
             BBS.DAL.Section Section = new BBS.DAL.Section();
             _data = Section.GetSectionName();
             DataListSection.DataSource = _data.Tables[0];
             DataListSection.DataBind();   
         }
 
         protected void DataListSection_ItemDataBound(object sender, DataListItemEventArgs e)
         {
             if(_data.Tables.Length == 1)
             {
                 BBS.DAL.Section Section = new BBS.DAL.Section();
                 DataSet dsSecondSecionInfo = Section.SecondSectionInfo();
             }
             DetailsView dvSecondSection = e.Item.FindControl("DetailsViewSection") as DetailsView;
             dvSecondSection.DataSource = new DataView(_data.Tables[1]).RowFilter("Session='" + Label)e.Item.FindControl("LabelSectionName")).Text + "'");
//这里可能有错误,你检查下
dvSecondSection.DataBind(); } } }
        public DataSet SecondSectionInfo()
         {
             SqlConnection conn = getConn();
             SqlDataAdapter AdapterSectionName = new SqlDataAdapter("select * from [SecondSection]", conn);
             DataSet thisDataSet = new DataSet();
             AdapterSectionName.Fill(thisDataSet, "[SecondSection]");
             return thisDataSet;  
         }

差不多就这个样子。

无之无 | 园豆:5095 (大侠五级) | 2012-05-19 08:14

@笨笨蜗牛: 

   /// thisGV.DataSource = new DataView(_data.Tables[1]).RowFilter = string.Format("Section='{0}'", ((Label)e.Item.FindControl("LabelSectionName")).Text);
           DataView newview = new DataView();
           newview.Table = _data.Tables[1];
          newview.RowFilter = string.Format("Section='{0}'", ((Label)e.Item.FindControl("LabelSectionName")).Text);
          thisGV.DataSource = newview;
          thisGV.DataBind();

2种方式 都是显示  

未将对象引用设置到对象的实例。费解了

雪槐1124 | 园豆:195 (初学一级) | 2012-05-20 00:09
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册