ASP.NET(vb.net) & DataList - Visual Studio 2005,2008,2010 (Fx 2.0,3.5,4.0) Example scripts how to use DataList control in asp.net ,Development and sample scripts on Visual Studio 2005,2008,2010 (FX 2.0,3.5,4.0)
ShotDev Focus:
- ASP.NET(vb.net) & DataList - Visual Studio 2005,2008,2010 (Fx 2.0,3.5,4.0)
Example
DataList1.aspx
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="DataList1.aspx.vb" Inherits="DataList1" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>ShotDev.Com Tutorial</title> </head> <body> <form id="form1" runat="server"> <asp:DataList id="myDataList" runat="server" RepeatColumns="2"> <HeaderTemplate> <b>My Category</b><br/> </HeaderTemplate> <ItemTemplate> <div style="width:100px" align="center"> <asp:Image id="imgPicture" runat="server"></asp:Image> <br /> <asp:HyperLink id="hplCategory" runat="server"></asp:HyperLink> </div> </ItemTemplate> </asp:DataList> </form> </body> </html>
DataList1.aspx.vb
Imports System.Data
Imports System.Data.OleDb
Partial Class DataList1
Inherits System.Web.UI.Page
Dim objConn As OleDbConnection
Dim objCmd As OleDbCommand
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim strConnString As String
strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
Server.MapPath("database/mydatabase.mdb") & ";"
objConn = New OleDbConnection(strConnString)
objConn.Open()
BindData()
End Sub
Protected Sub BindData()
Dim strSQL As String
strSQL = "SELECT * FROM category"
Dim dtReader As OleDbDataReader
objCmd = New OleDbCommand(strSQL, objConn)
dtReader = objCmd.ExecuteReader()
'*** BindData to DataList ***'
myDataList.DataSource = dtReader
myDataList.DataBind()
dtReader.Close()
dtReader = Nothing
End Sub
Protected Sub Page_Unload(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Unload
objConn.Close()
objConn = Nothing
End Sub
Protected Sub myDataList_ItemDataBound(ByVal sender As Object, ByVal e As DataListItemEventArgs) Handles myDataList.ItemDataBound
'*** Image ***'
Dim img As Image = CType(e.Item.FindControl("imgPicture"), Image)
If Not IsNothing(img) Then
img.ImageUrl = "images/" & e.Item.DataItem("Picture")
'img.Attributes.Add("OnClick","window.location='http://www.shotdev.com?CateID="&e.Item.DataItem("CategoryID")&"'")
'img.Style.Add("cursor","hand")
End If
'*** HyperLink ***'
Dim hplCate As HyperLink = CType(e.Item.FindControl("hplCategory"), HyperLink)
If Not IsNothing(hplCate) Then
hplCate.Text = e.Item.DataItem("CategoryName")
hplCate.ToolTip = e.Item.DataItem("CategoryName")
hplCate.NavigateUrl = "http://www.shotdev.com?CateID=" & e.Item.DataItem("CategoryID")
End If
End Sub
End Class
Create a asp.net file and save to path root-path/dotnet/
Run
http://localhost/dotnet/DataList1.aspx
Screenshot

