ASP.NET FAQ - DataList

Find answers for the most frequently asked questions
Expand All Collapse All

Use a DataList as follows.

.aspx


<asp:datalist id='DataList1' runat='server'>
<ItemTemplate>
	<asp:repeater datasource='<%#Container.DataItem%>' runat=server ID='Repeater1' >
	<ItemTemplate>
			<%# Container.DataItem %>
	</ItemTemplate>
	</asp:repeater>
</ItemTemplate>
</asp:datalist>

VB.NET


Private Sub Page_Load(sender As Object, e As System.EventArgs)
   	’ Put user code to initialize the page here
  	 ’ Create and populate a multi-dimensional array
   	Dim MyArray(4) As Integer()
	Dim i As Integer
	For i = 0 To 3
	      MyArray(i) = New Integer(5) {}
	      Dim x As Integer
		For x = 0 To 4
		         MyArray(i)(x) = x + 5 * i
		Next 
	Next 
	DataList1.DataSource = MyArray
	DataList1.DataBind()
End Sub ’Page_Load

C#


private void Page_Load(object sender, System.EventArgs e)
{
	// Put user code to initialize the page here
	// Create and populate a multi-dimensional array
	int [][] MyArray = new int[4][];
	for (int i=0; i<4; i++) 
	{
		MyArray[i] = new int[5];
		for (int x=0; x<5; x++) 
		{
			MyArray[i][x] = x+(5*i);
		}
	}
	DataList1.DataSource = MyArray;
	DataList1.DataBind();
}

Permalink

Use namespace System.IO


<asp:label id='lblHeader' runat='server'></asp:label><br>
<b>Get Information on Directory:</b><br>
<p><asp:textbox id='txtPath' runat='server'></asp:textbox>
<p></p>
<asp:button id='btnSubmit' runat='server' text='Go!' type='Submit'></asp:button>
<p></p>
<div style='width:100%; height:200; overflow:auto;'>
<asp:datalist id='DataList1' runat='server' DataKeyField='FullName' OnSelectedIndexChanged='SelectedIndexChanged'>
	<ItemTemplate>
	<li>
		<%# DataBinder.Eval(Container.DataItem, 'Name') %>
		<br>
		<font size='-1'>[
		<asp:linkbutton Text='View Contents' CommandName='Select' runat='server' ID='Linkbutton1' />] 
			| [
		<%# DataBinder.Eval(Container.DataItem, 'Length') %>
		bytes] </font>
		<p>
	</ItemTemplate>
</asp:datalist>
</div> 
<p>
<hr>
</p><asp:label id='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 As String = Request('txtPath')
            	lblHeader.Text = 'File Listing for ' & strDir & ''
            	Dim dirInfo As New DirectoryInfo(strDir)
            	’ Get the files for the directory strDir
            	Dim fInfos As FileInfo() = dirInfo.GetFiles('*.*')
            	DataList1.DataSource = fInfos
            	DataList1.DataBind()
        End If
End Sub

Protected Sub SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataList1.SelectedIndexChanged
        Dim strFilePath As String = DataList1.DataKeys(DataList1.SelectedItem.ItemIndex).ToString()
        Dim fInfo As FileInfo = New FileInfo(strFilePath)
        Dim objStream As StreamReader = fInfo.OpenText()
        Dim strContents As String = 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 here
	if (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 + '
' + '';
}
Permalink

VB.NET


<asp:DataList id='DataList1' runat='server'>
	<HeaderTemplate>
	<table>
	</HeaderTemplate>
	<ItemTemplate>
	<tr>
	<td>
		<%# CType(Container.DataItem, DictionaryEntry).Key  %>
	</td>
	<td>
		<%# CType(Container.DataItem, DictionaryEntry).Value%>
	</td>
	</tr>
	</ItemTemplate>
	<FooterTemplate>
	</table>
	</FooterTemplate>
</asp:DataList>

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

C#


<asp:DataList id='DataList1' runat='server'>
	<HeaderTemplate>
	<table>
	</HeaderTemplate>
	<ItemTemplate>
	<tr>
	<td>
		<%# ((DictionaryEntry)Container.DataItem).Key %>
	</td>
	<td>
		<%# ((DictionaryEntry)Container.DataItem).Value %>
	</td>
	</tr>
	</ItemTemplate>
	<FooterTemplate>
	</table>
	</FooterTemplate>
</asp:DataList>

private void Page_Load(object sender, System.EventArgs e)
{
	// Put user code to initialize the page here
	DataList1.DataSource = System.Environment.GetEnvironmentVariables();
	DataList1.DataBind();
}
Permalink

<asp:Datalist runat='server' OnItemCommand ='ItemCmd' id='Datalist1' Font-Size='10pt' Font-Name='Verdana'>
	<ItemTemplate>
		<asp:LinkButton style='text-decoration:none' runat='server' id='btnDetails' Text='+' CommandName='Show' Font-Name='Verdana' />
		<b>
		<%# DataBinder.Eval(Container.DataItem, 'employeeid') %>
		</b>
		<br />
		<asp:label id='lblEID' Visible='False' runat='Server' Text=’<%# DataBinder.Eval(Container.DataItem, 'Employeeid') %>’ />
		<asp:DataGrid runat='server' id='Datagrid1' Font-Name='Verdana' Font-Size='10pt' HorizontalAlign='Center'
				Visible='False' Width='85%'>
	</ItemTemplate>
</asp:Datalist>

VB.NET


Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
If Not Page.IsPostBack Then
            ’Bind data to DataList
End If
End Sub

Private Sub Datalist1_ItemCommand(ByVal source As Object, _
    ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) 
If e.CommandName = 'Show' Then
            Dim EmpIDlabel As Label = e.Item.FindControl('lblEID')
            Dim strEmpID As String = EmpIDlabel.Text
            CType(e.Item.FindControl('Datagrid1'), DataGrid).DataSource = GetEmpDetails(strEmpID)
            CType(e.Item.FindControl('Datagrid1'), DataGrid).DataBind()
            CType(e.Item.FindControl('Datagrid1'), DataGrid).Visible = True
            CType(e.Item.FindControl('btnDetails'), LinkButton).Text = '-'
            CType(e.Item.FindControl('btnDetails'), LinkButton).CommandName = 'Hide'
End If
If e.CommandName = 'Hide' Then
            CType(e.Item.FindControl('Datagrid1'), DataGrid).Visible = False
            CType(e.Item.FindControl('btnDetails'), LinkButton).Text = '+'
            CType(e.Item.FindControl('btnDetails'), LinkButton).CommandName = 'Show'
End If
End Sub

Function GetEmpDetails(ByVal Employeeid As String) As SqlDataReader
        Const strConnString As String = 'server=localhost;uid=sa;pwd=;database=northwind'
        Dim objConn As New SqlConnection(strConnString)
        Dim strSQL As String
        strSQL = 'SELECT FirstName , LastName ,Title, Address FROM Employees ' & _
                 'WHERE Employeeid = @Employeeid'
        Dim objCmd As New SqlCommand(strSQL, objConn)
        Dim paramEmployeeid As SqlParameter
        paramEmployeeid = New SqlParameter('@Employeeid', SqlDbType.VarChar, 10)
        paramEmployeeid.Value = Employeeid
        objCmd.Parameters.Add(paramEmployeeid)
        objConn.Open()   ’Open the connection
        Return objCmd.ExecuteReader(CommandBehavior.CloseConnection)
End Function

C#


private void Page_Load(object sender, System.EventArgs e)
{
	// Put user code to initialize the page here
	if(!Page.IsPostBack )
	{
		//Bind data to Datalist1
	}
}

protected void ItemCmd(object source  ,  System.Web.UI.WebControls.DataListCommandEventArgs e ) 
{ 
	if( e.CommandName == 'Show' )
	{
		Label EmpIDlabel =(Label) e.Item.FindControl('lblEID');
		string strEmpID = EmpIDlabel.Text;
		((DataGrid)e.Item.FindControl('Datagrid1')).DataSource = GetEmpDetails(strEmpID);
		((DataGrid)e.Item.FindControl('Datagrid1')).DataBind();
		((DataGrid)e.Item.FindControl('Datagrid1')).Visible = true;
		((LinkButton)e.Item.FindControl('btnDetails')).Text = '-';
		((LinkButton)e.Item.FindControl('btnDetails')).CommandName = 'Hide';
	}
	if( e.CommandName == 'Hide' )
	{
		((DataGrid)e.Item.FindControl('Datagrid1')).Visible = false;
		((LinkButton )e.Item.FindControl('btnDetails')).Text = '+';
		((LinkButton)e.Item.FindControl('btnDetails')).CommandName = 'Show';
	}
}

protected  SqlDataReader GetEmpDetails(string Employeeid )
{
	string strConnString  = 'server=localhost;uid=sa;pwd=;database=northwind';
	SqlConnection objConn = new SqlConnection(strConnString);
	string strSQL   ;
	strSQL = 'SELECT FirstName , LastName ,Title, Address FROM Employees WhERE Employeeid = @Employeeid';
	SqlCommand objCmd = new SqlCommand(strSQL, objConn);
	SqlParameter paramEmployeeid ; 
	paramEmployeeid = new SqlParameter('@Employeeid', SqlDbType.VarChar, 10);
	paramEmployeeid.Value = Employeeid;
	objCmd.Parameters.Add(paramEmployeeid);
	objConn.Open()  ;// Open the connection;
	return objCmd.ExecuteReader(CommandBehavior.CloseConnection);
}
Permalink

<asp:DataList id='DataList1' runat='server'></asp:DataList>

VB.NET

In class


Public Class DatalistLabelColumn
    Implements ITemplate

Public Sub New()
End Sub ’New

Public Sub InstantiateIn(ByVal container As Control) Implements System.Web.UI.ITemplate.InstantiateIn
        	Dim label1 As New Label
        	AddHandler label1.DataBinding, AddressOf Me.BindLabelColumn
        	container.Controls.Add(label1)
End Sub ’InstantiateIn

Public Sub BindLabelColumn(ByVal sender As Object, ByVal e As EventArgs)
        	Dim lbl As Label = CType(sender, Label)
        	Dim container As 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 

End Class ’DatalistLabelColumn

Dim ds As DataSet = ’Fill the dataset
DataList1.ItemTemplate = New DatalistLabelColumn
DataList1.DataSource = ds
DataList1.DataBind()

C#

In class


public class DatalistLabelColumn : ITemplate
{
	public DatalistLabelColumn()
	{
		//Add constructor stuff here
	}

	public void InstantiateIn(Control container)
	{
		Label label1 = new Label();
		label1.DataBinding += new EventHandler(this.BindLabelColumn);
		container.Controls.Add(label1);
	}

	public void BindLabelColumn(object sender, EventArgs e)
	{
		Label lbl = (Label)sender;
		DataListItem  container = (DataListItem)lbl.NamingContainer ;
		String strVals =Convert.ToString(DataBinder.Eval(((DataListItem)container).DataItem, 'LastName')) 
			+ ', ' +
			Convert.ToString(DataBinder.Eval(((DataListItem)container).DataItem, 'FirstName')) ;
		lbl.Text  = strVals;
		
	}
}

DataSet ds = //Assign appropriate value;
DataList1.ItemTemplate = new DatalistLabelColumn();
DataList1.DataSource =ds;
DataList1.DataBind();
Permalink

<asp:DataList id='DataList1' runat='server' onItemDataBound='ItemDB'> 
<HeaderTemplate > 
    <table width=100%> 
</HeaderTemplate> 
<ItemTemplate> 
   <tr><td> 
   <asp:Label Runat=server 
   text=<%#DataBinder.Eval(Container.DataITem, 'Title')%> ID='lblTitle'> 
   </asp:Label> 
   <td><asp:Label Runat=server 
   text=<%#DataBinder.Eval(Container.DataITem, 'LastName')%> ID='lblLastName'> 
   </asp:Label> 
</ItemTemplate> 
<FooterTemplate> 
   </table> 
</FooterTemplate> 
</asp:DataList> 

VB.NET


Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
If Not Page.IsPostBack Then
	BindTitle()
End If
End Sub

’Bind Data to DataList Populating the Dataset
Sub BindTitle()
	Dim ds As New DataSet
	Dim sqlStmt As String = 'SELECT * FROM Employees order by title'
	Dim conString As String = '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 As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs)
If e.Item.ItemType = ListItemType.AlternatingItem Or e.Item.ItemType = ListItemType.Item Then
Dim strval As String = CType(e.Item.FindControl('lblTitle'), Label).Text
Dim title As String = ViewState('title')
	If title = strval Then
		CType(e.Item.FindControl('lblTitle'), Label).Text = ''
		e.Item.Visible = False
	Else
		title = strval
		ViewState('title') = title
		CType(e.Item.FindControl('lblTitle'), Label).Text = title
		e.Item.Visible = True
	End If
End If
End Sub

C#


void Page_Load(object sender, EventArgs e) 
{ 
	if (!(Page.IsPostBack)) 
	{ 
		BindTitle(); 
	} 
} 

void BindTitle() 
{ 
	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(); 
} 

protected void ItemDB(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; 
		} 
	} 
}
Permalink

<asp:datalist id='DataList1' runat='server'>
	<ItemTemplate>
		<%#DataBinder.Eval(Container.DataItem, 'ProductID').ToString()%>
		<%#DataBinder.Eval(Container.DataItem, 'ProductName').ToString()%>
	</ItemTemplate>
</asp:datalist>
<table width='50%' border='0'>
	<tr>
		<td><asp:LinkButton id='lnkPrevious' runat='server'><<</asp:LinkButton>
		<td><asp:LinkButton id='lnkNext' runat='server'>>></asp:LinkButton>
	</tr>
</table>

VB.NET


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 
        	If Not Page.IsPostBack Then
            		ViewState('Start') = 0
            		bindList()
        	End If
End 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 <= 0 Then
            		ViewState('Start') = 0
              End If
              bindList()
End Sub

Private Sub lnkNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lnkNext.Click
        	Dim dlistcount As Integer = DataList1.Items.Count
        	intStart = ViewState('Start') + ViewState('pageSize')
        	ViewState('Start') = intStart
        	If dlistcount < ViewState('pageSize') Then
            		ViewState('Start') = ViewState('Start') - ViewState('pageSize')
        	End If
        	bindList()
End Sub

Private Sub lnkFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
  	If intStart <= 0 Then
            		ViewState('Start') = 0
        	End If
        	bindList()
End Sub

C#


int intStart ;

SqlConnection cn ; 
SqlDataAdapter da ; 
DataSet ds ; 
private void Page_Load(object sender, System.EventArgs e)
{
	// Put user code to initialize the page here
	if(!Page.IsPostBack )
	{
		ViewState['Start'] = 0;
		bindList();
	}
}

void bindList()
{
	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();
}

private void lnkPrevious_Click(object sender, System.EventArgs e)
{
	intStart = (int) ViewState['Start'] -(int) ViewState['pageSize'];
	ViewState['Start'] = intStart;
	if (intStart <= 0 )
	{
		ViewState['Start'] = 0;
	}
	bindList();
}

private void lnkNext_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();
}
Permalink

<asp:DataList id='DataList1' runat='server'>
	<ItemTemplate>
	<asp:HyperLink Runat =server NavigateUrl =’<%#'webform1.aspx?id=' + DataBinder.Eval(Container.DataItem, 'productid').ToString()%>’ ID='Hyperlink1'>
		<%#DataBinder.Eval(Container.DataItem, 'ProductName')%>
	</asp:HyperLink>
	</ItemTemplate>
</asp:DataList>
Permalink

<asp:datalist id='DataList1' OnItemDataBound ='ItemDB' runat='server'>
<ItemTemplate >
<asp:CheckBox id='chkDiscontinued' Runat =server  
	checked=<%#Convert.ToBoolean(DataBinder.Eval(Container.DataItem , 'Discontinued').ToString())%>>
</asp:CheckBox> 
<asp:label ID='lblName' runat=server text=<%#DataBinder.Eval(Container.DataItem , 'ProductName').ToString() %>></asp:label>
</ItemTemplate>
</asp:datalist>

VB.NET


’Populate the DataList in the Page_Load Event
Protected Sub ItemDB(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs)
        If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
            If CType(e.Item.FindControl('chkDiscontinued'), CheckBox).Checked Then
                e.Item.BackColor = Color.CadetBlue
            End If
        End If
End Sub

C#


//Bind the DataList in the Page_Load Event
protected void ItemDB(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;
		}
	}   
}	
Permalink

<asp:datalist id='DataList1' OnItemDataBound ='ItemDB' GridLines='Both' RepeatColumns='2' runat='server' ShowHeader='true'>
	<HeaderStyle Font-Bold='True' HorizontalAlign='Center' Font-Name='verdana' Font-Size='10pt'></HeaderStyle>
	<ItemStyle Font-Size='10pt'></ItemStyle>
	<Headertemplate>
		Employee Details
	</Headertemplate>
	<ItemTemplate>
		<%#DataBinder.Eval(Container.DataItem,'LastName')%>
		,
		<%#DataBinder.Eval(Container.DataItem,'FirstName')%>
		<br>
		HireDate :
		<%#DataBinder.Eval(Container.DataItem,'HireDate')%>
		<br>
	</ItemTemplate>
</asp:datalist>

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
	’Populate the DataList with DataSet
End Sub

Protected Sub ItemDB(ByVal sender As Object, 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
		’To check if the BirthDate has null values
	            	If Not drv.Row('BirthDate').ToString = DBNull.Value.ToString Then
	                ’Assign the BirthDate to variable BDate
                		BDate = DateTime.Parse(drv.Row('BirthDate').ToString)
		End If

	            	’If the Employee BirthDay is Today 
	            	’Change the row Text Color to blue
            		If BDate.ToString('dd/MM') = DateTime.Now.ToString('dd/MM') Then
		                e.Item.ForeColor = Color.Blue
		                e.Item.Font.Bold = True
		End If
	End If
End Sub

C#


private void Page_Load(object sender, System.EventArgs e)
{
	// Put user code to initialize the page here
	DataList1.DataSource =BindDataClass.BindData ();
	DataList1.DataBind (); 
}

protected void ItemDB(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 values
		if(  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 blue
		if (BDate.ToString('dd/MM') == DateTime.Now.ToString('dd/MM'))
		{
			e.Item.ForeColor = Color.Blue;
			e.Item.Font.Bold = true;
		}
	}
}
Permalink

<asp:DataList id='DataList1' runat='server' >
<HeaderTemplate ><table></HeaderTemplate>
<ItemTemplate >
<tr>
	<td bgcolor =<%#Container.DataItem%> runat=server id='td'>
	<%#Container.DataItem%>
	</td>
</tr>
</ItemTemplate>
<FooterTemplate></table></FooterTemplate>
</asp:DataList>

VB.NET


Dim arrlist As New ArrayList
Dim enumColor As New KnownColor
Dim Colors As Array = [Enum].GetValues(enumColor.GetType())
Dim clr As Object
For Each clr In Colors
            If Not Color.FromKnownColor(CType(clr, KnownColor)).IsSystemColor Then
		arrlist.Add(clr.ToString())
            End If
Next 
DataList1.DataSource = arrlist
DataList1.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();
Permalink

Share with

Couldn't find the FAQs you're looking for?

Please submit your question and answer.