Create new Website and in Default.aspx add two Listbox (ListBox1 , ListBox2) and add items in Listbox1 and add two buttons (<< ,>>) with Id's
(btnMoveLeft,btnMoveRight)
The Javascript code will be:
//function fnMoveItems(lstbxFrom,lstbxTo)
//{
// var varFromBox = document.all(lstbxFrom);
// var varToBox = document.all(lstbxTo);
// if ((varFromBox != null) && (varToBox != null))
// {
// if(varFromBox.length < 1)
// {
// alert('There are no items in the source ListBox');
// return false;
// }
// if(varFromBox.options.selectedIndex == -1)
// when no Item is selected the index will be -1
// {
// alert('Please select an Item to move');
// return false;
// }
// while ( varFromBox.options.selectedIndex >= 0 )
// {
// Create a new instance of ListItem
// var newOption = new Option();
// newOption.text = varFromBox.options[varFromBox.options.selectedIndex].text;
// newOption.value = varFromBox.options[varFromBox.options.selectedIndex].value;
//Append the item in Target Listbox
// varToBox.options[varToBox.length] = newOption;
//Remove the item from Source Listbox
// varFromBox.remove(varFromBox.options.selectedIndex);
// }
// }
// return false;
//}
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 Default3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
btnMoveRight.Attributes.Add("onclick", "return fnMoveItems('ListBox1','ListBox2')");
btnMoveLeft.Attributes.Add("onclick", "return fnMoveItems('ListBox2','ListBox1')");
}
}
Monday, March 31, 2008
Move item from Listbox to another (2)
Create new Website and in Default.aspx add two Listbox (lstEmployees , lstSelectedEmployees) and in lstEmployees Listbox add items and add four buttons (<< ,>>,<,>) with Id's
(btn_AddAll,btn_RemoveAll,btn_Add,btn_Remove)
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 Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btn_Add_Click(object sender, EventArgs e)
{
if (lstEmployees.SelectedIndex > -1)
{
//Gets the value of items in list.
string _value = lstEmployees.SelectedItem.Value;
// Gets the Text of items in the list.
string _text = lstEmployees.SelectedItem.Text;
//create a list item
ListItem item = new ListItem();
//Assign the values to list item
item.Text = _text;
item.Value = _value;
//Add the list item to the selected list of employees
lstSelectedEmployees.Items.Add(item);
//Remove the details from employee list
lstEmployees.Items.Remove(item);
}
}
protected void btn_Remove_Click(object sender, EventArgs e)
{
if (lstSelectedEmployees.SelectedIndex > -1)
{
//Gets the value of items in list.
string _value = lstSelectedEmployees.SelectedItem.Value;
//Gets the Text of items in the list.
string _text = lstSelectedEmployees.SelectedItem.Text;
//create a list item
ListItem item = new ListItem();
item.Text = _text;
//Assign the values to list item
item.Value = _value;
//Remove from the selected list
lstSelectedEmployees.Items.Remove(item);
//Add in the Employee list
lstEmployees.Items.Add(item);
}
}
protected void btn_AddAll_Click(object sender, EventArgs e)
{
int _count = lstEmployees.Items.Count;
if (_count != 0)
{
for (int i = 0; i < _count; i++)
{
ListItem item = new ListItem();
item.Text = lstEmployees.Items[i].Text;
item.Value = lstEmployees.Items[i].Value;
//Add the item to selected employee list
lstSelectedEmployees.Items.Add(item);
}
}
//clear employee list
lstEmployees.Items.Clear();
}
protected void btn_RemoveAll_Click(object sender, EventArgs e)
{
int _count = lstSelectedEmployees.Items.Count;
if (_count != 0)
{
for (int i = 0; i < _count; i++)
{
ListItem item = new ListItem();
item.Text = lstSelectedEmployees.Items[i].Text;
item.Value = lstSelectedEmployees.Items[i].Value;
lstEmployees.Items.Add(item);
}
}
//clear the items
lstSelectedEmployees.Items.Clear();
}
}
(btn_AddAll,btn_RemoveAll,btn_Add,btn_Remove)
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 Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btn_Add_Click(object sender, EventArgs e)
{
if (lstEmployees.SelectedIndex > -1)
{
//Gets the value of items in list.
string _value = lstEmployees.SelectedItem.Value;
// Gets the Text of items in the list.
string _text = lstEmployees.SelectedItem.Text;
//create a list item
ListItem item = new ListItem();
//Assign the values to list item
item.Text = _text;
item.Value = _value;
//Add the list item to the selected list of employees
lstSelectedEmployees.Items.Add(item);
//Remove the details from employee list
lstEmployees.Items.Remove(item);
}
}
protected void btn_Remove_Click(object sender, EventArgs e)
{
if (lstSelectedEmployees.SelectedIndex > -1)
{
//Gets the value of items in list.
string _value = lstSelectedEmployees.SelectedItem.Value;
//Gets the Text of items in the list.
string _text = lstSelectedEmployees.SelectedItem.Text;
//create a list item
ListItem item = new ListItem();
item.Text = _text;
//Assign the values to list item
item.Value = _value;
//Remove from the selected list
lstSelectedEmployees.Items.Remove(item);
//Add in the Employee list
lstEmployees.Items.Add(item);
}
}
protected void btn_AddAll_Click(object sender, EventArgs e)
{
int _count = lstEmployees.Items.Count;
if (_count != 0)
{
for (int i = 0; i < _count; i++)
{
ListItem item = new ListItem();
item.Text = lstEmployees.Items[i].Text;
item.Value = lstEmployees.Items[i].Value;
//Add the item to selected employee list
lstSelectedEmployees.Items.Add(item);
}
}
//clear employee list
lstEmployees.Items.Clear();
}
protected void btn_RemoveAll_Click(object sender, EventArgs e)
{
int _count = lstSelectedEmployees.Items.Count;
if (_count != 0)
{
for (int i = 0; i < _count; i++)
{
ListItem item = new ListItem();
item.Text = lstSelectedEmployees.Items[i].Text;
item.Value = lstSelectedEmployees.Items[i].Value;
lstEmployees.Items.Add(item);
}
}
//clear the items
lstSelectedEmployees.Items.Clear();
}
}
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);
}
}
(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
}
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);
}
}
}
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);
}
}
}
Friday, March 28, 2008
How to move item from ListBox to another
Create new page and add two ListBox (lstEmployees ,lstSelectedEmployees) and in lstEmployees ListBox add items and the other one don't
also add four buttons (btn_Add , btn_Remove ,btn_AddAll ,btn_RemoveAll)
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 Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btn_Add_Click(object sender, EventArgs e)
{
if (lstEmployees.SelectedIndex > -1)
{
string _value = lstEmployees.SelectedItem.Value; //Gets the value of items in list.
string _text = lstEmployees.SelectedItem.Text; // Gets the Text of items in the list.
ListItem item = new ListItem(); //create a list item
item.Text = _text; //Assign the values to list item
item.Value = _value;
lstSelectedEmployees.Items.Add(item); //Add the list item to the selected list of employees
lstEmployees.Items.Remove(item); //Remove the details from employee list
}
}
protected void btn_Remove_Click(object sender, EventArgs e)
{
if (lstSelectedEmployees.SelectedIndex > -1)
{
string _value = lstSelectedEmployees.SelectedItem.Value; //Gets the value of items in list.
string _text = lstSelectedEmployees.SelectedItem.Text; // Gets the Text of items in the list.
ListItem item = new ListItem(); //create a list item
item.Text = _text; //Assign the values to list item
item.Value = _value;
lstSelectedEmployees.Items.Remove(item); //Remove from the selected list
lstEmployees.Items.Add(item); //Add in the Employee list
}
}
protected void btn_AddAll_Click(object sender, EventArgs e)
{
int _count = lstEmployees.Items.Count;
if (_count != 0)
{
for (int i = 0; i < _count; i++)
{
ListItem item = new ListItem();
item.Text = lstEmployees.Items[i].Text;
item.Value = lstEmployees.Items[i].Value;
//Add the item to selected employee list
lstSelectedEmployees.Items.Add(item);
}
}
//clear employee list
lstEmployees.Items.Clear();
}
protected void btn_RemoveAll_Click(object sender, EventArgs e)
{
int _count = lstSelectedEmployees.Items.Count;
if (_count != 0)
{
for (int i = 0; i < _count; i++)
{
ListItem item = new ListItem();
item.Text = lstSelectedEmployees.Items[i].Text;
item.Value = lstSelectedEmployees.Items[i].Value;
lstEmployees.Items.Add(item);
}
}
lstSelectedEmployees.Items.Clear();//clear the items
}
}
also add four buttons (btn_Add , btn_Remove ,btn_AddAll ,btn_RemoveAll)
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 Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btn_Add_Click(object sender, EventArgs e)
{
if (lstEmployees.SelectedIndex > -1)
{
string _value = lstEmployees.SelectedItem.Value; //Gets the value of items in list.
string _text = lstEmployees.SelectedItem.Text; // Gets the Text of items in the list.
ListItem item = new ListItem(); //create a list item
item.Text = _text; //Assign the values to list item
item.Value = _value;
lstSelectedEmployees.Items.Add(item); //Add the list item to the selected list of employees
lstEmployees.Items.Remove(item); //Remove the details from employee list
}
}
protected void btn_Remove_Click(object sender, EventArgs e)
{
if (lstSelectedEmployees.SelectedIndex > -1)
{
string _value = lstSelectedEmployees.SelectedItem.Value; //Gets the value of items in list.
string _text = lstSelectedEmployees.SelectedItem.Text; // Gets the Text of items in the list.
ListItem item = new ListItem(); //create a list item
item.Text = _text; //Assign the values to list item
item.Value = _value;
lstSelectedEmployees.Items.Remove(item); //Remove from the selected list
lstEmployees.Items.Add(item); //Add in the Employee list
}
}
protected void btn_AddAll_Click(object sender, EventArgs e)
{
int _count = lstEmployees.Items.Count;
if (_count != 0)
{
for (int i = 0; i < _count; i++)
{
ListItem item = new ListItem();
item.Text = lstEmployees.Items[i].Text;
item.Value = lstEmployees.Items[i].Value;
//Add the item to selected employee list
lstSelectedEmployees.Items.Add(item);
}
}
//clear employee list
lstEmployees.Items.Clear();
}
protected void btn_RemoveAll_Click(object sender, EventArgs e)
{
int _count = lstSelectedEmployees.Items.Count;
if (_count != 0)
{
for (int i = 0; i < _count; i++)
{
ListItem item = new ListItem();
item.Text = lstSelectedEmployees.Items[i].Text;
item.Value = lstSelectedEmployees.Items[i].Value;
lstEmployees.Items.Add(item);
}
}
lstSelectedEmployees.Items.Clear();//clear the items
}
}
Question for Dot Net Interview (Part 7):
--* What is Data Binding?
Data binding is a way used to connect values from a collection of data (e.g. DataSet) to the controls on a web form.
--* Describe the difference between inline and code behind.
1)Inline code is written along side the HTML in a page.
2)Code-behind is code written in a separate file and referenced by the .aspx page
--* What is the transport protocol you use to call a Web service?
SOAP (Simple Object Access Protocol) is the preferred protocol.
--* What is database replicaion
Replication is the process of copying/moving data between databases on the same or different servers
--* What are the different types of replication?
1) Transactional
2) Snapshot
3) Merge
--* What is the difference among “dropping a table”, “truncating a table” and “deleting all records” from a table.
* Dropping : (Table structure + Data are deleted), Invalidates the dependent objects ,Drops the indexes
* Truncating: (Data alone deleted), Performs an automatic commit, Faster than delete
* Delete : (Data alone deleted), Doesn’t perform automatic commit
--* BulkCopy is a tool used to copy huge amount of data from tables
--* Types of Trigger:
Inserted and Deleted.
--* What is SQL Profiler?
SQL Profiler is a graphical tool that allows system administrators to monitor events in an instance of Microsoft SQL Server
--* What is User Defined Functions?
User-Defined Functions allow to define its own T-SQL functions that can accept 0 or more parameters
and return a single scalar data value or a table data type
Data binding is a way used to connect values from a collection of data (e.g. DataSet) to the controls on a web form.
--* Describe the difference between inline and code behind.
1)Inline code is written along side the HTML in a page.
2)Code-behind is code written in a separate file and referenced by the .aspx page
--* What is the transport protocol you use to call a Web service?
SOAP (Simple Object Access Protocol) is the preferred protocol.
--* What is database replicaion
Replication is the process of copying/moving data between databases on the same or different servers
--* What are the different types of replication?
1) Transactional
2) Snapshot
3) Merge
--* What is the difference among “dropping a table”, “truncating a table” and “deleting all records” from a table.
* Dropping : (Table structure + Data are deleted), Invalidates the dependent objects ,Drops the indexes
* Truncating: (Data alone deleted), Performs an automatic commit, Faster than delete
* Delete : (Data alone deleted), Doesn’t perform automatic commit
--* BulkCopy is a tool used to copy huge amount of data from tables
--* Types of Trigger:
Inserted and Deleted.
--* What is SQL Profiler?
SQL Profiler is a graphical tool that allows system administrators to monitor events in an instance of Microsoft SQL Server
--* What is User Defined Functions?
User-Defined Functions allow to define its own T-SQL functions that can accept 0 or more parameters
and return a single scalar data value or a table data type
Question for Dot Net Interview (Part 6):
--* What is the difference between Dataset.clone() and Dataset.copy()?
1) Dataset.clone() --> Copy only structure (Returns a DataSet with the same structure (tables andrelationships) but no data.)
2) Dataset.copy() --> Copy Structure & Data Both (Returns an exact duplicate of the DataSet, with the same set of tables, relationships, and data.)
--* What is the difference between autopostback and ispostback?
1) Autopostback - Property of the control
2) IsPostback - Property of the Page class
--* What is the difference between ExcuteQuery and ExcuteNonQuery ?
1) ExcuteQuery used with select statement
2) ExcuteNonQuery used with insert ,update ,delete statement
3) ExcuteNonQuery return number of row affected
4) ExcuteQuery return one or more row of data
--* What is Tracing :
Trace in ASP.Net is nothing but to trace error and It will also give some details of the error, so that the user can easily debug the code.
--* Types of Tracing :
1) Application Level Tracing
2) Page Level Tracing
--* Different types of style sheets:
1) External
2) Inline
--* Viewstate : stores the state of controls in HTML hidden fields.
--* Can we run asp.net apllication without WEB.CONFIG file?
YES , Because all the configuration settings will be available under MACHINE.CONFIG file
--* Session state is global to your entire application for the current user. Session state can be lost in several ways:
1) If the user closes and restarts the browser.
2) If the user accesses the same page through a different browser window, although the session will still exist if a web page is accessed through the original browser window. Browsers differ on how they handle this situation.
3) If the session times out because of inactivity. By default, a session times out after 20 idle minutes.
4) If the programmer ends the session by calling Session.Abandon().
--* A transactionis:- a set of operations that must either succeed or fail as a unit. The goal of a transaction is to ensure that data is always in a valid
--* Types of Data Source in the.NET Framework:
1) SqlDataSource: This data source allows you to connect to any data source that has an ADO.NET data provider
2) ObjectDataSource: This data source allows you to connect to a custom data access class
3) XmlDataSource : This data source allows you to connect to an XML file
4) SiteMapDataSource: This data source allows you to connect to the Web.Sitemap file that describes the navigational structure of your website
--* Types of ADO.Net Data Provider:
1) System.Data.SqlClient
2) System.Data.OracleClient
3) System.Data.OleDb
4) System.Data.Odbc
--* What are types of sub queries :
1) Single row subquery
2) Multiple row subquery
3) Multiple column subquery
--* What command do we use to rename a Database sp_renamedb ‘oldname’ , ‘newname’
--* What is the command that we use to install IIS after you installed ASP.NET aspnet_regsql -i
--* What is the purpose of @@Identety:
Returns the last identity value inserted as a result of the last INSERT or SELECT INTO statement.
--* Describe the difference between inline code and code behind.
1) Inline code written along side the html in a page.
2) Code-behind is code written in seprate file and refrenced by .aspx in page.
--* What is the difference between System.Array.CopyTo and System.Array.Clone in .NET?
1) The Clone() method returns a new array object containing all the elements in the original array.
2) The CopyTo() method copies the elements into another existing array.
--* What is Encapsulation?
Encapsulation - is the ability of an object to hide its data and methods from the rest of the world. It is one of the fundamental principles of OOPs
--* Who is a protected class-level variable available to?
It is available to any sub-class (a class inheriting this class).
--* Describe the accessibility modifier “protected internal”.
It is available to classes that are within the same assembly and derived from the specified base class
--* What’s the difference between the Debug class and Trace class?
1) Use Debug class for debug builds,
2) use Trace class for both debug and release builds
--* Explain the differences between public, protected, private and internal.
1) Public: Allows class, methods, fields to be accessible from anywhere i.e. within and outside an assembly.
2) Private: When applied to field and method allows to be accessible within a class.3) Protected: Similar to private but can be accessed by members of derived class also.
4) Internal: They are public within the assembly i.e. they can be accessed by anyone within an assembly but outside assembly they are not visible.
--* List the event handlers that can be included in Global.asax?
1) Application start and end event handlers
2) Session start and end event handlers
3) Per-request event handlers
4) Non-deterministic event handlers
--* What’s a strong name?
A strong name includes: the name of the assembly, version number, culture identity, and a public key token.
--* What is constructor.
Constructor is a method in the class which has the same name as the class It initialises the member attributes whenever an instance of the class is created.
--* What is difference between CHAR and VARCHAR:
1) CHAR pads blank spaces to the maximum length.
2) VARCHAR2 does not pad blank spaces.
3) For CHAR the maximum length is 255 and 2000 for VARCHAR
--* What are the four types of events ?
1. System Events.
2. Control Events
3. User Events
4. Other Events
--* How many Global.asax files can an Application have?
Only one Global.asax file and it’s placed in the virtual directory’s root
--* What is a session?
A user accessing an application is known as a session.
--* Different between Local and Global Variable:
1) A variable is global if it is declared outside of the main program block and not within a function
2) A variable is local if it is declared between the function declaration
3) Global variables are visible and available to all statements in a setup script that follow its declaration
4) Local variables are visible and available only within the function where they are declared.
--* Difference Between EXE and DLL file :
1) Exe is the Application
2) DLL is the Component
3) Exe can execute on its own
4) DLL Can not execute on its own
5) EXE can run independently
6) DLL will run within an Exe
7) EXE is an out-process file
8) DLL is an in-process file
--* Difference between Strong and Weak type
1) Strong type: Checking the types of variables at compile time
2) Weak type: Checking the types of variables at run time.
--* Difference between Function and Stored Procedure:
1) Function returns only one Value
2) Stored Procedure can return many value or not return
3) Functions Must return a value, procedures doesn't need to
4) Functions can be used within a Stored Procedure but stored procedures cannot be used within a function
--* How to kill session?
use session.abandont method
--* What are the different types of cookies
1) Persistent Cookies.
2) Non Persistent CookiesPersistent cookies are those which is storing in cookies folder in hard disk.Non persistent cookies is created on memory, inside the browser process. When we close the browser, that memory region will collect by Operating system, and the cookie will also deleted.
--* Write the HTML for a hyperlink that will send mail when the user clicks the link.
Send mail
1) Dataset.clone() --> Copy only structure (Returns a DataSet with the same structure (tables andrelationships) but no data.)
2) Dataset.copy() --> Copy Structure & Data Both (Returns an exact duplicate of the DataSet, with the same set of tables, relationships, and data.)
--* What is the difference between autopostback and ispostback?
1) Autopostback - Property of the control
2) IsPostback - Property of the Page class
--* What is the difference between ExcuteQuery and ExcuteNonQuery ?
1) ExcuteQuery used with select statement
2) ExcuteNonQuery used with insert ,update ,delete statement
3) ExcuteNonQuery return number of row affected
4) ExcuteQuery return one or more row of data
--* What is Tracing :
Trace in ASP.Net is nothing but to trace error and It will also give some details of the error, so that the user can easily debug the code.
--* Types of Tracing :
1) Application Level Tracing
2) Page Level Tracing
--* Different types of style sheets:
1) External
2) Inline
--* Viewstate : stores the state of controls in HTML hidden fields.
--* Can we run asp.net apllication without WEB.CONFIG file?
YES , Because all the configuration settings will be available under MACHINE.CONFIG file
--* Session state is global to your entire application for the current user. Session state can be lost in several ways:
1) If the user closes and restarts the browser.
2) If the user accesses the same page through a different browser window, although the session will still exist if a web page is accessed through the original browser window. Browsers differ on how they handle this situation.
3) If the session times out because of inactivity. By default, a session times out after 20 idle minutes.
4) If the programmer ends the session by calling Session.Abandon().
--* A transactionis:- a set of operations that must either succeed or fail as a unit. The goal of a transaction is to ensure that data is always in a valid
--* Types of Data Source in the.NET Framework:
1) SqlDataSource: This data source allows you to connect to any data source that has an ADO.NET data provider
2) ObjectDataSource: This data source allows you to connect to a custom data access class
3) XmlDataSource : This data source allows you to connect to an XML file
4) SiteMapDataSource: This data source allows you to connect to the Web.Sitemap file that describes the navigational structure of your website
--* Types of ADO.Net Data Provider:
1) System.Data.SqlClient
2) System.Data.OracleClient
3) System.Data.OleDb
4) System.Data.Odbc
--* What are types of sub queries :
1) Single row subquery
2) Multiple row subquery
3) Multiple column subquery
--* What command do we use to rename a Database sp_renamedb ‘oldname’ , ‘newname’
--* What is the command that we use to install IIS after you installed ASP.NET aspnet_regsql -i
--* What is the purpose of @@Identety:
Returns the last identity value inserted as a result of the last INSERT or SELECT INTO statement.
--* Describe the difference between inline code and code behind.
1) Inline code written along side the html in a page.
2) Code-behind is code written in seprate file and refrenced by .aspx in page.
--* What is the difference between System.Array.CopyTo and System.Array.Clone in .NET?
1) The Clone() method returns a new array object containing all the elements in the original array.
2) The CopyTo() method copies the elements into another existing array.
--* What is Encapsulation?
Encapsulation - is the ability of an object to hide its data and methods from the rest of the world. It is one of the fundamental principles of OOPs
--* Who is a protected class-level variable available to?
It is available to any sub-class (a class inheriting this class).
--* Describe the accessibility modifier “protected internal”.
It is available to classes that are within the same assembly and derived from the specified base class
--* What’s the difference between the Debug class and Trace class?
1) Use Debug class for debug builds,
2) use Trace class for both debug and release builds
--* Explain the differences between public, protected, private and internal.
1) Public: Allows class, methods, fields to be accessible from anywhere i.e. within and outside an assembly.
2) Private: When applied to field and method allows to be accessible within a class.3) Protected: Similar to private but can be accessed by members of derived class also.
4) Internal: They are public within the assembly i.e. they can be accessed by anyone within an assembly but outside assembly they are not visible.
--* List the event handlers that can be included in Global.asax?
1) Application start and end event handlers
2) Session start and end event handlers
3) Per-request event handlers
4) Non-deterministic event handlers
--* What’s a strong name?
A strong name includes: the name of the assembly, version number, culture identity, and a public key token.
--* What is constructor.
Constructor is a method in the class which has the same name as the class It initialises the member attributes whenever an instance of the class is created.
--* What is difference between CHAR and VARCHAR:
1) CHAR pads blank spaces to the maximum length.
2) VARCHAR2 does not pad blank spaces.
3) For CHAR the maximum length is 255 and 2000 for VARCHAR
--* What are the four types of events ?
1. System Events.
2. Control Events
3. User Events
4. Other Events
--* How many Global.asax files can an Application have?
Only one Global.asax file and it’s placed in the virtual directory’s root
--* What is a session?
A user accessing an application is known as a session.
--* Different between Local and Global Variable:
1) A variable is global if it is declared outside of the main program block and not within a function
2) A variable is local if it is declared between the function declaration
3) Global variables are visible and available to all statements in a setup script that follow its declaration
4) Local variables are visible and available only within the function where they are declared.
--* Difference Between EXE and DLL file :
1) Exe is the Application
2) DLL is the Component
3) Exe can execute on its own
4) DLL Can not execute on its own
5) EXE can run independently
6) DLL will run within an Exe
7) EXE is an out-process file
8) DLL is an in-process file
--* Difference between Strong and Weak type
1) Strong type: Checking the types of variables at compile time
2) Weak type: Checking the types of variables at run time.
--* Difference between Function and Stored Procedure:
1) Function returns only one Value
2) Stored Procedure can return many value or not return
3) Functions Must return a value, procedures doesn't need to
4) Functions can be used within a Stored Procedure but stored procedures cannot be used within a function
--* How to kill session?
use session.abandont method
--* What are the different types of cookies
1) Persistent Cookies.
2) Non Persistent CookiesPersistent cookies are those which is storing in cookies folder in hard disk.Non persistent cookies is created on memory, inside the browser process. When we close the browser, that memory region will collect by Operating system, and the cookie will also deleted.
--* Write the HTML for a hyperlink that will send mail when the user clicks the link.
Send mail
Subscribe to:
Comments (Atom)

