Wednesday, December 31, 2008

Happy New Year 2009 !!!

I wish a Very Happy New Year 2009  to all the readers of http://www.dotnetglobe.com  !!!

May this coming new year will bring you success, joy and a great future ahead.

 

Cheers

~Brij Mohan

Thursday, December 25, 2008

Microsoft launches DreamSpark: Indian students get access to technical software at no charge

- Access to software developer and designer tools to students at no charge

- DreamSpark includes:

o Visual Studio 2005/2008 Professional Edition,

o Expression Studio (includes Web, Blend, Media and Design),

o SQL Server 2005 Express, SQL Server 2005 Developer Edition,

o Windows Server 2008 Standard Edition,

o XNA Game Studio 2.0 and

o 12-month trial academic subscription to the XNA Creators Club

- Available online on www.dreamsparkindia.com; and via DVDs at NIIT, Aptech and Hughes Net Fusion Centers

Thursday, December 4, 2008

CSS Browser Selector

While designing the webpage we often struggle to maintain the consistency of our site across different browsers. To achieve this we use different browser hacks. But again that ends up with making our programming more complicated.

In this post I am going to give you the link of a JavaScript using which you just need to know basics of CSS.

CSS Browser Selector is a very small JavaScript with just one line and less than 1kb which empower CSS selectors. It gives you the ability to write specific CSS code for each operating system and each browser.

This can work for the following Browsers

  • Internet Explorer (All versions)
  • Mozilla, Firefox (all versions), Camino
  • Opera (All versions)
  • Konqueror
  • Safari, NetNewsWire, OmniWeb, Shiira, Google Chrome

I think we have almost covered all the browsers right, if I am missing any let me know.

Instead of writing everything again here, and making this post lengthy I will suggest you to visit this site to download the JavaScript code and instructions on how to use this from here.

Saturday, November 22, 2008

LINQ to XML and LINQ to Objects Basic Sample

In this post I will show how to use LINQ to XML and LINQ to Objects, very basic example with sample code.

image  image

First I have created a XML file which contains the Customer Details, as given below

<?xml version="1.0" encoding="utf-8" ?>
<
customers>
    <
customer>
        <
customerid>ALFKI</customerid>
        <
city>Berlin</city>
        <
age>20</age>
    </
customer>
    <
customer>
        <
customerid>BONAP</customerid>
        <
city>Marseille</city>
        <
age>21</age>
    </
customer>
    <
customer>
        <
customerid>CONSH</customerid>
        <
city>London</city>
        <
age>30</age>
    </
customer>
    <
customer>
        <
customerid>EASTC</customerid>
        <
city>London</city>
        <
age>34</age>
    </
customer>
    <
customer>
        <
customerid>FRANS</customerid>
        <
city>Torino</city>
        <
age>35</age>
    </
customer>
    <
customer>
        <
customerid>LONEP</customerid>
        <
city>Portland</city>
        <
age>40</age>
    </
customer>
    <
customer>
        <
customerid>NORTS</customerid>
        <
city>London</city>
        <
age>25</age>
    </
customer>
    <
customer>
        <
customerid>THEBI</customerid>
        <
city>Portland</city>
        <
age>36</age>
    </
customer>
</
customers>

 

I have also created a class with the same properties which I have given in the XML document above, and populating the same data,

public class Customer
{
public string CustomerID { get; set; }
public string City { get; set; }
public int Age { get; set; }
public static IEnumerable<Customer> CreateCustomers()
{
   return new List<Customer>
   {
      new Customer { CustomerID = "ALFKI", City = "Berlin", Age=20   },
      new Customer { CustomerID = "BONAP", City = "Marseille" , Age=21},
      new Customer { CustomerID = "CONSH", City = "London", Age=30    },
      new Customer { CustomerID = "EASTC", City = "London", Age=34    },
      new Customer { CustomerID = "FRANS", City = "Torino", Age=35    },
      new Customer { CustomerID = "LONEP", City = "Portland", Age=40  },
      new Customer { CustomerID = "NORTS", City = "London" , Age=25   },
      new Customer { CustomerID = "THEBI", City = "Portland", Age=26  }    
   };
 }
}

Now I have got the Object ready to query using LINQ with the Same data which I have in my XML Document.

Note above how I'm using the new "Automatic Properties and Object Initializers" feature of C# to define the properties (and avoid having to define a field for them and initialize the Objects). And In all the functions below I am reading the XML Document, using XDocument Class within System.Xml.Linq namespace to open and query document.

First I have to populate the DropDownList in the UI, to display all the Cities, I am reading the Cities from the object alternatively you can also read the same from XML document, both will return the same result. This DropDown will allow the users to select the customers located in the selected City.

public static List<Customer> GetCities()
{
     var customers = from customer in Customer.CreateCustomers()
                     orderby customer.City
                     select new Customer { City = customer.City };
     return customers.ToList();
}

I have also added one more DropDown to let the user to select the DataSource (XML or Object), once both the selection is made, users can click on Get Customers to Get the Customer details. Depending on the DataSource and City, the function below will return the List of Customers wither from XML or the Object.

 

public static List<Customer> GetCustomerFromXML(string city)
{
    XDocument xmlDoc = XDocument.Load(HttpContext.Current.Server.MapPath("CustomerXML.xml"));
    var customers = from customer in xmlDoc.Descendants("customer")
                    where customer.Element("city").Value == city
                    select new Customer
                    {
                        CustomerID = customer.Element("customerid").Value,
                        City = customer.Element("city").Value,
                        Age = Convert.ToInt32(customer.Element("age").Value)
                    };
    return customers.ToList();
}

Similarly I am writing the function below to read the object, and returning the List<Customer> which we can bind directly to the GridView.

public static List<Customer> GetCustomersFromObject(string city)
{
     var customers = from customer in Customer.CreateCustomers()
                     where customer.City == city
                     select new Customer
                     {
                         CustomerID = customer.CustomerID,
                         City = customer.City,
                         Age = customer.Age
                     };
     return customers.ToList();
 }

Note: In the functions above the only trick I've made to the LINQ to XML query is to use the "select new Customer" instead of only "select" clause from "select new" (with no type-name).  With this trick I'm returning a sequence of Customer objects that I can pass from class to class, assembly to assembly, and across web-services.

In the code behind of ASPX Page first I have populated the Cities DropDown to List all the cities on Page_Load, then OnClick of the Get Customers  I have written the code to Get the City, and the DataSource from the DropDownList and Call the appropriate method depending on the DataSource.

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        //get all the cities
        ddlCity.DataSource = Customer.GetCities();
        ddlCity.DataTextField = "city";
        ddlCity.DataValueField = "city";
        ddlCity.DataBind();
    }
}
protected void btnGetCustomers_Click(object sender, EventArgs e)
{
    //binding the Customer from Object
    if (ddlSource.SelectedValue == "Object")
    {
        gvCustomerDetails.Visible = true;
        lblMessage.Text = "Displaying data from Object";
        gvCustomerDetails.DataSource = Customer.GetCustomersFromObject(ddlCity.Text);
        gvCustomerDetails.DataBind();
    }
    //binding the Customer from XML
    else if (ddlSource.SelectedValue == "XML")
    {
        gvCustomerDetails.Visible = true;
        lblMessage.Text = "Displaying data from XML";
        gvCustomerDetails.DataSource = Customer.GetCustomerFromXML(ddlCity.Text);
        gvCustomerDetails.DataBind();
    }
    else
    {
        gvCustomerDetails.Visible = false;
        lblMessage.Text = "Please select the Data Source";
    }
}

Below is my ASPX code which is very much self explanatory, so I have not given much explanation for that, this just for the control name reference.

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <h3>
        LINQ to Object and LINQ to XML Demo</h3>
    <div>
        <table>
            <tr>
                <td>
                    <label for="source" id="lblSource">
                        Data Source :</label>
                </td>
                <td>
                    <asp:DropDownList ID="ddlSource" runat="server">
                        <asp:ListItem Text="Select Source" Value="0" Selected="True"></asp:ListItem>
                        <asp:ListItem Text="XML" Value="XML"></asp:ListItem>
                        <asp:ListItem Text="Object" Value="Object"></asp:ListItem>
                    </asp:DropDownList>
                </td>
                <td></td>
            </tr>
            <tr>
                <td>
                    <label for="empid" id="lblEmp">
                        Select City :</label>
                </td>
                <td>
                    <asp:DropDownList runat="server" ID="ddlCity"></asp:DropDownList>
                </td>
                <td><asp:Button runat="server" ID="btnGetCustomers" Text="Get Customers" 
                        onclick="btnGetCustomers_Click" /></td>
            </tr>
        </table>
        <br />
        <asp:Label runat="server" ID="lblMessage"></asp:Label>
        <br />
        <asp:GridView ID="gvCustomerDetails" runat="server">
        </asp:GridView>
    </div>
    </form>
</body>
</html>
 

You can download the sample code from here.

You can refer my previous posts, to see the Example of LINQ To SQL.

You can see the well formatted code in my ASP.NET Blog.

 

For more knowledge you can refer the post below.

http://weblogs.asp.net/scottgu/archive/2007/08/07/using-linq-to-xml-and-how-to-build-a-custom-rss-feed-reader-with-it.aspx

http://www.c-sharpcorner.com/UploadFile/scottlysle/L2OinCS05242008233051PM/L2OinCS.aspx

Monday, November 10, 2008

uCertify - The fastest way to IT Certification

This is one of the best Preparation Kit for exam 70-528 I found in the market, equipped with all the tools which candidate needs.

I was just thinking where I can say this needs improvement, so I went through entire test from all the different test modes, instead I discovered lot more good things, like this not only provided good explanation of the correct as well as incorrect answers but also I found uCertify has provided 'Fact' in learn and test mode, which is very helpful.

We can not only bookmark the question but also rate the question, we can also Add the Tags for the Question to which we feel more important of can help later on.

This has also provided MSDN Reference, where I can get more details if I want to get into more in-depth

And needless to say, the best part of uCertify I found is Test History and Readiness Report,

There are other lots of good features which I have not mentioned here, but I strongly recommend uCertify, for those who is aspirant for Microsoft .NET certifications.

Web : http://www.ucertify.com

Sunday, November 2, 2008

TwoSelect User Control, Moving Items between ListBox Controls - Part 2

As I promised in this post I am showing you how to implement TwoSelect Control, which I created in my Previous Post

You can download the Complete Source with implementation from here

In this post to get the Available Users and Added Users I am fetching from DataBase using LINQ to SQL, but you can also use either sql or Stored Procedure as per your convenience.

So the first step in Consuming this control in our code is by Creating the Tables, whose structure will look something like this

image

In the User Table I have stored all the User Information, in Group Table I have stored all the Group Details and in UserGroup Table I have related the User and Groups.

In the current project I have not given any screen to enter the User Details or Group Details, as this was out of scope of the current topics, so you can insert the User and Group directly in the Database Tables.

Secondly, Now coming to .NET Code, first I have created a LINQ to SQL and named this as UserGroupData.dbml, and added all the three tables into the designer.

After this I have added the Class to Handle the Data from LINQ to SQL and pass the data to Presentation Layer.

 

In the below given code snippet I am Retrieving all the User who is already present in the Group, this function accepts the groupId as input parameter and return IDataReader to the Presentation Layer, so that we can pass the DataReader directly to the DataSourceAdded Property of the TwoSelect Control and Bind the Data to the Added Users ListBox.

Code Snippet

1:   /// <summary>
2:  /// Gets the group user.
3:  /// </summary>
4:  /// <param name="groupId">The group id.</param>
5:  /// <returns>IDataReader</returns>
6:  public IDataReader GetGroupUser(int groupId)
7:   {
8:   UserGroupDataDataContext data = new UserGroupDataDataContext();
9:   var user = from Grp in data.Groups
10:   join UsrGrp in data.UserGroups on Grp.GroupId equals UsrGrp.GroupId
11:   join Usr in data.Users on UsrGrp.UserId equals Usr.UserId
12:   where Grp.GroupId == groupId
13:   orderby Usr.UserId
14:   select new
15:  {
16:   UserName = Usr.UserName,
17:   GroupName = Grp.GroupName,
18:   UserId = Usr.UserId
19:   };
20:   IDbCommand command = data.GetCommand(user);
21:   command.Connection = data.Connection;
22:   if(command.Connection.State == ConnectionState.Closed)
23:   command.Connection.Open();
24:   IDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
25:   return reader;
26:   }

 

In the below given code snippet I am Retriving all the User who not present in the Group, this function accepts the groupId as input parameter and return IDataReader to the Presentation Layer, so that we can pass the DataReader directly to the DataSourceAvailable Property of the TwoSelect Control and Bind the Data to the Available Users ListBox.

Code Snippet

1:   /// <summary>
2:  /// Users not in the selected group.
3:  /// </summary>
4:  /// <param name="groupId">The group id.</param>
5:  /// <returns>IDataReader</returns>
6:  public IDataReader UserNotInGroup(int groupId)
7:   {
8:   UserGroupDataDataContext data = new UserGroupDataDataContext();
9:   var user = from Grp in data.Groups
10:   join UsrGrp in data.UserGroups on Grp.GroupId equals UsrGrp.GroupId
11:   join Usr in data.Users on UsrGrp.UserId equals Usr.UserId
12:   where Grp.GroupId != groupId
13:   orderby Usr.UserId
14:   select new
15:  {
16:   UserName = Usr.UserName,
17:   GroupName = Grp.GroupName,
18:   UserId = Usr.UserId
19:   };
20:   IDbCommand command = data.GetCommand(user);
21:   command.Connection = data.Connection;
22:   if(command.Connection.State == ConnectionState.Closed)
23:   command.Connection.Open();
24:   IDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
25:   return reader;
26:   }

 

The function below will get all the Group names and Bind the Group DropDown, to display all the Groups so that we can select the Groups whose user I want to manage.

Code Snippet

1:   /// <summary>
2:  /// Gets all group.
3:  /// </summary>
4:  /// <returns>IDataReader</returns>
5:  public IDataReader GetAllGroup()
6:   {
7:   UserGroupDataDataContext data = new UserGroupDataDataContext();
8:   var groups = from Grp in data.Groups
9:   orderby Grp.GroupName
10:   select new
11:   {
12:   GroupId = Grp.GroupId,
13:   GroupName = Grp.GroupName
14:   };
15:   IDbCommand command = data.GetCommand(groups);
16:   command.Connection = data.Connection;
17:   if (command.Connection.State == ConnectionState.Closed)
18:   command.Connection.Open();
19:   IDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);

20:   return reader;
21:   }

 

This function will update the DataBase once we have selected the Users in a particular Group.

Code Snippet

1:   /// <summary>
2:  /// Saves the group user.
3:  /// </summary>
4:  /// <param name="group">The group.</param>
5:  public void SaveGroupUser(List<UserGroup> usrGroup)
6:   {
7:   UserGroupDataDataContext data = new UserGroupDataDataContext();

8:   //first delete all the existing user data from User Group
9:  foreach (UserGroup match in usrGroup)
10:   {
11:   var deleteUserGroup = from userGroups in data.UserGroups
12:   where userGroups.UserId == match.UserId
13:   select userGroups;
14:   foreach (var userGroup in deleteUserGroup)
15:   data.UserGroups.DeleteOnSubmit(userGroup);

16:   data.SubmitChanges();
17:   }

18:   //MISSING : Logic to implement Save new Data related to User and Group
19: 
20:   }

 

The same things you can do without using the LINQ to SQL either by writing the Stored Procedure or using the SQL Queries.

I am giving below the sample stored procedure which can replace the function GetGroupUser and GetUserNoInGroup

  • GetGroupUser
   1:  SET ANSI_NULLS ON
   2:  GO
   3:  SET QUOTED_IDENTIFIER ON
   4:  GO
   5:  CREATE PROCEDURE [dbo].[GetGroupUser]
   6:      
   7:      (
   8:      @GroupId int
   9:      )
  10:      
  11:  AS
  12:      SET NOCOUNT ON 
  13:      
  14:      SELECT 
  15:          Usr.UserName, 
  16:          Grp.GroupName,
  17:          Usr.UserId 
  18:      From  
  19:          GROUPS Grp
  20:      INNER JOIN 
  21:          UserGroup UsrGrp
  22:      ON 
  23:          Grp.GroupId = UsrGrp.GroupId
  24:      INNER JOIN USERS Usr ON
  25:          UsrGrp.UserId = Usr.UserId
  26:      WHERE 
  27:          Grp.GroupId=@GroupId
  28:      ORDER BY Usr.UserId
  29:      
  30:      RETURN

 

  • GetUserNotInGroup
   1:  SET ANSI_NULLS ON
   2:  GO
   3:  SET QUOTED_IDENTIFIER ON
   4:  GO
   5:  ALTER PROCEDURE [dbo].[UserNotInGroup]
   6:      
   7:      (
   8:      @GroupId int
   9:      )
  10:      
  11:  AS
  12:      SET NOCOUNT ON
  13:      SELECT 
  14:          Usr.UserName,
  15:          Usr.UserId, 
  16:          Grp.GroupName 
  17:      From  
  18:          GROUPS Grp
  19:      INNER JOIN 
  20:          UserGroup UsrGrp
  21:      ON 
  22:          Grp.GroupId = UsrGrp.GroupId
  23:      INNER JOIN USERS Usr ON
  24:          UsrGrp.UserId = Usr.UserId
  25:      WHERE 
  26:          Grp.GroupId!=@GroupId
  27:      ORDER BY Usr.UserId
  28:      
  29:      RETURN

 

 

Third, I have included the UserControl in my Project, and added in Default.asp page simply by Drag and Drop.

Also I have added one DropDown Control to get the List of all the Groups and Added a Save Button to Save the Changes back to the database.

After designing the Page will look something similar to this

image

 
 

Now in Code Behind I tried to implement only the logic which is related to the TwoSelect User controls,

Code Snippet

1:   public partial class _Default : System.Web.UI.Page
2:  {
3:   UserData data = new UserData();

4:   private int GetGroupId
5:   {
6:   get { return Convert.ToInt32(ddlGroup.SelectedValue); }
7:   }
8:   /// <summary>
9:  /// Handles the Load event of the Page control.
10:  /// </summary>
11:  /// <param name="sender">The source of the event.</param>
12:  /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
13:  protected void Page_Load(object sender, EventArgs e)
14:   {
15:   if (!IsPostBack)
16:   {
17:   ddlGroup.DataSource = data.GetAllGroup();
18:   ddlGroup.DataTextField = "GroupName";
19:   ddlGroup.DataValueField = "GroupId";
20:   ddlGroup.DataBind();
21:   ListItem item = new ListItem();
22:   item.Text = "Please Select";
23:   item.Value = "0";
24:   ddlGroup.Items.Insert(0,item);
25:   }
26:   }

27:   protected void ddlGroup_SelectedIndexChanged(object sender, EventArgs e)
28:   {
29:   //Bind the User Control Available List Box
30:  uclTwoSelect.DataSourceAvailable = data.GetGroupUser(this.GetGroupId);
31:   uclTwoSelect.DataTextFieldAvailable = "UserName";
32:   uclTwoSelect.DataValueFieldAvailable = "UserId";

33:   //Bind the User Control User Already Added List Box
34:  uclTwoSelect.DataSourceAdded = data.UserNotInGroup(this.GetGroupId);
35:   uclTwoSelect.DataTextFieldAdded = "UserName";
36:   uclTwoSelect.DataValueFieldAdded = "UserId";

37:   uclTwoSelect.BindControl();
38:   }

39:   /// <summary>
40:  /// Saves the User associated with the Groups.
41:  /// </summary>
42:  /// <param name="sender">The source of the event.</param>
43:  /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
44:  protected void btnSave_Click(object sender, EventArgs e)
45:   {
46:   UserGroup user = new UserGroup();
47:   List<UserGroup> group = new List<UserGroup>();
48:   ListItemCollection userGroup = uclTwoSelect.AddedItems;
49:   foreach (ListItem item in userGroup)
50:   {
51:   user.GroupId = Convert.ToInt32(ddlGroup.SelectedValue);
52:   user.UserId = Convert.ToInt32(item.Value);

53:   group.Add(user);
54:   }

55:   data.SaveGroupUser(group);
56:   }
57:   }

You can see in the code above I am binding the DropDown List of the Group and on SelectedIndexChanged even of the DropDown I am Binding the twoSelect Controls Data, with the DataSourceAvailable and DataSourceAdded Properties.

Sorry for the Code Formatting Guys, but you can always refer my asp.net weblog for my neat and clear code formatting.

This is just the one of the implementation you can download the Control and its implementation code from here and Enhance the Control.

 

References

http://www.west-wind.com/WebLog/posts/141435.aspx

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listbox.aspx

http://msdn.microsoft.com/en-us/library/fb3w5b53(VS.85).aspx

 

Thanks

Brij Mohan

Friday, October 31, 2008

.NET Framework 4 Poster

First of all welcome guys to .NET 4.0.
This  a very cool poster that shows off the new stuff in .NET Framework 3.5 SP1 and .NET Framework 4.
Here is the DeepZoom version! (use your mouse wheel or click and control-click to zoom in and out)

Download the .NET Framework 4 Poster PDF for high quality printing.

Enjoy! 

Sunday, October 26, 2008

TwoSelect User Control, Moving Items between ListBox Controls - Part 1

This post will show you how to create TwoSelect User control, basically this control can be used in the scenario where you want to add Users to selected Group. Left side of the ListBox will contain the users which is not present in the group and right side of the ListBox contains the list of users already present in the group.

I have created this as a very Generic control which can be used in other scenarios other then this also. You can customize the Display Text, button captions, some behaviours of the ListBox of this Control.

Also you need to provide the DataSource for the ListBoxes as IDataReader.

 

image

 

 

This control is built in Visual Studio 2008 with Framework 3.5 sp1, but this can be used in Visual Studio 2005 or Framework 2.0 also.

Following are the list of properties which are exposed in this control with their Types

 

   1:  public ListItemCollection AddedItems
   2:   
   3:  public ListItemCollection AvailableItems
   4:   
   5:  public string AvailableItemText
   6:   
   7:  public string AddedItemsText
   8:   
   9:  public string AddAllItemsButtonText
  10:   
  11:  public string AddSelectedItemsButtonText
  12:   
  13:  public string RemoveSelectedItemButtonText
  14:   
  15:  public string RemoveAllItemsButtonText
  16:   
  17:  public ListSelectionMode AvailableListSelectionMode
  18:   
  19:  public ListSelectionMode AddedItemsListSelectionMode
  20:   
  21:  public IDataReader  DataSourceAvailable
  22:   
  23:  public IDataReader DataSourceAdded
  24:   
  25:  public string DataTextFieldAvailable
  26:   
  27:  public string DataValueFieldAvailable
  28:   
  29:  public string DataTextFieldAdded
  30:   
  31:  public string DataValueFieldAdded
 

On click of the 'Add' Button, I am adding the items in the ArrayList and then from ArrayList I am moving from Available Items List Box to Added Items ListBox.

Add OnClick

1:  /// <summary>
2:  /// Add all the selected items from the Available Items to the Added Items
3:  /// </summary>
4:  /// <param name="sender">The source of the event.</param>
5:  /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
6:  protected void btnAdd_Click(object sender, EventArgs e)
7:   {
8:   if (lstAvailable.SelectedIndex >= 0)
9:   {
10:   for (int i = 0; i < lstAvailable.Items.Count; i++)
11:   {
12:   if (lstAvailable.Items[i].Selected)
13:   {
14:   if (!arlList.Contains(lstAvailable.Items[i]))
15:   arlList.Add(lstAvailable.Items[i]);
16:   }
17:   }
18:   for (int i = 0; i < arlList.Count; i++)
19:   {
20:   if (!lstAdded.Items.Contains((ListItem)arlList[i]))
21:   lstAdded.Items.Add((ListItem)arlList[i]);
22:   lstAvailable.Items.Remove((ListItem)arlList[i]);
23:   }
24:   }
25:   }

 

On Click of 'Add All' I am first adding all the items from Available Items to Added Items ListBox, and then clearing all the items from the Available Items ListBox

Code Snippet

1:   /// <summary>
2:  /// Add all the items from the Available items to the Added Items
3:  /// </summary>
4:  /// <param name="sender">The source of the event.</param>
5:  /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
6:  protected void btnAddAll_Click(object sender, EventArgs e)
7:   {
8:   foreach (ListItem list in lstAvailable.Items)
9:   {
10:   lstAdded.Items.Add(list);
11:   }
12:   lstAvailable.Items.Clear();
13:   }

 

On click of the 'Remove' Button, I am adding the items in the ArrayList and then from ArrayList I am moving from Added Items List Box back to Available Items ListBox.

Code Snippet

1:   /// <summary>
2:  /// Moves the Selected items from the Added items to the Available items
3:  /// </summary>
4:  /// <param name="sender">The source of the event.</param>
5:  /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
6:  protected void btnRemove_Click(object sender, EventArgs e)
7:   {
8:   if (lstAdded.SelectedIndex >= 0)
9:   {
10:   for (int i = 0; i < lstAdded.Items.Count; i++)
11:   {
12:   if (lstAdded.Items[i].Selected)
13:   {
14:   if (!arlList.Contains(lstAdded.Items[i]))
15:   arlList.Add(lstAdded.Items[i]);
16:   }
17:   }
18:   for (int i = 0; i < arlList.Count; i++)
19:   {
20:   if (!lstAvailable.Items.Contains((ListItem)arlList[i]))
21:   lstAvailable.Items.Add((ListItem)arlList[i]);
22:   lstAdded.Items.Remove((ListItem)arlList[i]);
23:   }
24:   }
25:   }

 

On Click of 'Remove All' I am first adding all the items from Added Items List Box to Available Items ListBox, and then clearing all the items from the Added Items ListBox.

Code Snippet

1:   /// <summary>
2:  /// Moves all the items from the Added items to the Available items
3:  /// </summary>
4:  /// <param name="sender">The source of the event.</param>
5:  /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
6:  protected void btnRemoveAll_Click(object sender, EventArgs e)
7:   {
8:   foreach (ListItem list in lstAdded.Items)
9:   {
10:   lstAvailable.Items.Add(list);
11:   }
12:   lstAdded.Items.Clear();
13:   }

 

In ASPX code I am adding just basic look and feel which will look like the one which is given in the screen shot at the beginning of the Post, but you can extend this control to customize the look and feel.

 

In this Post, I have given the source code of the Control only, in my next Post I will give the sample implementation of this control, using ASP.NET and LINQ.

 

You can download the Source Code (C#) and Help File from here.

 

Sorry for the Code Formatting Guys, but you can always refer my asp.net weblog for my neat and clear code formatting.

 

Thanks

Brij Mohan