Friday 31 October 2008

Sunday 26 October 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

Share:

Friday 17 October 2008

ASP.NET MVC Beta released

Click here to download it.  You can also visit www.asp.net/mvc to explore tutorials, quickstarts, and videos to learn more.

The ASP.NET MVC Beta works with both .NET 3.5 and .NET 3.5 SP1, and supports both VS 2008 and Visual Web Developer 2008 Express SP1 (which is free - and now supports class libraries and web application project types).

Here's what's new in ASP.NET MVC Beta:

Also, jQuery now integrated with ASP.NET MVC! When you go File | New ASP.NET MVC Project, you've already got jQuery.

If you was already working with ASP.NET Preview releases the you may not need these lines in your web.config file anymore

<system.web>

<pages>

<namespaces>
                <add namespace="System.Web.Mvc"/>
                <add namespace="System.Web.Mvc.Ajax"/>
                <add namespace="Microsoft.Web.Mvc"/> 
</namespaces>

.....................................

</pages>

</system.web>

This is just one of my findings, but I would like to hear more, what other changes may require if I am already using Preview Releases.

.

Share:

Saturday 11 October 2008

SQL Server Coding Standards

ASP.NET, C# or VB.NET often goes hands in hands, In my previous post I have given the C# Coding Standards, but without the correct SQL Server Coding standards, my previous post was almost incomplete, so in this post I am giving you the Link to SQL Server Coding Standards.

You can get more details in Pinal Dave SQLAuthority.com, http://blog.sqlauthority.com/2007/06/06/sql-server-database-coding-standards-and-guidelines-complete-list-download/

This post by Pinal Dave consists of Series of Database Coding Standards and Guidelines.

SQL SERVER Database Coding Standards and Guidelines - Introduction
SQL SERVER - Database Coding Standards and Guidelines - Part 1
SQL SERVER - Database Coding Standards and Guidelines - Part 2


SQL SERVER Database Coding Standards and Guidelines Complete List Download

Share:

Friday 10 October 2008

C# Coding Standards

Following the right Coding standars comes with Practice and proper Guidance if you are a starter, and also lots of companies define or customize their own coding standards.

When I came across this challange to define the coding standard for my team, I thought instead of reinventing the wheel and recollecting everything from my experience, better if I get something which is already defined and documented. So I started googling and came across a very good document which covers almost all the aspects of C# Coding standards which I was looking for. This Document covers the following topics*

  • Cover all major C# Language features.
  • Provide guidelines on coding style & language usage (but not syntax)
  • Demonstrate all rules in code where applicable.
  • Describe rules in structure that is easy to read & use.
  • Define clear & concise rules.
  • Use consistent terminology & rule patterns.
  • Only provide rules where there is a clear cut best practice.
  • Lead developers to a "pit of success" and avoid common "pits of failure"
  • You can download the PDF from here http://weblogs.asp.net/lhunt/attachment/591275.ashx

    Original Post you can find here : http://weblogs.asp.net/lhunt/pages/CSharp-Coding-Standards-document.aspx

    Thanks

    ~Brij

    * All the Copyrights in this Post are the property of the Respective Owner.

    Share: