Search This Blog

Tuesday, October 28, 2014

Accept only numbers in a APS.NET Textbox using JavaScript

Client side validations become very important when submitting data on a web page. Here is a practical scenario. We have an input box and it must accept only numbers, and it should not allow other characters as input. We will validate the contents of the input box at the client side before calling a post back event.


The Markup and Script
<html>
<head></head>
<body>

    <div>
        Enter only Numbers: <asp:TextBox ID="txtNumbers" value="" runat="server" 
            onkeypress="javascript:return isNumber (event)" ></asp:TextBox>
    </div>
    
</body>

<script>

    // WRITE THE VALIDATION SCRIPT IN THE HEAD TAG.
    
    function isNumber(evt) {
        var iKeyCode = (evt.which) ? evt.which : evt.keyCode
        if (iKeyCode != 46 && iKeyCode > 31 && (iKeyCode < 48 || iKeyCode > 57))
            return false;

        return true;
    }    
    
</script>
</html>

Overview

In the head tag we have the JavaScript block with a function named as “isNumber()”, which takes a Keyboard event as parameter. The parameter will be checked against “Ascii KeyCodes”.
Ascci stands for “American Standard Code for Information Interchange”.

Check KeyCode

Individual key events will be checked for each character entered in the textbox.
The code “var iKeyCode = (evt.which) ? evt.which : evt.keyCode” works like a conditional “if…else”. This piece of code can also be written in this way.
Var iKeyCode;
If (evt.which)
 iKeyCode = evt.which;
Else
 iKeyCode = evt.keyCode; // KEYWORDS ARE CASE SENSITIVE.
The value in iKeyCode will be checked against a range of Ascii codes to make sure it’s a number (numeric value). The function will return “true” or “false” based on the entered value. No value will be displayed on the “textbox” control if the condition is returned as “false”.
The input control in the body tag accepts the user entered values, which calls the JavaScript function “isNumber()” on its “onkeypress” event. So every time a user tries to enter a value other than numbers, the field won’t accept the value.


Thanks
Ziad Sowan

Monday, October 27, 2014

Implement Ajax Control Toolkit with SharePoint 2013


Hi All

After Painstaking research, I managed to make AjaxControlToolkit and especially Update Panel working with SharePoint 2013 using the following steps:


1- Download Ajax Control Toolkit 4.5 from

http://ajaxcontroltoolkit.codeplex.com/releases/view/112805


2- Add (AjaxControlToolkit and AjaxMin dlls) reference to your project.


3- Register AjaxControlToolkit and AjaxMin namespaces in SharePoint package Designer.


  • To Open package Designer see below image


  • To register AjaxControlToolkit and AjaxMin as safe controls see below two images




  • Register AjaxMin dll as above step.

4- Add AjaxToolkit Assembly under assemblies node in Web.Config file of your web application. below line should be added
<add assembly="AjaxControlToolkit, Version=4.5.7.1005, Culture=neutral, PublicKeyToken=28f01b0e84b6d53e" />


5- Replace the default ScriptManager in your masterpage with the ToolkitScriptManager


  • First add below line in head section of the master page
<%@ Register assembly="AjaxControlToolkit, Version=4.5.7.1005, Culture=neutral, PublicKeyToken=28f01b0e84b6d53e" namespace="AjaxControlToolkit" tagprefix="asp" %>



  • Second Remove default script manger then add below ToolkitScriptManager line, make sure its placed inside form section of master page

<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server" EnablePageMethods="false" EnablePartialRendering="true" EnableScriptGlobalization="false" EnableScriptLocalization="true">
               </asp:ToolkitScriptManager>



6- Final step and very important step that's make ajax Update panel control work with sharepoint 2013 , move SPWebPartManager from head section to form section



Finally it should look like below :


<SharePoint:SharePointForm onsubmit="if (typeof(_spFormOnSubmitWrapper) != 'undefined') {return _spFormOnSubmitWrapper();} else {return true;}" runat="server">

<SharePoint:AjaxDelta id="DeltaSPWebPartManager" runat="server">
        <WebPartPages:SPWebPartManager runat="Server"/>
    </SharePoint:AjaxDelta>
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server" EnablePageMethods="false" EnablePartialRendering="true" EnableScriptGlobalization="false" EnableScriptLocalization="true">
               </asp:ToolkitScriptManager>

Note: to use ajax in your webpart add below line in the top of your webpart design

<%@ Register Assembly="AjaxControlToolkit, Version=4.5.7.1005, Culture=neutral, PublicKeyToken=28f01b0e84b6d53e" Namespace="AjaxControlToolkit" TagPrefix="asp" %>


Thanks

Happy AJAX


Ziad Sowan