Friday 25 September 2015

Show browser file or input type=file show only button in asp.net or HTML


//CSS

input[type=file] {
position: absolute;
border: solid transparent;
border-width: 0 0 0px 0px;
opacity: 0.0;
filter: alpha(opacity = 0);
}
.filebutton {
position: relative;
cursor: pointer;
text-align: center;
}
.filebutton span {
text-align: center;
padding: 2px 6px 3px;
background: #eac380 -moz-linear-gradient(center top , #eac380 5%, #fae4bd 100%) repeat scroll 0 0;
display: inline-block;

}

// HTML Controls

<label class=" filebutton">
   <span>Add Files</span>
   <input type="file" id="files" name="file"></input>
</label>

// Asp.net Controls

                               <label class="filebutton">
                                  <span>Browse...</span>
                                  <asp:FileUpload ID="FileUpload1" runat="server"/>
                              </label>

Thursday 24 September 2015

How can I format currency by simple using JQuery in all format

// HTML

<input type='text' id='currency'/>


//JS

$('#currency').keyup(function (e) {
            $(this).val(formatCurrency(this.value.replace(/[,$]/g, '')));
        }).on('keypress', function (e) {
            if (!$.isNumeric(String.fromCharCode(e.which))) e.preventDefault();
        }).on('paste', function (e) {
            var cb = e.originalEvent.clipboardData || window.clipboardData;
            if (!$.isNumeric(cb.getData('text'))) e.preventDefault();
        });
        function formatCurrency(number) {
            var x = number.toString();
            var lastThree = x.substring(x.length - 3);
            var otherNumbers = x.substring(0, x.length - 3);
            if (otherNumbers != '')
                lastThree = ',' + lastThree;
            return otherNumbers.replace(/\B(?=(\d{2})+(?!\d))/g, ",") + lastThree;
        }


// Result 



1,11,11,111

Saturday 5 September 2015

jQuery Show upload button instead of file upload



<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>jQuery Show upload button instead of file upload</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>
<script type="text/javascript">
    $(function () {
        $("#fileupload1").change(function () {
            $("#FileName").html($("#fileupload_Dilog").val().substring($("#fileupload_Dilog").val().lastIndexOf('\\') + 1));
        });
    });
</script>
<style type="text/css">
.button {
background: #333;
color: #fff;
padding: 10px 15px;
border-radius: 5px;
}
.button:hover {
background: #51A8CA;
}
</style>
</head>
<body>
<form id="form1>
<div><br />
<input style="display:none" type="file" id="fileupload_Dilog" />
<input type="button" class="button" id="btnUpload" onclick='$("#fileupload_Dilog").click()' value="Upload"/>
<span id="FileName"></span>
</div>
</form>
</body>
</html>



Wednesday 2 September 2015

Export to Excel using Stream Writer in c#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ExportDataGridviewtoExcel
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            BindDataGrid();
        }
        private void BindDataGrid()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("UserId", typeof(Int32));
            dt.Columns.Add("UserName", typeof(string));
            dt.Columns.Add("Education", typeof(string));
            dt.Columns.Add("Location", typeof(string));
            dt.Rows.Add(1, "Krishna", "MCA", "Gurgaon");
            dt.Rows.Add(2, "Raja", "MBA", "Delhi");
            dt.Rows.Add(3, "Sanu", "B.Tech", "kanpu");
            dt.Rows.Add(4, "amit", "MSC", "delhi");
            dt.Rows.Add(5, "sonu", "CA", "Jaipur");
            dt.Rows.Add(6, "rohit", "B.Tech", "Nagpur");
            dataGridView1.DataSource = dt;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (dataGridView1.Rows.Count > 0)
            {
                try
                {
                    // Bind Grid Data to Datatable
                    DataTable dt = new DataTable();
                    foreach (DataGridViewColumn col in dataGridView1.Columns)
                    {
                        dt.Columns.Add(col.HeaderText, col.ValueType);
                    }
                    int count = 0;
                    foreach (DataGridViewRow row in dataGridView1.Rows)
                    {
                        if (count < dataGridView1.Rows.Count - 1)
                        {
                            dt.Rows.Add();
                            foreach (DataGridViewCell cell in row.Cells)
                            {
                                dt.Rows[dt.Rows.Count - 1][cell.ColumnIndex] = cell.Value.ToString();
                            }
                        }
                        count++;
                    }
                    // Bind table data to Stream Writer to export data to respective folder
                    StreamWriter wr = new StreamWriter(@""+textBox1.Text+"\\Book1.xls");
                    // Write Columns to excel file
                    for (int i = 0; i < dt.Columns.Count; i++)
                    {
                        wr.Write(dt.Columns[i].ToString().ToUpper() + "\t");
                    }
                    wr.WriteLine();
                    //write rows to excel file
                    for (int i = 0; i < (dt.Rows.Count); i++)
                    {
                        for (int j = 0; j < dt.Columns.Count; j++)
                        {
                            if (dt.Rows[i][j] != null)
                            {
                                wr.Write(Convert.ToString(dt.Rows[i][j]) + "\t");
                            }
                            else
                            {
                                wr.Write("\t");
                            }
                        }
                        wr.WriteLine();
                    }
                    wr.Close();
                    label1.Text = "Data Exported Successfully";
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            DialogResult result = folderBrowserDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                textBox1.Text = folderBrowserDialog1.SelectedPath.ToString();
            }           
        }
    }
}


// Result


Excel Sort values in ascending order using function TEXTJOIN

 Excel ::  Text ::  1,3,5,2,9,5,11 Result :: 1,2,3,5,5,9,11 Formula ::     TEXTJOIN ( ",",1,SORT(MID(SUBSTITUTE( A1 ,","...