Sunday, 18 January 2015

Java Script Code

  1. Alert Message in cs page.
    Page.RegisterStartupScript("UserMsg", "<Script language='javascript'>alert('" + "Password mismatch" + "');</script>");

    2.       Call javascript method from code behind page.
    ClientScript.RegisterStartupScript(GetType(), "amod", "callMyJSFunction()", true);

    3.       Redirect a page in HTML java script.
    window.location = "http://www.businessdesigncentre.co.uk/Hotels";

    4.       Call java script method on onload
    window.onload = callonload;

    5.       Add Image on background.
    <td width="20%" style="background-image: url('images/navi_bg.jpg'); background-position: top; background-repeat: no-repeat;" valign="top">

    6.       open the page on button click with the help of javascript 151114
    <input id="btnGoBack" runat="server" onclick="javascript:window.open('form6.aspx','_self')" type="button" value="        Go Back       " />

    7.       anchor hover change colorggggggggggggg
    <style type="text/css">
        a{text-decoration:none; border-bottom:1px solid #6B8F00;}
        a:hover{text-decoration: none;}
        </style>

    8.       Prevent to go back page specilly used after logout or login
    <BODY onLoad="javascript:history.go(+1);document.logoff.submit();">
    <form action="exhibitorloginzone.aspx" method="post" name="logoff">
    </form>

    9.       Attache image in TD
    <td width="20%" style="background-image: url('images/navi_bg.jpg'); background-position: top;  background-repeat: no-repeat;" valign="top">
     <uc1:LeftNavCtrl ID="LeftNavCtrl1" runat="server" />                                </td>

    10.   Find id in javascript
    document.getElementById("<%=txtSearch1.ClientID%>").style.borderColor="red";
    document.getElementById("<%=txtSearch1.ClientID%>").style.borderWidth = "1px";
      document.getElementById("<%=txtSearch1.ClientID%>").style.borderStyle = "solid";

    11.   Count total character in textbox for multiline to max limit textbox.
     <asp:TextBox ID="txtthink" Width="300px" MaxLength="150" TextMode="MultiLine"
    onkeyup="LimtCharacters(this,150,'lblcount');" runat="server"></asp:TextBox>
    <br/>
    Number of Characters Left:
    <label id="lblcount" style="background-color:#E2EEF1;color:Red;font-weight:bold;">150</label>  

         <script type="text/javascript">
             function LimtCharacters(txtMsg, CharLength, indicator) {
                 chars = txtMsg.value.length;
                 document.getElementById(indicator).innerHTML = CharLength - chars;
                 if (chars > CharLength) {
                     txtMsg.value = txtMsg.value.substring(0, CharLength);
                     alert("You can not enter more than 150 character");
                 }
             }
        </script>

    12.   Validate EmailID on onblur
    <input type="text" name="email" id="email" onblur='Javascript:checkEmail();'  class="formtext" /></td>
     <script type="text/javascript">
         function checkEmail() {

             var Email = document.getElementById("email");
             var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

             if (!filter.test(email.value)) {
                 alert('Please provide a valid email address');
                 Email.style.borderColor = "red";
                 Email.style.borderWidth = "1px";
                 Email.style.borderStyle = "solid";
                 Email.focus();
                 return false;
             }
             else {
                 Email.style.borderStyle = "none";
             }
         }
     </script>

    13.   Close the page on button click.
    <center><input name="Close" value="CLOSE WINDOW" accesskey="C" style="border: 1px solid rgb(0, 0, 0);
    font-weight: bold; font-size: 8pt; color: rgb(255, 255, 255); font-family: Arial; background-color: #8CC63E;"
    type="button" onclick="javascript:window.close();" /></center><br />

    14.   Nice alert ...anywhere u can use.
    Page.RegisterStartupScript("Myscript", "<script language='javascript'>alert('Please Enter Company Name Or Stand Number.')</script>");

    15.   close the window code
    ScriptManager.RegisterStartupScript(this.Page, GetType(), "ClosePopup", "window.close();", true);
    16.   Open the same page on button click or reload the same page
    <center><input type="button" id="Button1" value="Add another"
    onclick="javascript:window.open('ExhibitorRegistrationForm.aspx','_self');" /></center>

    17.   To open the page on button click within script
    function OpenPage(url) {
                window.open(url, 'mywindow', 'titlebar=yes, top=150, left=100, status=0, toolbar=no, scrollbars=1, resizable=1, width=660, height=450 ');
            }

    18.   change border color of textbox in javascript on click then go in cs page also
            function searchText()
    {
    if (document.getElementById("<%=txtMainSearch.ClientID%>").value=="")
       {
         alert("You must enter desire content for the search.");    
         //Change color of the textbox
         document.getElementById("<%=txtMainSearch.ClientID%>").style.borderColor="red";
         document.getElementById("<%=txtMainSearch.ClientID%>").style.borderWidth = "1px";
         document.getElementById("<%=txtMainSearch.ClientID%>").style.borderStyle = "solid";             
         document.getElementById("<%=txtMainSearch.ClientID%>").focus();
         return false;
       }
       else
        return true;
    }
    </script>

    <asp:Button ID="btnSearch" runat="server" Text="Search" CausesValidation="false" OnClientClick="return
    searchText();" onclick="btnSearch_Click" />

    19.   pRINT WHOLE PAGE
    <body class="body" onload="javascript:window.print();">

    20.   Display confirm messessage on button click.
    <asp:Button ID="btnSave" runat="server"  Text=" Submit “   OnClick="btnSave_Click" 
    OnClientClick="Javascript:return confirm('Are you sure you want to confirm your order');"  />

    21.   just go one page back with the help of anchor but in the same page. in the case of true false
    <p align="center"><font size="2" face="verdana"><strong><a href="javascript:history.back()">Go Back</a></strong></font></p>

    22.   Prevent to go back in java script and redirect to page also. 211114
    <BODY onLoad="javascript:history.go(+1);document.logoff.submit();">

    <form action="exhibitorloginzone.aspx" method="post" name="logoff">
    </form>
    </BODY>

    23.   Java script functions with Radio button list.
    function Validatevalues() {
               var RBID = '<%=RadioButtonList2.ClientID %>';
              var RB1 = document.getElementById(RBID);
                var radio = RB1.getElementsByTagName("input");
               for (var i = 0; i < radio.length; i++) {
                    if (radio[i].value == "other") {
                        if (document.getElementById('<%=TextBox47.ClientID %>').value == "") {
                            alert("Please insert the other value");
                            return false;
                        }
                        else return true;
                    }
                    else return true;     
                }
            }

    24.   Custom validation with check box list.
    function ValidateModuleList1(source, args) {
                var chkListModules = document.getElementById('<%= ChList1.ClientID%>');
                var chkListinputs = chkListModules.getElementsByTagName("input");
                for (var i = 0; i < chkListinputs.length; i++) {
                    if (chkListinputs[i].checked) {
                        args.IsValid = true;
                        return;
                    }
                }
                args.IsValid = false;
            }
    <asp:CheckBoxList ID="ChList1" runat="server" RepeatColumns="2" RepeatLayout="Table" Width="100%">
    <asp:ListItem Value="Health & Nutrition" Text="Health & Nutrition"></asp:ListItem>
    <asp:ListItem Value="Natural Living" Text="Natural Living"></asp:ListItem>
    <asp:ListItem Value="Natural Beauty & Spa" Text="Natural Spa"></asp:ListItem>
    </asp:CheckBoxList>
    <asp:CustomValidator runat="server" ID="CustomValidato1" ClientValidationFunction="ValidateModuleList1"
     ErrorMessage="Required!" Display="Dynamic"><span style="color:Red;">Please select at least one Show category..!</span></asp:CustomValidator>

    25. Find ID in Checkbox in javascript
            function validateservice() {
                var x1 = document.getElementById("<%=CheckBox1.ClientID%>");
                var x2 = document.getElementById("<%=CheckBox3.ClientID%>");
               
                if (x1.checked) {
                    x2.checked = false;
                    x3.checked = false;
                }
    <asp:CheckBox ID="CheckBox1" runat="server" onclick="validateservice();" />





    26. Find ID of check box list and check its checked or not.
    var atLeast = 1
            function Validate() {
                var CHK = document.getElementById("<%=chkprodlist.ClientID%>");
                var checkbox = CHK.getElementsByTagName("input");
                var counter = 0;
                for (var i = 0; i < checkbox.length; i++) {
                    if (checkbox[i].checked) {
                        counter++;
                    }
                }
                if (atLeast > counter) {
                    alert("Please select atleast one Product/Services ?");
                   return false;
                }
                return true;
            }
    <asp:CheckBoxList ID="chkprodlist" runat="server" RepeatColumns="3" Width="100%">
                                                    <asp:ListItem Value="Service Desk/Helpdesk" Text="Service Desk/Helpdesk"></asp:ListItem>
    </asp:CheckBoxList>

    <asp:CheckBox ID="CheckBox2" runat="server" onclick="validatemajor();" />


    27.  Reload the page after closing on page.
    function closechildwindow() {window.opener.document.location.href = 'http://onlineexhibitormanual.com/rhshamptoncourt15/form11.aspx';
    window.close();
    }
    <body onunload="closechildwindow()" bgcolor="#D8F0FA">
    28. Session Time out counter and show alert.
    <script type="text/javascript">
           
            var sessionTimeout = "<%= Session.Timeout %>";
            function DisplaySessionTimeout() {
                //assigning minutes left to session timeout to Label
                document.getElementById("<%= lblSessionTime.ClientID %>").innerHTML = sessionTimeout;
                sessionTimeout = sessionTimeout - 1;

                //if session is not less than 0
                if (sessionTimeout >= 0)
                //call the function again after 1 minute delay
                    window.setTimeout("DisplaySessionTimeout()", 60000);
                else {
                    //show message box
                    alert("Your current Session is over.");
                }
            }
        </script>
    <strong>Session Time Left</strong>: <asp:Label ID="lblSessionTime" runat="server" ForeColor="Red" Font-Bold="true"></asp:Label>

    //set DisplaySessionTimeout() as the startup script of this page
            Page.ClientScript.RegisterStartupScript(this.GetType(), "onLoad", "DisplaySessionTimeout()", true);

    29.  Reload the same page.
    location.reload();

    30.  Call function after some time or set the time in function
    window.setTimeout("DisplaySessionTimeout()", 60000);

    31. Prevent to go back
    <BODY onLoad="javascript:history.go(+1);document.logoff.submit();">

    32. Select the dropdownlist value
    <title>Get Dropdownlist selected value and text in JavaScript</title>
    <script type="text/javascript">
    function GetCountryDetails() {
    // Get id of dropdownlist
    var parm = document.getElementById("ddlCountry");
    // Get Dropdownlist selected item text
    document.getElementById('lbltxt').innerHTML = parm.options[parm.selectedIndex].text;
    // Get Dropdownlist selected value item
    document.getElementById('lblid').innerHTML = parm.options[parm.selectedIndex].value;
    }
    </script>

    33. Calculation example in javascript
    //section A
            function TotalCost1() {
                var a = 0;
                var sum = 0;
                var sum1 = 0;
                a = document.getElementById("<%=TextBox1.ClientID%>").value;
                sum = a * 3575.00;
                document.getElementById("<%=TextBox2.ClientID%>").value = sum;
                document.getElementById("<%=TextBox7.ClientID%>").value = parseFloat(parseFloat(document.getElementById("<%=TextBox2.ClientID%>").value) + parseFloat(document.getElementById("<%=TextBox4.ClientID%>").value) + parseFloat(document.getElementById("<%=TextBox6.ClientID%>").value)).toFixed(2);

                sum1 = parseFloat(document.getElementById("<%=TextBox7.ClientID%>").value);

                document.getElementById("<%=TextBox8.ClientID%>").value = parseFloat(sum1 * 0.1236).toFixed(2);

                document.getElementById("<%=TextBox9.ClientID%>").value = parseFloat(parseFloat(document.getElementById("<%=TextBox7.ClientID%>").value) + parseFloat(document.getElementById("<%=TextBox8.ClientID%>").value)).toFixed(2);
                document.getElementById("<%=TextBox19.ClientID%>").value = parseFloat(parseFloat(document.getElementById("<%=TextBox9.ClientID%>").value) + parseFloat(document.getElementById("<%=TextBox18.ClientID%>").value)).toFixed(2);

            }

    34.  Restrict back button in Java script in Asp.Net C#
      <script type="text/javascript">        window.history.forward();
            function noBack() { window.history.forward(); }
        </script>

No comments:

Post a Comment