Thursday 6 November 2014

JQuery issue after partial postback

I came to know this theory after much R&D. I was not aware why JQuery is not get fired after partial postback. Then from google, I found solution as below. We have to register JQuery events after end of partial postback.

<asp:ScriptManager ID="smngr" runat="server"></asp:ScriptManager>
        <asp:UpdatePanel ID="upanel" runat="server">
            <ContentTemplate>
                <asp:Button ID="btn" Text="Test" runat="server" OnClick="btn_Click" />
                <input type="button" value="check" id="btncheck" />
            </ContentTemplate>
        </asp:UpdatePanel>

<script>

        Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(beginRequestHandler)
        Sys.WebForms.PageRequestManager.getInstance().add_endRequest(endRequestHandler)

        function beginRequestHandler() {
            var btn = document.getElementById('<%= btn.ClientID %>');
            btn.disabled = "disabled";
        }

        function endRequestHandler() {
            var btn = document.getElementById('<%= btn.ClientID %>');
            btn.disabled = "";
            register();
        }

        $(document).ready(function () {
            register();
        });

        function register()
        {
            $("#btncheck").click(function () {
                alert("Hello");
            });
        }


    </script>

Monday 3 November 2014

Generate Excel file using C#


using Excel = Microsoft.Office.Interop.Excel;

protected void ExportToExcell(object sender, EventArgs e)
{           
            string data = null;
            int i = 0;
            int j = 0;

            Excel.Application xlApp;
            Excel.Workbook xlWorkBook;
            Excel.Worksheet xlWorkSheet;
            object misValue = System.Reflection.Missing.Value;

            xlApp = new Excel.Application();
            xlWorkBook = xlApp.Workbooks.Add(misValue);
            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

           

            for (i = 0; i <= 10; i++)
            {
                for (j = 0; j <= 5; j++)
                {
                    data = "Ajay " + i.ToString();
                    xlWorkSheet.Cells[i + 1, j + 1] = data;
                    xlWorkSheet.Cells[1, j + 1].Font.Bold = true;
                    xlWorkSheet.Cells[1, j + 1].Interior.ColorIndex = 10;
                }
            }

            xlWorkBook.SaveAs(Server.MapPath("~/Docs/") + "csharp.net-informations.xls",
           Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, 
           Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
            xlWorkBook.Close(true, misValue, misValue);
            xlApp.Quit();

            releaseObject(xlWorkSheet);
            releaseObject(xlWorkBook);
            releaseObject(xlApp);
  
}



private void releaseObject(object obj)
{
            try
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
                obj = null;
            }
            catch (Exception ex)
            {
                obj = null;       
            }
            finally
            {
                GC.Collect();
            }           

}