Thursday, June 9, 2016

Make Boostrap columns equal in height using JQuery

I faced issue when we displaying photo gallery, some images are bigger in height and other are small in height, to resolve issue i found out below solution to equalize Bootstrap columns so all images looks same in height

Even when you resize or view in smaller device it will automatically resize Bootstrap columns height


 <script type="text/javascript">
        function equalizeHeight() {
            var row = $('.equalize');
            $.each(row, function () {
                var maxh = 0;
                $.each($(this).find('> div[class^="col-"]'), function () {
                    $(this).css("height", "");
                });
            });
            $.each(row, function () {
                var maxh = 0;
                $.each($(this).find('> div[class^="col-"]'), function () {
                    if ($(this).height() > maxh)
                        maxh = $(this).height();
                });
                $.each($(this).find('> div[class^="col-"]'), function () {
                    $(this).height(maxh);
                });
            });
        }
        $(window).resize(function () {
            equalizeHeight();
        });

        $(document).ready(function () {
            equalizeHeight();
        });
    </script>

How to know which device you are viewing using Bootstrap and JQuery function

Bootstrap automatically detect which device size you are viewing a web page

Bootstrap detect four device sizes
xs - Extra Small
sm - Small
md - Medium
lg - Large


When we develop websites or web applications sometime there may be a situation when you need to identify which device size it is, below is the JQuery code to identify which device size.


<script type="text/javascript">
    function findBootstrapEnvironment() {
        var envs = ['xs', 'sm', 'md', 'lg'];

        var $el = $('<div>');
        $el.appendTo($('body'));

        for (var i = envs.length - 1; i >= 0; i--) {
            var env = envs[i];

            $el.addClass('hidden-' + env);
            if ($el.is(':hidden')) {
                $el.remove();
                return env;
            }
        }
    }
    function fnLeftFilterHideShow() {
        if (findBootstrapEnvironment() == "xs") {
            $('#abc').collapse('hide');
        } else {
            $('#abc').collapse('show');
        }
    }
</script>

How to detect enter key pressed on HTML textbox

Question: How to detect enter key pressed on HTML textbox


Below is HTML syntax, now when user write on textbox we need to track enter key

<input type="text" placeholder="Search"  id="txtSearch" class="search-query form-control">
                    <span class="input-group-btn">
                        <button type="button" id="btnSearch" runat="server" class="btn btn-danger"><span class=" glyphicon glyphicon-search"></span></button>
                    </span>

Solution: you need to add onKeyPress event to the textbox and write javascript function as below

onKeyPress="fnSearchClick(event, this)"

<script type="text/javascript">
    function fnSearchClick(e, textarea) {
        var code = (e.keyCode ? e.keyCode : e.which);
        if (code == 13) { //Enter keycode
            document.getElementById('btnSearch').click();
        }
    }
</script>

Tuesday, June 7, 2016

How to verify RadScriptManager already exist in the ASP.NET Page using C#

I come around a situation where i need to verify that RadScriptManager exists in the page?

This situation occurred when there is Master page and child pages. Some child pages has RadScriptManager added but others has not added RadScriptManager Control in the page

To Resolved this issue and add RadScriptManager into Master Page use below code


if (Telerik.Web.UI.RadScriptManager.GetCurrent(Page) == null) {
 placeHolder.Controls.Add(new Telerik.Web.UI.RadScriptManager());
}

How to export DataTable To .CSV file in ASP.NET C#

To Export DataTable to .CSV file in ASP.NET C# use below function

private void DataTableToCSV(DataTable dt) 
        { 
            string filename = Server.MapPath("~/download.csv"); 
            StreamWriter sw = new StreamWriter(filename, false); 
            int iColCount = dt.Columns.Count; 
            for (int i = 0; i < iColCount; i++) 
            { 
                sw.Write(dt.Columns[i]); 
                if (i < iColCount - 1) 
                { 
                    sw.Write(","); 
                } 
            } 
            sw.Write(sw.NewLine); 
            foreach (DataRow dr in dt.Rows) 
            { 
                for (int i = 0; i < iColCount; i++) 
                { 
                    if (!Convert.IsDBNull(dr[i])) 
                    { 
                        sw.Write(dr[i].ToString()); 
                    } 
                    if (i < iColCount - 1) 
                    { 
                        sw.Write(","); 
                    } 
                } 
                sw.Write(sw.NewLine); 
            } 
            sw.Close(); 
            Response.Clear(); 
            Response.ContentType = "application/csv"; 
            Response.AddHeader("Content-Disposition", "attachment; filename=download.csv"); 
            Response.WriteFile(filename); 
            Response.Flush(); 
            Response.End(); 
        }

LINQ - Perform Inner Join C#

This post show you how to do different Inner joins in C#
1) Simple inner join that join two elements using single key
class Employee
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }

    class EmployeeAddress
    {
        public string Address { get; set; }
        public Employee employee { get; set; }
    }
    class Program
    {
        /// <summary>
        /// Simple inner join.
        /// </summary>
        public static void InnerJoinExample()
        {
            Employee john = new Employee { FirstName = "John", LastName = "Clinton" };
            Employee serry = new Employee { FirstName = "Serry", LastName = "Bond" };


            EmployeeAddress johnAddress = new EmployeeAddress { Address = "1, abc, NJ-07027", employee = john };
            EmployeeAddress serryAddress = new EmployeeAddress { Address = "2, xyz, NJ-08110", employee = serry };


            // Create two lists.
            List<Employee> employees = new List<Employee> { john, serry };
            List<EmployeeAddress> employeeAddresses = new List<EmployeeAddress> { johnAddress, serryAddress };


            var query = from employee in employees
                        join employeeAddress in employeeAddresses on employee equals employeeAddress.employee
                        select new { EmployeeFirstName = employee.FirstName, EmployeeLastName = employee.LastName, EmployeeAddress = employeeAddress.Address };

            foreach (var item in query)
            {
                Console.WriteLine("Employee Name: {0} {1}  Address: {2} \n", item.EmployeeFirstName, item.EmployeeLastName, item.EmployeeAddress);
            }

        }

        static void Main(string[] args)
        {
            InnerJoinExample();
        }
        
    }