DataGrid等数据控件的用法(4)

利用ASP.NET DataGrid显示主次关系的数据
在写论坛的过程中,因为在显示各个版区的时候需要显示主次关系的数据,于是嵌套了DataGrid,其他的数据控件应也适用。
aspx页面
<asp:datalist id="DataList1" runat="server">
                     <ItemTemplate>
                        <FONT face="宋体">
                           <table width="100%" border="0">
                              <tr>
                                 <td align="left">* <A href='bigclass.aspx?typeid=<%# DataBinder.Eval(Container.DataItem, "typeid") %>&bigclassid=<%# DataBinder.Eval(Container.DataItem, "bigclassid") %>' target=_self class="top"><%# DataBinder.Eval(Container.DataItem, "bigclassname") %></A></td>
                              </tr>
                              <tr>
                                 <td>
                                    <asp:DataList runat="server" Id="ChildDataList" GridLines="None" Bordercolor="black" cellpadding="3" cellspacing="0" Headerstyle-BackColor="#8080C0" Headerstyle-Font-Name="Arial" Headerstyle-Font-Size="8" Font-Name="Arial" Font-Size="8" datasource=' <%# DataBinder.Eval(Container.DataItem, "myrelation") %>' RepeatColumns="2">
                                       <ItemTemplate>
                                              <A href='smallclass.aspx?typeid=<%# DataBinder.Eval(Container.DataItem, "typeid") %>&bigclassid=<%# DataBinder.Eval(Container.DataItem, "bigclassid") %>&smallclassid=<%# DataBinder.Eval(Container.DataItem, "smallclassid") %>' target=_self class="top">
                                             <%# DataBinder.Eval(Container.DataItem, "smallclassname") %>
                                          </A>
                                       </ItemTemplate>
                                    </asp:DataList>
                                 </td>
                              </tr>
                           </table>
                        </FONT>
                     </ItemTemplate>
                  </asp:datalist>

aspx.cs里写:
string ctypeid=Request["typeid"];   
            if (ctypeid==null)
            {ctypeid="1";}
            int typeid=Int32.Parse(ctypeid);   
            SqlConnection myConnection=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["conn"]);
            myConnection.Open();
            SqlDataAdapter cmd1 = new SqlDataAdapter("select * from px_bigclass where typeid="+typeid,myConnection);

            //Create and fill the DataSet.
            DataSet ds = new DataSet();
            cmd1.Fill(ds,"px_bigclass");

            //Create a second DataAdapter for the Titles table.
            SqlDataAdapter cmd2 = new SqlDataAdapter("select * from px_smallclass where typeid="+typeid,myConnection);
            cmd2.Fill(ds,"px_smallclass");

            //Create the relation bewtween the Authors and Titles tables.
            ds.Relations.Add("myrelation",ds.Tables["px_bigclass"].Columns["bigclassid"], ds.Tables["px_smallclass"].Columns["bigclassid"]);

            //Bind the Authors table to the parent Repeater control, and call DataBind.
            DataList1.DataSource = ds.Tables["px_bigclass"].DefaultView;
            DataBind();

            //Close the connection.
            myConnection.Close();   


另一种做法,将子DataGrid写成一个用户控件,然后再父DataGrid中的模板列中包含用户控件

参考
http://dotnet.aspx.cc/ShowDetail.aspx?id=149E5DD7-3B32-461e-ACC6-51D1652E6746
posted @ 2004-04-25 17:51  9527的晃悠人生  阅读(1153)  评论(0编辑  收藏  举报