Saturday, January 4, 2014

Save jSign image to file using php

Setup jSign first
in head section of page
<!-- you load jquery somewhere above here ... -->
<!--[if lt IE 9]>
<script type="text/javascript" src="libs/flashcanvas.js"></script>
<![endif]-->
in body section
<form action="save-sign.php" method="post">
 Sign here
 <div id="signature"></div>
 <input type="hidden" name="hdnSignature" id="hdnSignature" />
 <input type="button" id="btn_submit" value="Submit Form" />
</form>
<script>
$(function () {
 'use strict';
 var $sigdiv = $("#signature");
 $sigdiv.jSignature();// inits the jSignature widget.
 
 //save data to hidden field before submiting the form
 $('#btn_submit').click(function () {
  var datapair = $sigdiv.jSignature("getData""image");
  $('#hdnSignature').val(datapair[1]);
 //now submit form 
document.forms[0].submit();
  });
 });
</script>
and save-sign.php file should be like this
<?php
$sign = '';
if(isset($_POST['hdnSignature']))
{
 $sign = $_POST['hdnSignature'];
 $path = "path to file.png";
 $sign = base64_decode($sign); //convert base64
 file_put_contents($path$sign); //save to file
 //all done
}
?>

Validating date using Compare Validator asp net

Use below given code snippet
<asp:CompareValidator ID="CompareValidator1" runat="server" ErrorMessage="Invalid Date"  
Operator="DataTypeCheck" Type="Date" ControlToValidate="{}" ControlToCompare="not required">
</asp:CompareValidator>

Export / Output asp net gridview output to excel file

If you want to export gridview output to an excel file you can use the following code.
In this example it is done by clicking a button
Requirements
  1. GridView with ID GridView1
  2. ASP NET Button With ID Button1
    protected void Button1_Click(object sender, EventArgs e)
    {
        string filename = "Bingo.xls";
        ExportToExcel(filename, GridView1);
    }
     
    private void ExportToExcel(string filename, GridView gv)
    {
        Response.ClearContent();
        Response.AppendHeader("content-disposition""attachment; filename=" + filename);
        Response.Charset = "";
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.ContentType = "application/vnd.ms-excel";
        System.IO.StringWriter stringWriter = new System.IO.StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(stringWriter);
        gv.RenderControl(htw);
        Response.Write(stringWriter.ToString());
        Response.End();
    }
Half way there Don't forget to override VerifyRenderingInServerForm
public override void VerifyRenderingInServerForm(Control control)
{
    /* Verifies that the control is rendered */
}
All done.