Private Sub Page_Load(sender As Object, e As System.EventArgs)
’ Put user code to initialize the page here
’ Createand populate a multi-dimensional array
Dim MyArray(4) AsInteger()
Dim i AsIntegerFor i = 0To3
MyArray(i) = NewInteger(5) {}
Dim x AsIntegerFor x = 0To4
MyArray(i)(x) = x + 5 * i
NextNext
DataList1.DataSource = MyArray
DataList1.DataBind()
End Sub ’Page_Load
C#
privatevoidPage_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here// Create and populate a multi-dimensional arrayint [][] MyArray = newint[4][];
for (int i=0; i<4; i++)
{
MyArray[i] = newint[5];
for (int x=0; x<5; x++)
{
MyArray[i][x] = x+(5*i);
}
}
DataList1.DataSource = MyArray;
DataList1.DataBind();
}
<asp:labelid='lblHeader'runat='server'></asp:label><br><b>Get Information on Directory:</b><br><p><asp:textboxid='txtPath'runat='server'></asp:textbox><p></p><asp:buttonid='btnSubmit'runat='server'text='Go!'type='Submit'></asp:button><p></p><divstyle='width:100%; height:200; overflow:auto;'><asp:datalistid='DataList1'runat='server'DataKeyField='FullName'OnSelectedIndexChanged='SelectedIndexChanged'><ItemTemplate><li><%#DataBinder.Eval(Container.DataItem, 'Name') %><br><fontsize='-1'>[
<asp:linkbuttonText='View Contents'CommandName='Select'runat='server'ID='Linkbutton1' />]
| [
<%#DataBinder.Eval(Container.DataItem, 'Length') %>
bytes] </font><p></ItemTemplate></asp:datalist></div><p><hr></p><asp:labelid='lblFileContents'runat='server'></asp:label>
VB.NET
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
’Put user code to initialize the page here
If Request('txtPath') <> ''Then
Dim strDir AsString = Request('txtPath')
lblHeader.Text = 'File Listing for ' & strDir & ''
Dim dirInfo AsNew DirectoryInfo(strDir)
’ Get the files for the directory strDir
Dim fInfos As FileInfo() = dirInfo.GetFiles('*.*')
DataList1.DataSource = fInfos
DataList1.DataBind()
EndIfEnd Sub
Protected Sub SelectedIndexChanged(ByVal sender AsObject, ByVal e As System.EventArgs) Handles DataList1.SelectedIndexChanged
Dim strFilePath AsString = DataList1.DataKeys(DataList1.SelectedItem.ItemIndex).ToString()
Dim fInfo As FileInfo = New FileInfo(strFilePath)
Dim objStream As StreamReader = fInfo.OpenText()
Dim strContents AsString = objStream.ReadToEnd()
objStream.Close()
lblFileContents.Text = 'Contents of ' & fInfo.Name & ':' & _
'' & vbCrLf & strContents & vbCrLf & ''End Sub
C#
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page hereif (Request['txtPath']!= null)
{
string strDir = Request['txtPath'];
lblHeader.Text = 'File Listing for ' + strDir + '';
DirectoryInfo dirInfo =new DirectoryInfo(strDir);
// Get the files for the directory strDir
FileInfo[] fInfos = dirInfo.GetFiles('*.*');
DataList1.DataSource = fInfos;
DataList1.DataBind();
}
}
protected void SelectedIndexChanged(Object sender , System.EventArgs e )
{
string strFilePath = DataList1.DataKeys[(int)DataList1.SelectedItem.ItemIndex].ToString();
FileInfo fInfo = new FileInfo(strFilePath);
StreamReader objStream = fInfo.OpenText();
string strContents = objStream.ReadToEnd();
objStream.Close();
lblFileContents.Text = 'Contents of ' + fInfo.Name + ':' + '' + '
' + strContents + '
' + '';
}
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
’Put user code to initialize the page here
DataList1.DataSource = System.Environment.GetEnvironmentVariables()
DataList1.DataBind()
End Sub
privatevoidPage_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
DataList1.DataSource = System.Environment.GetEnvironmentVariables();
DataList1.DataBind();
}
Public Class DatalistLabelColumn
Implements ITemplate
Public Sub New()
End Sub ’NewPublic Sub InstantiateIn(ByVal containerAs Control) Implements System.Web.UI.ITemplate.InstantiateIn
Dim label1 AsNew Label
AddHandler label1.DataBinding, AddressOf Me.BindLabelColumn
container.Controls.Add(label1)
End Sub ’InstantiateIn
Public Sub BindLabelColumn(ByVal sender AsObject, ByVal e As EventArgs)
Dim lbl As Label = CType(sender, Label)
Dim containerAs DataListItem = CType(lbl.NamingContainer, DataListItem)
Dim strVals As [String] = Convert.ToString(DataBinder.Eval(CType(container, DataListItem).DataItem, 'LastName')) + ', ' + Convert.ToString(DataBinder.Eval(CType(container, DataListItem).DataItem, 'FirstName'))
lbl.Text = strVals
End Sub ’BindLabelColumn
EndClass ’DatalistLabelColumn
Dim ds As DataSet = ’Fill the dataset
DataList1.ItemTemplate = New DatalistLabelColumn
DataList1.DataSource = ds
DataList1.DataBind()
Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
IfNot Page.IsPostBack Then
BindTitle()
EndIfEnd Sub
’Bind Datato DataList Populating the Dataset
Sub BindTitle()
Dim ds AsNew DataSet
Dim sqlStmt AsString = 'SELECT * FROM Employees order by title'
Dim conString AsString = 'server=localhost;database=Northwind;uid=sa;pwd=;'
Dim myda As SqlDataAdapter = New SqlDataAdapter(sqlStmt, conString)
myda.Fill(ds, 'Table')
DataList1.DataSource = ds
DataList1.DataBind()
End Sub
’The ItemDataBound Event
Protected Sub ItemDB(ByVal sender AsObject, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs)
If e.Item.ItemType = ListItemType.AlternatingItem Or e.Item.ItemType = ListItemType.Item Then
Dim strval AsString = CType(e.Item.FindControl('lblTitle'), Label).Text
Dim title AsString = ViewState('title')
If title = strval Then
CType(e.Item.FindControl('lblTitle'), Label).Text = ''
e.Item.Visible = FalseElse
title = strval
ViewState('title') = title
CType(e.Item.FindControl('lblTitle'), Label).Text = title
e.Item.Visible = TrueEndIfEndIfEnd Sub
C#
voidPage_Load(object sender, EventArgs e)
{
if (!(Page.IsPostBack))
{
BindTitle();
}
}
voidBindTitle()
{
DataSet ds = new DataSet();
string sqlStmt = 'SELECT * FROM Employees order by title';
string conString = 'server=localhost;database=Northwind;uid=sa;pwd=;';
SqlDataAdapter myda = new SqlDataAdapter(sqlStmt, conString);
myda.Fill(ds, 'Table');
DataList1.DataSource = ds;
DataList1.DataBind();
}
protectedvoidItemDB(object sender, System.Web.UI.WebControls.DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.AlternatingItem | e.Item.ItemType == ListItemType.Item)
{
string strval = ((Label)(e.Item.FindControl('lblTitle'))).Text;
string title = ViewState('title');
if (title == strval)
{
((Label)(e.Item.FindControl('lblTitle'))).Text = '';
e.Item.Visible = false;
}
else
{
title = strval;
ViewState('title') = title;
((Label)(e.Item.FindControl('lblTitle'))).Text = title;
e.Item.Visible = true;
}
}
}
Dim intStart As Integer
Dim intpageSize As Integer
Dim cn As SqlConnection
Dim da As SqlDataAdapter
Dim ds As DataSet
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
’Put user code to initialize the page here
IfNot Page.IsPostBack Then
ViewState('Start') = 0
bindList()
EndIfEnd Sub
Sub bindList()
cn = New SqlConnection('server=localhost;uid=sa;pwd=;database=northwind')
da = New SqlDataAdapter('Select * from Products ', cn)
ds = New DataSet
intStart = ViewState('Start')
ViewState('pageSize') = 14
da.Fill(ds, intStart, ViewState('pageSize'), 'Table')
DataList1.DataSource = ds
DataList1.DataBind()
End Sub
Private Sub lnkPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lnkPrevious.Click
intStart = ViewState('Start')- ViewState('pageSize')
ViewState('Start') = intStart
If intStart <= 0Then
ViewState('Start') = 0EndIf
bindList()
End Sub
Private Sub lnkNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lnkNext.Click
Dim dlistcount AsInteger = DataList1.Items.Count
intStart = ViewState('Start') + ViewState('pageSize')
ViewState('Start') = intStart
If dlistcount < ViewState('pageSize') Then
ViewState('Start') = ViewState('Start') - ViewState('pageSize')
EndIf
bindList()
End Sub
Private Sub lnkFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
If intStart <= 0Then
ViewState('Start') = 0EndIf
bindList()
End Sub
C#
int intStart ;
SqlConnection cn ;
SqlDataAdapter da ;
DataSet ds ;
privatevoidPage_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page hereif(!Page.IsPostBack )
{
ViewState['Start'] = 0;
bindList();
}
}
voidbindList()
{
cn = new SqlConnection('server=localhost;uid=sa;pwd=;database=northwind');
da = new SqlDataAdapter('Select * from Products ', cn);
ds = new DataSet();
intStart = (int)ViewState['Start'];
ViewState['pageSize'] = 14;
da.Fill(ds, intStart,(int) ViewState['pageSize'], 'Table');
DataList1.DataSource = ds;
DataList1.DataBind();
}
privatevoidlnkPrevious_Click(object sender, System.EventArgs e)
{
intStart = (int) ViewState['Start'] -(int) ViewState['pageSize'];
ViewState['Start'] = intStart;
if (intStart <= 0 )
{
ViewState['Start'] = 0;
}
bindList();
}
privatevoidlnkNext_Click(object sender, System.EventArgs e)
{
int dlistcount = DataList1.Items.Count;
intStart = (int) ViewState['Start']+(int) ViewState['pageSize'];
ViewState['Start'] = intStart;
if ( dlistcount < (int)ViewState['pageSize'] )
{
ViewState['Start'] = (int)ViewState['Start'] - (int)ViewState['pageSize'];
}
bindList();
}
’Populatethe DataList in the Page_Load EventProtectedSub ItemDB(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs)Ife.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem ThenIfCType(e.Item.FindControl('chkDiscontinued'), CheckBox).Checked Thene.Item.BackColor = Color.CadetBlueEndIfEndIfEndSub
C#
//Bind the DataList in the Page_Load EventprotectedvoidItemDB(object sender , System.Web.UI.WebControls.DataListItemEventArgs e )
{
if(( e.Item.ItemType == ListItemType.Item)||( e.Item.ItemType == ListItemType.AlternatingItem) )
{
if( ((CheckBox)(e.Item.FindControl('chkDiscontinued'))).Checked )
{
e.Item.BackColor = Color.CadetBlue;
}
}
}
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
’Put user code to initialize the page here
’Populate the DataList with DataSet
End Sub
Protected Sub ItemDB(ByVal sender AsObject, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles DataList1.ItemDataBound
Dim drv As DataRowView = CType(e.Item.DataItem, DataRowView)
Dim BDate As DateTime
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
’Tocheckif the BirthDate has nullvaluesIfNot drv.Row('BirthDate').ToString = DBNull.Value.ToString Then
’Assign the BirthDate tovariable BDate
BDate = DateTime.Parse(drv.Row('BirthDate').ToString)
EndIf
’If the Employee BirthDay is Today
’Change the rowText Color to blue
If BDate.ToString('dd/MM') = DateTime.Now.ToString('dd/MM') Then
e.Item.ForeColor = Color.Blue
e.Item.Font.Bold = TrueEndIfEndIfEnd Sub
C#
privatevoidPage_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
DataList1.DataSource =BindDataClass.BindData ();
DataList1.DataBind ();
}
protectedvoidItemDB(Object sender , System.Web.UI.WebControls.DataListItemEventArgs e )
{
DataRowView drv = (DataRowView)e.Item.DataItem;
DateTime BDate=Convert.ToDateTime (null) ;
if ((e.Item.ItemType == ListItemType.Item )||( e.Item.ItemType == ListItemType.AlternatingItem))
{ //To check if the BirthDate has null valuesif( drv.Row['BirthDate'].ToString() != DBNull.Value.ToString())
{
//Assign the BirthDate to variable BDate
BDate = DateTime.Parse(drv.Row['BirthDate'].ToString());
}
//If the Employee BirthDay is Today //Change the row Text Color to blueif (BDate.ToString('dd/MM') == DateTime.Now.ToString('dd/MM'))
{
e.Item.ForeColor = Color.Blue;
e.Item.Font.Bold = true;
}
}
}
Dimarrlist As New ArrayListDimenumColor As New KnownColorDimColors As Array = [Enum].GetValues(enumColor.GetType())Dimclr As ObjectForEach clr In ColorsIfNot Color.FromKnownColor(CType(clr, KnownColor)).IsSystemColor Thenarrlist.Add(clr.ToString())EndIfNextDataList1.DataSource = arrlistDataList1.DataBind()
C#
ArrayList arrlist = new ArrayList ();
KnownColor enumColor = new KnownColor();
Array Colors = Enum.GetValues(enumColor.GetType());
foreach(object clr in Colors)
{
if (!Color.FromKnownColor((KnownColor)clr).IsSystemColor)
arrlist.Add ( clr.ToString());
}
DataList1.DataSource = arrlist;
DataList1.DataBind();