Showing posts with label JavaScript. Show all posts
Showing posts with label JavaScript. Show all posts

Thursday, December 22, 2011

Execute JavaScript from C# in update panel

Place the code before closing the update panel

<Triggers>
         <asp:PostBackTrigger ControlID = "btnSave" />
 </Triggers>

so that it would recognize the control under where the javascript written from c#

Tuesday, December 20, 2011

JS Alert Message by C# Code

Normally:
public static void ShowMsgBox(string message)
        {           
            Page page = HttpContext.Current.CurrentHandler as Page;
            page.ClientScript.RegisterStartupScript(typeof(Page), "abc", "<script type='text/javascript'>alert('" + message + "');</script>");           
        }

        public static void ShowMsgBoxByPageHide(string message)
        {
            // Cleans the message to allow single quotation marks
            string cleanMessage = message.Replace("'", "\'");
            string script = "<script type='text/javascript'>alert('" + message + "');</script>";

            // Gets the executing web page
            Page page = HttpContext.Current.CurrentHandler as Page;

            // Checks if the handler is a Page and that the script isn't allready on the Page
            if (page != null && !page.ClientScript.IsClientScriptBlockRegistered("alert"))
            {
                page.ClientScript.RegisterClientScriptBlock(typeof(Page), "alert", script);
            }
        }

For in Update Panel:
public static void ShowMsgBoxInUpdatePanel(string message)
        {
            // Cleans the message to allow single quotation marks
            string cleanMessage = message.Replace("'", "");
            string alertScript = "javascript: alert('" + cleanMessage + "')";
            Page page = HttpContext.Current.CurrentHandler as Page;
            ScriptManager.RegisterStartupScript(page, page.GetType(), "alertScript", alertScript, true);
        }

Thursday, February 10, 2011

Allowing only Numeric in web TextBox using JS

<script type = "text/javascript">
        function isNumberKey(evt) {
            var charCode = (evt.which) ? evt.which : event.keyCode
            if (charCode > 31 && charCode != 46 && (charCode < 48 || charCode > 57))
                return false;
            return true;
        }
</script>


Call the isNumberKey(event) function on keypress event of the textbox like this:
<asp:TextBox id="txtQty"  runat="server" onkeypress="return isNumberKey(event);"/>

Monday, January 31, 2011

Directly print any div of web page using JavaScript & HTML


Sample Div that is to print:
<div id="divPrint">
  <br />
<asp:Label ID="lblDetails" runat="server" Font-Bold="True" Font- Size="Medium">
</asp:Label>
  <br />                                   
      <asp:GridView ID="gvSample" runat="server">
      </asp:GridView>
</div>


JavaScript Code:
<script type="text/javascript">

        function CallPrint(strid) {
            var prtContent = document.getElementById(strid);
            var width = 600;
            var height = 500;
            var left = (screen.width - width) / 2;
            var top = (screen.height - height) / 2;
            var WinPrint = window.open('', '', 'letf=' + left + ',top=' + top + ',width=' + width + ',height=' + height + ',toolbar=0,scrollbars=0,status=0');
            WinPrint.document.write(prtContent.innerHTML);
            WinPrint.document.close();
            WinPrint.focus();
            WinPrint.print();
            WinPrint.close();
        }
</script>


Print Button:
<input type="button" value="Print" id="btnPrint" runat="Server" onclick="javascript:CallPrint('divPrint')" />