2/26/12

Extend web control with an extension method


When using Label and TextBox controls, we often have the need to reset the text value and/or hide the control during a post back. A common example is when using a Label control to display messages to the user. We only need to set the message once after a postback. We may then need to reset the content of that message and not display the control all together on subsequent postbacks.  

To avoid repeating the same lines of codes, we can extent the web controls by adding an extension method that would handle that for us.

public static class ogUIExtensions
{
       
       /// <summary>
       /// resets the text control properties and hide
       /// </summary>
       /// <param name="ctrl"></param>
       public static void ResetAndHide(this WebControl ctrl)
       {
            ctrl.Visible = false;
            (ctrl as ITextControl).Text = string.Empty; 
       }
}

This snippet extends the WebControl class by adding a method that basically sets the Visible property to false (hide) and sets the Text property of the control to an empty string. The WebControl class has not Text property, so we need to upcast to the ITextControl interface to have access to that property. Both Label and TextBox controls implement the ITextControl interface.

We can use this extension method in this way:

Label lbl = new Label();
lbl.ResetAndHide();

I hope it helps.
og-bit.com

2/25/12

Select/UnSelect all items from CheckBoxList, DropDownList, ListBox with an Extension Method

The CheckBoxList, DropDownList and ListBox controls have an items collection based on the ListItemCollection class. To select/unselect the items on the list, we can iterate through the items in the collection and set the selected attribute to true or false.

The following code snippet shows an extension method that implements a common solution for all these controls. The method allows us to extend the functionality of these controls, so we are able to set the selected attribute with just one call.


public static class ogUIExtensions
    {
        /// <summary>
        /// extension method for all ListItemCollection controls to set the selected state
        /// </summary>
        /// <param name="list"></param>
        /// <param name="state"></param>
        public static void SelectAll(this ListItemCollection list, bool state)
        {          
            foreach (ListItem item in list)
            {
                item.Selected = state;
            }
        }
    }


Whenever, we need to set the selected state of any of these controls, we just need to call the method as shown below:

CheckBoxList ckList = new CheckBoxList();
ckList.Items.SelectAll(true); //checks all the items
ckList.Items.SelectAll(false);//unchecks all the items

The same can be done for ListBox and DropDownList controls.
Hope it helps.

2/22/12

Insert Multiple Records in One Transaction

If you want to insert several rows into a table in one transaction, you can use the Select into statement with a combination of a select union statement. For an example, you can look at the following sample:

INSERT INTO table1(col1, col2)
SELECT 'one' ,1
UNION ALL
SELECT 'two' ,2
UNION ALL
SELECT 'three' ,3




The union statements result in three rows which are returned with two columns. The result set is then inserted as one transaction into table1. If any of the rows has any constraint violations, the entire transaction is not committed.

If you are using SQL Server 2008, you can use this syntax:

INSERT INTO table1(col1, col2)
values ('one' ,1),
       ('two' ,'osc'),
       ('three' ,3)



That also allows us to insert multiple records without using a union statement. If any of the values fails, none of the rows are inserted.