Saturday, January 4, 2014

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.

Monday, November 25, 2013

How to disable jQuery UI tab(s)

Single tab

$( "#tabs" ).tabs( "option", "disabled", 1);

Multiple tabs

$( "#tabs" ).tabs('option','disabled', [1, 2, 3, 4]);

Saturday, November 9, 2013

An entry with the same key already exists.

Causes:
  • Multiple asp net controls with same ID
  • Mostly occurs inside ListView control 
  • Keep an eye on validator controls. Try to have different id on master and child pages.

Sunday, November 3, 2013

Change an Image while using JCrop

Initialize jCrop
var jcrop_api;
        function InitjCrop() {
            $('#id').Jcrop({  }, function () {
                jcrop_api = this;
            });
        }
To unhook jCrop
jcrop_api.destroy();
To Hook jCrop again
InitjCrop();