Wednesday, 5 June 2013

The difference between trigger() and triggerHandler()

<!DOCTYPE html>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">
</script>
<script>
$(document).ready(function(){
  $("input").select(function(){
    $("input").after(" Text marked!");
  });
  $("#btn1").click(function(){
    $("input").trigger("select");
  });
  $("#btn2").click(function(){
    $("input").triggerHandler("select");
  });
});
</script>
</head>
<body>

<p>Click each button to see the difference between trigger() and triggerHandler().
<br><br>
<input type="text" value="Hello World">
<br><br>
<button id="btn1">trigger()</button>
<button id="btn2">triggerHandler()</button>

</body>
</html>

trigger() is call a internel code of select event and select a textbox
The trigger() method triggers the specified event and the default behavior of an event (like form submission) for the selected elements.

triggerHandler() is call internel code of select event ,but textbox is not select

Accordion Panel in jquery

Use below code for make content accordion.


Write this Code in .aspx Page

<div class="accordion">
    <h3 class="">Question One Sample Text</h3>
    <p style="display: none; ">Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Morbi malesuada, ante at feugiat tincidunt, enim massa gravida metus, commodo lacinia massa diam vel eros. Proin eget urna. Nunc fringilla neque vitae odio. Vivamus vitae ligula.</p>
    <h3 class="">This is Question Two</h3>
    <p style="display: none; ">Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Morbi malesuada, ante at feugiat tincidunt, enim massa gravida metus, commodo lacinia massa diam vel eros. Proin eget urna. Nunc fringilla neque vitae odio. Vivamus vitae ligula.</p>
    <h3 class="">Another Questio here</h3>
    <p style="display: none; ">Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Morbi malesuada, ante at feugiat tincidunt, enim massa gravida metus, commodo lacinia massa diam vel eros. Proin eget urna. Nunc fringilla neque vitae odio. Vivamus vitae ligula.</p>
    <h3 class="">Sample heading</h3>
    <p style="display: none; ">Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Morbi malesuada, ante at feugiat tincidunt, enim massa gravida metus, commodo lacinia massa diam vel eros. Proin eget urna. Nunc fringilla neque vitae odio. Vivamus vitae ligula.</p>
    <h3 class="active">Sample Question Heading</h3>
    <p style="display: block; ">Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Morbi malesuada, ante at feugiat tincidunt, enim massa gravida metus, commodo lacinia massa diam vel eros. Proin eget urna. Nunc fringilla neque vitae odio. Vivamus vitae ligula.</p>
</div>


// Script tag for slide up slide down content.

 <script type="text/javascript">
        $(document).ready(function () {

            $(".accordion h3:first").addClass("active");
            $(".accordion p:not(:first)").hide();

            $(".accordion h3").click(function () {
                $(this).next("p").slideToggle("slow")
        .siblings("p:visible").slideUp("slow");
                $(this).toggleClass("active");
                $(this).siblings("h3").removeClass("active");
            });

        });
</script>

// Style sheet for Accordion Panel

 <style type="text/css">
body {
    margin: 10px auto;
    width: 570px;
    font: 75%/120% Arial, Helvetica, sans-serif;
}
.accordion {
    width: 480px;
    border-bottom: solid 1px #c4c4c4;
}
.accordion h3 {
    background: #e9e7e7 url(images/arrow-square.gif) no-repeat right -51px;
    padding: 7px 15px;
    margin: 0;
    font: bold 120%/100% Arial, Helvetica, sans-serif;
    border: solid 1px #c4c4c4;
    border-bottom: none;
    cursor: pointer;
}
.accordion h3:hover {
    background-color: #e3e2e2;
}
.accordion h3.active {
    background-position: right 5px;
}
.accordion p {
    background: #f7f7f7;
    margin: 0;
    padding: 10px 15px 20px;
    border-left: solid 1px #c4c4c4;
    border-right: solid 1px #c4c4c4;
}
</style>

Export Html table data to Excel using jQuery

First of all add reference of jquery before apply this code.this code is usefull for export html table data in excel sheet.

write this in .aspx or html page

<div id="dvData">
<table>
    <tr>
        <th>Column One </th>
        <th>Column Two</th>
        <th>Column Three</th>
    </tr>
    <tr>
        <td>row1 Col1</td>
        <td>row1 Col2</td>
        <td>row1 Col3</td>
   </tr>
   <tr>
        <td>row2 Col1</td>
        <td>row2 Col2</td>
        <td>row2 Col3</td>
   </tr>
      <tr>
        <td>row3 Col1</td>
        <td>row3 Col2</td>
        <td>row3 Col3</td>  
   </tr>
</table>
</div>
<br/>
<input type="button" id="btnExport" value=" Export Table data into Excel " />

Add below code in script tag

$("#btnExport").click(function(e{
      window.open('data:application/vnd.ms-excel,' +encodeURIComponent($('#dvData').html()));
       e.preventDefault();
}); 

ajax call in jquery asp.net

Ajax call in jquery use
  • url
    Type: String
    A string containing the URL to which the request is sent.
  • async (default: true)
    Type: Boolean
    By default, all requests are sent asynchronously (i.e. this is set to true by default). If you need synchronous requests, set this option to false. Cross-domain requests and dataType: "jsonp" requests do not support synchronous operation. Note that synchronous requests may temporarily lock the browser, disabling any actions while the request is active. 
  • cache (default: true, false for dataType 'script' and 'jsonp')
    Type: Boolean
    If set to false, it will force requested pages not to be cached by the browser. 
  • contentType (default: 'application/x-www-form-urlencoded; charset=UTF-8')
    Type: String
    When sending data to the server, use this content type. Default is "application/x-www-form-urlencoded; charset=UTF-8", which is fine for most cases. 


$.ajax({
            type: "POST",
            url: "../Pagename.aspx/user",
            data: JSON.stringify({ userId: 1, status: 1 }),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            async: false,
            success: function (data) {
                                //do logic
                           }
        });

Create Zip File in C#

Apply this code in codebehind file for download image,document and any other file in zip folder.

using (ZipFile zip = new ZipFile())
                    {
                        zip.AlternateEncodingUsage = ZipOption.AsNecessary;
                        DirectoryInfo dr = new DirectoryInfo(HttpContext.Current.Server.MapPath(strRequest));
                        //                        zip.AddDirectoryByName(strRequest);
                        if (dr.Exists)
                        {
                            foreach (FileInfo fl in dr.GetFiles())
                            {
                                string filePath = dr.FullName + "\\" + fl.Name;
                                //zip.AddFile(filePath, strRequest);
                                zip.AddFile(filePath,dr.Name);
                            }
                            Response.Clear();
                            Response.BufferOutput = false;
                            string zipName = String.Format("Zip_{0}.zip", dr.Name);
                            Response.ContentType = "application/zip";
                            Response.AddHeader("content-disposition", "attachment; filename=" + zipName);
                            zip.Save(Response.OutputStream);
                            Response.Flush();
                            Response.End();
                        }
                        else
                        {
                            // redirect to page which called download page
                            Response.Redirect(Server.MapPath(Request.UrlReferrer.AbsolutePath), false);
                        }
                    }

Change Date Formate in C#

Convert.ToDateTime(DateTime.ParseExact(''12-03-2012", "dd-MM-yyyy", null).ToString("yyyy-MM-dd"))

protected static DateTime changedateformate(string Date)
    {
        string[] fromdate;
        DateTime cndate = DateTime.Now;
        if (Date != "")
        {
            if (Date.Contains('/'))
            {
                fromdate = Date.Split('/');
                Date = fromdate[2] + "/" + fromdate[1] + "/" + fromdate[0];
                cndate = Convert.ToDateTime(Date).Date;
            }
            else
            {
                fromdate = Date.Split('-');
                Date = fromdate[2] + "-" + fromdate[1] + "-" + fromdate[0];
                cndate = Convert.ToDateTime(Date).Date;
            }
        }
        return cndate;
    }

Delete Row From Datatable in Asp.net C#

string[] result = datatable.Rows.Cast<DataRow>().Select(row => row["COLUMN_NAME"].ToString()).ToArray();

if (Array.IndexOf(result, "colname") != -1)
                {
                    int RowNo = Array.IndexOf(result, "colname");
                    result = result.Where((val, idx) => idx != RowNo).ToArray();
                    datatable.Rows[RowNo].Delete();
                }
datatable.Acceptchanges();