当前位置:首页 >设计资讯> 正文

vb.net DataList循环中套循环

文章来源: 发布时间: 2017-12-24

代码部份:
Sub Page_Load(Src As Object, E As EventArgs)
  If Not IsPostBack Then
  Dim str As String = "select * from 表" 
  mynews.DataSource = dbopen(Str)
  mynews.DataBind() 
  End If
End Sub
’读取子栏目
Sub Item_Bound(ByVal sender As Object, ByVal e As DataListItemEventArgs)
     Dim str2 As String = "select *  from 表 where 关联字段=" & e.Item.DataItem("web_id") & ""
     Dim xDataList As DataList = CType(e.Item.FindControl("mynews2"), DataList)
     xDataList.DataSource = dbopen(str2)
     xDataList.DataBind()
End Sub
function dbopen(str as string)
 dim myvar as string=configurationsettings.appsettings("connstring") 
 dim conn as oledbconnection=new oledbconnection("provider=microsoft.jet.oledb.4.0;data source="&server.MapPath("admin/"&myvar))
 dim comm as oledbdataAdapter=new oledbdataAdapter(str,conn)
 dim ds as new dataset
 comm.fill(ds,"biao")
 dbopen = ds.Tables("biao").DefaultView
end function
控件应用部份:
<asp:DataList ID="mynews" RepeatDirection="Vertical" Width="100%" runat="server" DataKeyField="web_id" OnItemDataBound="Item_Bound">
 <itemtemplate>
  <%# container.dataitem("字段")%>
    <asp:DataList ID="mynews2" Width="98%" RepeatDirection="Vertical" runat="server">
              <itemtemplate>
                <%# container.dataitem("字段")%>
              </itemtemplate>
 </asp:DataList>
 </itemtemplate>
</asp:DataList>