.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> ASP.NET >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Bind combo using common function

Posted By:shankey       Posted Date: April 29, 2011    Points: 200    Category: ASP.NET    URL: http://www.dotnetspark.com  

Bind combo using common function which uses Optional and default parameter
 

Bind combo using common function which uses Optional and default parameter

Main screen




Sort in ascending order



Sort in descending order



Add new row (Custom row)



·  Entered item is added to the combo box



·  Verifying whether entered item value is set as option value



Set Selected item based on text entered





  Based on entered text, Item is set selected in combo box


Set Selected item based on value entered


Full C# code of the above example

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace BindComboWithCommonFunction
{
    public partial class Default : System.Web.UI.Page
    {
        public enum SortOrder
        {
            Ascending = 0,
            Descending
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("Months");
                dt.Columns.Add("Value");

                dt.Rows.Add("January", "1");
                dt.Rows.Add("February", "2");
                dt.Rows.Add("March", "3");
                dt.Rows.Add("April", "4");
                dt.Rows.Add("May", "5");
                dt.Rows.Add("June", "6");
                dt.Rows.Add("July", "7");
                dt.Rows.Add("August", "8");
                dt.Rows.Add("September", "9");
                dt.Rows.Add("October", "10");
                dt.Rows.Add("November", "11");
                dt.Rows.Add("December", "12");
                FillCombo(ddl, dt, "Months", "Value");
            }
        }
        #region

        public static void FillCombo(DropDownList dropDown, DataTable dtDropDown, string dataTextField, string dataValueField, int selectedItemValue = 0, string selectedItemText = "", bool isSort = false, SortOrder sortExpression = SortOrder.Ascending, bool addCustomFirstRow = false, string customRowText = "Please select...", string customRowValue = "0")
        {

            if (dropDown != null && dtDropDown != null)
            {
                dropDown.Items.Clear();

                if (dtDropDown.Rows.Count > 0)
                {
                    if (isSort)
                    {
                        string sordOrder = (SortOrder.Ascending == sortExpression ? " ASC" : " DESC");
                        dtDropDown.DefaultView.Sort = dataTextField + sordOrder;
                    }


                    dropDown.DataTextField = dataTextField;
                    dropDown.DataValueField = dataValueField;

                    dropDown.DataSource = dtDropDown;
                    dropDown.DataBind();
                }


                if (addCustomFirstRow)
                {
                    dropDown.Items.Insert(0, new ListItem(customRowText, customRowValue));
                }

                if (selectedItemValue != 0)
                {
                    dropDown.Items.FindByValue(Convert.ToString(selectedItemValue)).Selected = true;

                }
                if (!String.IsNullOrEmpty(selectedItemText))
                {
                    dropDown.Items.FindByText(selectedItemText).Selected = true;
                }

            }

        }
        public static void FillCombo(DropDownList dropDown, DataTable dtDropDown, string dataTextField, string dataValueField, bool isSort, SortOrder sortExpression)
        {
            FillCombo(dropDown, dtDropDown, dataTextField, dataValueField, 0, "", isSort, sortExpression);
        }
        public static void FillCombo(DropDownList dropDown, DataTable dtDropDown, string dataTextField, string dataValueField, bool addCustomFirstRow, string customRowText, string customRowValue)
        {
            FillCombo(dropDown, dtDropDown, dataTextField, dataValueField, 0, "", false, SortOrder.Ascending, addCustomFirstRow, customRowText, customRowValue);
        }
        public static void FillCombo(DropDownList dropDown, DataTable dtDropDown, string dataTextField, string dataValueField, int selectedItemValue)
        {
            FillCombo(dropDown, dtDropDown, dataTextField, dataValueField, selectedItemValue,"");
        }
        public static void FillCombo(DropDownList dropDown, DataTable dtDropDown, string dataTextField, string dataValueField, String selectedItemText)
        {
            FillCombo(dropDown, dtDropDown, dataTextField, dataValueField, 0, selectedItemText);
        }
        #endregion

        protected void btnAscending_Click(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Months");
            dt.Columns.Add("Value");

            dt.Rows.Add("January", "1");
            dt.Rows.Add("February", "2");
            dt.Rows.Add("March", "3");
            dt.Rows.Add("April", "4");
            dt.Rows.Add("May", "5");
            dt.Rows.Add("June", "6");
            dt.Rows.Add("July", "7");
            dt.Rows.Add("August", "8");
            dt.Rows.Add("September", "9");
            dt.Rows.Add("October", "10");
            dt.Rows.Add("November", "11");
            dt.Rows.Add("December", "12");
            FillCombo(ddl, dt, "Months", "Value", true, SortOrder.Ascending);
        }

        protected void btnDescending_Click(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Months");
            dt.Columns.Add("Value");

            dt.Rows.Add("January", "1");
            dt.Rows.Add("February", "2");
            dt.Rows.Add("March", "3");
            dt.Rows.Add("April", "4");
            dt.Rows.Add("May", "5");
            dt.Rows.Add("June", "6");
            dt.Rows.Add("July", "7");
            dt.Rows.Add("August", "8");
            dt.Rows.Add("September", "9");
            dt.Rows.Add("October", "10");
            dt.Rows.Add("November", "11");
            dt.Rows.Add("December", "12");
            FillCombo(ddl, dt, "Months", "Value", true, SortOrder.Descending);
        }

        protected void btnAddCustomRow_Click(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Months");
            dt.Columns.Add("Value");

            dt.Rows.Add("January", "1");
            dt.Rows.Add("February", "2");
            dt.Rows.Add("March", "3");
            dt.Rows.Add("April", "4");
            dt.Rows.Add("May", "5");
            dt.Rows.Add("June", "6");
            dt.Rows.Add("July", "7");
            dt.Rows.Add("August", "8");
            dt.Rows.Add("September", "9");
            dt.Rows.Add("October", "10");
            dt.Rows.Add("November", "11");
            dt.Rows.Add("December", "12");
            if (txtText.Text.Trim() != "" && txtValue.Text.Trim() != "")
                FillCombo(ddl, dt, "Months", "Value", true, txtText.Text.Trim(), txtValue.Text.Trim());
            else
                FillCombo(ddl, dt, "Months", "Value");
        }

        protected void btnSetSelectedItemText_Click(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Months");
            dt.Columns.Add("Value");

            dt.Rows.Add("January", "1");
            dt.Rows.Add("February", "2");
            dt.Rows.Add("March", "3");
            dt.Rows.Add("April", "4");
            dt.Rows.Add("May", "5");
            dt.Rows.Add("June", "6");
            dt.Rows.Add("July", "7");
            dt.Rows.Add("August", "8");
            dt.Rows.Add("September", "9");
            dt.Rows.Add("October", "10");
            dt.Rows.Add("November", "11");
            dt.Rows.Add("December", "12");
            if (txtSelectedText.Text.Trim() != "" && txtSelectedText.Text.Trim() != "")
                FillCombo(ddl, dt, "Months", "Value", txtSelectedText.Text.Trim());
            else
                FillCombo(ddl, dt, "Months", "Value");
        }

        protected void btnSetSelectedItemValue_Click(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Months");
            dt.Columns.Add("Value");

            dt.Rows.Add("January", "1");
            dt.Rows.Add("February", "2");
            dt.Rows.Add("March", "3");
            dt.Rows.Add("April", "4");
            dt.Rows.Add("May", "5");
            dt.Rows.Add("June", "6");
            dt.Rows.Add("July", "7");
            dt.Rows.Add("August", "8");
            dt.Rows.Add("September", "9");
            dt.Rows.Add("October", "10");
            dt.Rows.Add("November", "11");
            dt.Rows.Add("December", "12");
            if (txtSelectedValue.Text.Trim() != "" && txtSelectedValue.Text.Trim() != "")
            {
                if (Convert.ToInt32(txtSelectedValue.Text.Trim()) > 0 && Convert.ToInt32(txtSelectedValue.Text.Trim()) <= 12)
                    FillCombo(ddl, dt, "Months", "Value", Convert.ToInt32(txtSelectedValue.Text.Trim()));
                else
                    FillCombo(ddl, dt, "Months", "Value");
            }
            else
            {
                FillCombo(ddl, dt, "Months", "Value");
            }
        }
    }
}

HTML Code of the above example

 
Sorting    
Add Custom Row Text:
Value:
Selected Item Text:
Selected Item Value:
Months

You can download the sample project of the above example


 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend