Monday, March 31, 2008

Moving Item from listbox to another (1)

Create new Website and in Default.aspx add two Listbox (lstEmployees , lstSelectedEmployees) and add six buttons (<< ,>>,<,>,^,v) with Id's
(btnremoveall,btnaddall,btnremoveitem,btnadditem,btnColmoveup,btnColmovedown)

and in Default.aspx.cs

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
#region Methods
private void MoveUp(ListBox lstBox)
{
int iIndex, iCount, iOffset, iInsertAt, iIndexSelectedMarker = -1;
string lItemData, lItemval;
try
{

// Get the count of items in the list control
iCount = lstBox.Items.Count;

// Set the base loop index and the increment/decrement value based on the direction the item are being moved (up or down).
iIndex = 0;
iOffset = -1;

// Loop through all of the items in the list.
while (iIndex < iCount)
{
// Check if this item is selected.
if (lstBox.SelectedIndex > 0)
{
// Get the item data for this item
lItemval = lstBox.SelectedItem.Value.ToString();
lItemData = lstBox.SelectedItem.Text.ToString();
iIndexSelectedMarker = lstBox.SelectedIndex;

// Don't move selected items past other selected items
if (-1 != iIndexSelectedMarker)
{
for (int iIndex2 = 0; iIndex2 < iCount; ++iIndex2)
{
// Find the index of this item in enabled list
if (lItemval == lstBox.Items[iIndex2].Value.ToString())
{
// Remove the item from its current position
lstBox.Items.RemoveAt(iIndex2);

// Reinsert the item in the array one space higher than its previous position
iInsertAt = (iIndex2 + iOffset) < 0 ? 0 : iIndex2 + iOffset;
ListItem li = new ListItem(lItemData, lItemval);
lstBox.Items.Insert(iInsertAt, li);
break;
}
}
}
}
// If this item wasn't selected save the index so we can check it later so we don't move past the any selected items.
else if (-1 == iIndexSelectedMarker)
{
iIndexSelectedMarker = iIndex;
break;

}

iIndex = iIndex + 1;
}

if (iIndexSelectedMarker == 0)
lstBox.SelectedIndex = iIndexSelectedMarker;
else
lstBox.SelectedIndex = iIndexSelectedMarker - 1;
}
catch (Exception expException)
{
Response.Write(expException.Message);
}

}

private void MoveDown(ListBox lstBox)
{
try
{
int iIndex, iCount, iOffset, iInsertAt, iIndexSelectedMarker = -1;
string lItemData;
string lItemval;


// Get the count of items in the list control
iCount = lstBox.Items.Count;

// Set the base loop index and the increment/decrement value based on the direction the item are being moved (up or down).
iIndex = iCount - 1;
iOffset = 1;

// Loop through all of the items in the list.
while (iIndex >= 0)
{
// Check if this item is selected.
if (lstBox.SelectedIndex >= 0)
{
// Get the item data for this item
lItemData = lstBox.SelectedItem.Text.ToString();
lItemval = lstBox.SelectedItem.Value.ToString();
iIndexSelectedMarker = lstBox.SelectedIndex;

// Don't move selected items past other selected items
if (-1 != iIndexSelectedMarker)
{
for (int iIndex2 = 0; iIndex2 < iCount - 1; ++iIndex2)
{
// Find the index of this item in enabled list
if (lItemval == lstBox.Items[iIndex2].Value.ToString())
{
// Remove the item from its current position

lstBox.Items.RemoveAt(iIndex2);

// Reinsert the item in the array one space higher than its previous position
iInsertAt = (iIndex2 + iOffset) < 0 ? 0 : iIndex2 + iOffset;
ListItem li = new ListItem(lItemData, lItemval);
lstBox.Items.Insert(iInsertAt, li);
break;

}
}
}
}
iIndex = iIndex - 1;
}
if (iIndexSelectedMarker == lstBox.Items.Count - 1)
lstBox.SelectedIndex = iIndexSelectedMarker;
else
lstBox.SelectedIndex = iIndexSelectedMarker + 1;
}
catch (Exception expException)
{
Response.Write(expException.Message);
}
}

private void AddRemoveAll(ListBox aSource, ListBox aTarget)
{

try
{

foreach (ListItem item in aSource.Items)
{
aTarget.Items.Add(item);
}
aSource.Items.Clear();

}
catch (Exception expException)
{
Response.Write(expException.Message);
}

}

private void AddRemoveItem(ListBox aSource, ListBox aTarget)
{

ListItemCollection licCollection;

try
{

licCollection = new ListItemCollection();
for (int intCount = 0; intCount < aSource.Items.Count; intCount++)
{
if (aSource.Items[intCount].Selected == true)
licCollection.Add(aSource.Items[intCount]);
}

for (int intCount = 0; intCount < licCollection.Count; intCount++)
{
aSource.Items.Remove(licCollection[intCount]);
aTarget.Items.Add(licCollection[intCount]);
}

}
catch (Exception expException)
{
Response.Write(expException.Message);
}
finally
{
licCollection = null;
}

}
#endregion

protected void Page_Load(object sender, EventArgs e)
{

}

protected void btnaddall_Click(object sender, EventArgs e)
{
AddRemoveAll(lstEmployees, lstSelectedEmployees);
}

protected void btnadditem_Click(object sender, EventArgs e)
{
AddRemoveItem(lstEmployees, lstSelectedEmployees);
}

protected void btnremoveitem_Click(object sender, EventArgs e)
{
AddRemoveItem(lstSelectedEmployees, lstEmployees);
}

protected void btnremoveall_Click(object sender, EventArgs e)
{
AddRemoveAll(lstSelectedEmployees, lstEmployees);
}

protected void btnColmoveup_Click(object sender, EventArgs e)
{
MoveUp(lstSelectedEmployees);
}

protected void btnColmovedwn_Click(object sender, EventArgs e)
{
MoveDown(lstSelectedEmployees);
}
}

Copyright Method

Create new web project and in Default.asp add lable (Id=lblCopyright)

and in Default.aspx.cs

public string DynamicCopyright(string YearSiteCreated)
{
string DynamicCopyrightYear;
//Is the current year equal to the year the site was created?
if (DateTime.Now.Year.ToString() != YearSiteCreated)
{
//If not then concatenate year created and current year
DynamicCopyrightYear = YearSiteCreated + "-" + DateTime.Now.Year.ToString();
}
else
{
DynamicCopyrightYear = YearSiteCreated;
}

string Copyright = "Copyright © " + DynamicCopyrightYear + ". All rights reserved.";
return Copyright;
}

protected void Page_Load(object sender, System.EventArgs e)
{
lblCopyright.Text = DynamicCopyright(2006);
//Use the created year as parameter
}

Method to Encrypt and Decrypt

Create new Project and in add new class in App_Code name it Encryption

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Security.Cryptography;
using System.Xml;
using System.Text;
using System.IO;


public class Encryption
{
public string Decrypt(string stringToDecrypt)
{
stringToDecrypt = stringToDecrypt.Replace(" ", "+");
string sEncryptionKey = "01234567890123456789";
byte[] key = { };
byte[] IV = { 10, 20, 30, 40, 50, 60, 70, 80 };
byte[] inputByteArray = new byte[stringToDecrypt.Length];
try
{
key = Encoding.UTF8.GetBytes(sEncryptionKey.Substring(0, 8));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
inputByteArray = Convert.FromBase64String(stringToDecrypt);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(key, IV), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
Encoding encoding = Encoding.UTF8;
return encoding.GetString(ms.ToArray());
}
catch (System.Exception ex)
{
return (string.Empty);
}
}


public string Encrypt(string stringToEncrypt)
{
string sEncryptionKey = "01234567890123456789";
byte[] key = { };
byte[] IV = { 10, 20, 30, 40, 50, 60, 70, 80 };
byte[] inputByteArray; //Convert.ToByte(stringToEncrypt.Length)

try
{
key = Encoding.UTF8.GetBytes(sEncryptionKey.Substring(0, 8));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
inputByteArray = Encoding.UTF8.GetBytes(stringToEncrypt);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(key, IV), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
return Convert.ToBase64String(ms.ToArray());
}
catch
{
return (string.Empty);
}
}

}