Wednesday, July 16, 2014

JavaScript Function to Disable Back Event of web-Browser.

This Function adds # and 1 to window location in a very short period, fraction of second so that There is no Previous page Other than same page for back space of browser back event.
So it does not actually disables the back event but makes the page behave like that.

  //disable back button of web-Browser

        function changeHashOnLoad() {
            window.location.href += "#";
            setTimeout("changeHashAgain()", "50");
        }

        function changeHashAgain() {
            window.location.href += "1";
        }

        var storedHash = window.location.hash;
        window.setInterval(function () {
            if (window.location.hash != storedHash) {
                window.location.hash = storedHash;
            }
        }, 50);
        changeHashOnLoad();

Maintining Client Side State after Server Side PostBack in ASP .net.

In ASP .net After Server Side Post Back, JavaScript Function or Content Of a readonly text Box changed By JavaScript will be lost if not maintained or re-bound.

To Get The Associated Event available for the control after post bock we need to
re-assign such events on postback

Sys.WebForms.PageRequestManager.getInstance().add_endRequest(function (sender, e) 

will do that.

Example:

 $(function () {
            MaintainSelect();
            myf();
            $("#<%= txtnetPayable.ClientID %>").attr('readonly', 'readonly');

            Sys.WebForms.PageRequestManager.getInstance().add_endRequest(function (sender, e) {
                MaintainSelect();
                myf();
                $("#<%= txtnetPayable.ClientID %>").attr('readonly', 'readonly');
            });
        });


 function MaintainSelect() {
            $(document).ready(function () {
                $('.nepali-calendar').calendarsPicker({
                    calendar: $.calendars.instance('nepali', "ne")

                });
            });
            $('#<%=dtJVDate.ClientID%>').calendarsPicker({
                calendar: $.calendars.instance('nepali', "ne"),
                onSelect: function (date) {
                    $('.nepali-calendar').val(date[0].formatDate());
                }
            });
        }


 function myf() {
            $(function () {
                $("#<%= txtDiscount.ClientID %>").change(
                         function () {
                             getamt();
                         }
                                  );

                $("#<%= txtVatAmount.ClientID %>").change(
                        function () {
                            getamt();
                            $("#<%= txtnetPayable.ClientID %>").trigger('change');
                        }
                     );
            }
            );
                }


Here, in Above Example There Are Two Cases Handled After Post Back.
1. Calendar Picker is Maintained with its popup on click.
2. A Text Box 'txtnetPayable' which needs to be Read Only and Should Display Text on change of Other Text Box.

In First Case There is not Much Trick but Reassigning Events Makes it work.

In Second Case the IF the Text Box is Made Read only in Server Side Then all the Changes on client side will be lost on server side post back.
To Eliminate this behavior we need to make text box  Read Only In client side and again we need to Maintain it Property after Server Side Post Back.


Regex to Get Name Separated By Comma From PascalCase.

This is the Regex to Find the Starting of Word in PascalCase.
Finding the Beging of each word we can separete words is Pascal Cased Word and Hence can Get User Readable Name.

    private static Regex r = new Regex(
       @"(?<=[A-Z])(?=[A-Z][a-z])|(?<=[^A-Z])(?=[A-Z])|(?<=[A-Za-z])(?=[^A-Za-z])"
     );



Below is a C# Function to Get the Space Separated Name From Pascal Cased word.
        public static string GetNameFromPascalCaseString(string s)
        {
            if (s == null || s == "")
                return string.Empty;
            return r.Replace(s, " ");
        }

Below is another Function to Get Name Separated By Under Score '_' for each word in Pascal Cased Name.
        public static string GetNameWith_FromPascal(string s)
        {
            if (s == null || s == "")
                return string.Empty;
            return r.Replace(s, "_");
        }