Showing posts with label ASP. Show all posts
Showing posts with label ASP. Show all posts

Tuesday, February 3, 2015

Natural Sorting Using Linq For DataTable C#.


public partial class MasterDetailLn_LoanIssue : System.Web.UI.Page
{
private void function()
{
DataTable dtLn_LoanPaid = Ln_LoanPaidService.GetLn_LoanByDepartment();
   DataTable dtsorted=new DataTable();
        if(dtLn_LoanPaid!=null &&dtLn_LoanPaid.Rows.Count>0)
         dtsorted= dtLn_LoanPaid.Select().OrderBy(O=>O.Field<String>("PFNo"),new NaturalSortComparer<string>()).CopyToDataTable();
    }
}
public class NaturalSortComparer<T> : IComparer<string>, IDisposable
{
    private bool isAscending;

    public NaturalSortComparer(bool inAscendingOrder = true)
    {
        this.isAscending = inAscendingOrder;
    }

    #region IComparer<string> Members

    public int Compare(string x, string y)
    {
        throw new NotImplementedException();
    }

    #endregion

    #region IComparer<string> Members

    int IComparer<string>.Compare(string x, string y)
    {
        if (x == y)
            return 0;

        string[] x1, y1;

        if (!table.TryGetValue(x, out x1))
        {
            x1 = Regex.Split(x.Replace(" ", ""), "([0-9]+)");
            table.Add(x, x1);
        }

        if (!table.TryGetValue(y, out y1))
        {
            y1 = Regex.Split(y.Replace(" ", ""), "([0-9]+)");
            table.Add(y, y1);
        }

        int returnVal;

        for (int i = 0; i < x1.Length && i < y1.Length; i++)
        {
            if (x1[i] != y1[i])
            {
                returnVal = PartCompare(x1[i], y1[i]);
                return isAscending ? returnVal : -returnVal;
            }
        }

        if (y1.Length > x1.Length)
        {
            returnVal = 1;
        }
        else if (x1.Length > y1.Length)
        {
            returnVal = -1;
        }
        else
        {
            returnVal = 0;
        }

        return isAscending ? returnVal : -returnVal;
    }

    private static int PartCompare(string left, string right)
    {
        int x, y;
        if (!int.TryParse(left, out x))
            return left.CompareTo(right);

        if (!int.TryParse(right, out y))
            return left.CompareTo(right);

        return x.CompareTo(y);
    }

    #endregion

    private Dictionary<string, string[]> table = new Dictionary<string, string[]>();

    public void Dispose()
    {
        table.Clear();
        table = null;
    }
}

Thursday, January 15, 2015

Finding PostBack Control from ClientSide JavaScript in ASP .net.

We can Find the Control Creating PostBack in ASP .net webform from ClientSide Script using following Code and Also can Capture the end of Asynchronous postback event in case of update panel  where generally focus is lost after asynchronous post back.

 $(function () {

 Sys.WebForms.PageRequestManager.getInstance().add_endRequest(function (sender, e) {
         if (sender._postBackSettings.sourceElement.id == '<%=ddlLedger.ClientID %>')

                       //Your Control creating postback
                      $("#ctl00_ContentPlaceHolder1_ddlSubLedger_chosen .chosen-single").focus();
         else {
                  //do your thing
                }

            });        
});


Hence on endrequest we can get the postback control on completion of post back and capture the after post back event to do what needed to like may be a focus to next control which is usually lost in case we are using update panel or in case of chosen dropdown.

Monday, January 5, 2015

Increase Session Time Out in ASP .NET Application.

We Can Increase Session Time Out Using Following Two Methods.

1. Using Web.Config File.
2. Using IIS setting in the windows server(Application Server).

Using Web.Config File.

Using  web.Config file we need to add timeout="minutes" in  sessionstate of system.web section of web.config file.

<system.web>
        <sessionState timeout="540"/>
.....


 Using IIS setting in the windows server(Application Server).


Change the following time-outs in Internet Services Manager .Choose a value greater than the default of 20.
    1. Select Default Web Site > Properties > Home Directory > Application Settings > Configuration > Options.
    2. Enable the session state time-out and set the Session timeout for 60 minutes.

    3. Select Application Pools > DefaultAppPool > Properties.
    4. From the Performance tab under Idle timeout, set Shutdown worker processes after being idle for a value higher than 20.

The default session time-out setting on IIS is 20 minutes but it can be increased to a maximum of 24 hours or 1440 minutes. See Microsoft article Q233477 for details about increasing the timeout in IIS.
Symptom
When returning to the logon page for Web Interface, users often encounter an Error: Your session with the web-server expired. You have been logged out.
Cause
Web Interface 2.0 picks up the session timeout setting from IIS.
Resolution
1. Start Internet Services Manager 5.0.
2. For explicit authentication, right-click the /Citrix/MetaFrameXP/default virtual directory and view the Properties.
    For desktop credentials, pass-through users edit the /Citrix/MetaFrameXP/integrated virtual directory.
For Smart Card users, edit the /Citrix/MetaFrameXP/certificate virtual directory.
3. In the Application Settings section, click Configuration.
4. Select the App Options tab.
5. Ensure Enable session state is selected.
The default session time-out setting on IIS is 20 minutes but it can be increased to a maximum of 24 hours or 1440 minutes. See Microsoft article Q233477 for details about increasing the timeout in IIS.
NFuse Classic 1.71 and earlier
When Internet Explorer is configured never to check for newer versions of Web pages, NFuse Classic will not launch an application after a session is allowed to expire. To reproduce this problem:
    1. Configure your browser settings for 'Temporary Internet files and set Check for newer versions of stored pages to Never.
    2. Log on to Citrix NFuse Classic and view your published applications.
    3. Leave the system idle for 20 minutes or longer so that the Web server session expires.
    4. Click an application icon. The system returns you to the Logon page and a message appears stating that your session expired.
    5. Log on again and click the same application as before. You are again returned to the Logon page even though your session should not have expired this time.
Solutions
      • Clear the Temporary Internet Items folder
      • Upgrade to Web Interface 2.0 or later
      • Set Check for Newer versions of stored pages to Automatically
The default session time-out setting on IIS is 20 minutes but it can be increased to a maximum of 24 hours or 1440 minutes. See Microsoft article Q233477 for details about increasing the timeout in IIS.

Saturday, December 27, 2014

Confirm Before Leaving the Page

Following Function asks the User to Confirm if she really wants to leave the page on before unload of page.

 var myEvent = window.attachEvent || window.addEventListener;
        var chkevent = window.attachEvent ? 'onbeforeunload' : 'beforeunload'; ///for IE7, IE8

                    myEvent(chkevent, function(e) { // For >=IE7, Chrome, Firefox
                        var confirmationMessage = 'Are you sure to leave the page?';  // a space
                        (e || window.event).returnValue = confirmationMessage;
                        return confirmationMessage;
        });




This Will help to maintain data in accidental back space click. But if you click back space twice you will loose the Page. 

Friday, December 19, 2014

Convert To PDF Using Pechkin in asp .net.

  1. Download and compile this branch of the Pechkin project: https://github.com/tuespetre/Pechkin (This will solve the DLL hanging problem :)). Or, if you’re lazy, you can download here the needed DLLs. Buid the updated pechkin library
  2. Inside your web application solution add a reference to: Common.Logging.dll and Pechkin.dll.
    Add references
  3. Add to the root of your solution the following DLLs and set from the properties window: Copy to output directory » Copy always.Copy always
  4. Now you can start generating PDF files from HTML strings. Below is a very simple example that does this: 
    protected void btnExportToPDF_Click(object sender, EventArgs e)
    {
        var pechkin = Factory.Create(new GlobalConfig());
        var pdf = pechkin.Convert(new ObjectConfig()
                                .SetLoadImages(true).SetZoomFactor(1.5)
                                .SetPrintBackground(true)
                                .SetScreenMediaType(true)
                                .SetCreateExternalLinks(true), ltrReport.Text);

        //Return the PDF file
        Response.Clear();

        Response.ClearContent();
        Response.ClearHeaders();

        Response.ContentType = "application/pdf";
        Response.AddHeader("Content-Disposition", string.Format("attachment;filename=test.pdf; size={0}", pdf.Length));
        Response.BinaryWrite(pdf);

        Response.Flush();
        Response.End();
    }

5. After publishing it to IIS, select the application pool for your web site, then right-click and select Advanced Settings... Here make sure to enable 32-bit applications otherwise you will get this error:
Could not load file or assembly ‘Pechkin’ or one of its dependencies. An attempt was made to load a program with an incorrect format.
  1. Application pool settings

Thursday, August 14, 2014

How to Display Message "Caps Lock is on" on Password Text Box Using Javascript.

Below is the Script to identify if the Caps Lock is on  While typing on password text box.

Caps Lock is On Message Display.


sample code:
   <script type="text/javascript" language="Javascript">
        function capLock(e) {
            kc = e.keyCode ? e.keyCode : e.which;
            sk = e.shiftKey ? e.shiftKey : ((kc == 16) ? true : false);
            if (((kc >= 65 && kc <= 90) && !sk) || ((kc >= 97 && kc <= 122) && sk))
                document.getElementById('divMayus').style.visibility = 'visible';
            else
                document.getElementById('divMayus').style.visibility = 'hidden';
        }
    </script>


Call this Function on the keypress event of the Password Text Box.
onkeypress="capLock(event)"

Now, The Message will be displayed on the div with id="divMayus" as below.

<div id="divMayus" style="visibility: hidden; "><P style="color:red;">Caps Lock is on.</P></div>