Coding Cluster - using asp.net, c#, mvc 4, iphone, php, ios, javascript, in asp.net mvc 3 & more
 

Difference between jQuery .bind() vs .live() methods

Tuesday


.bind():
                     The .bind() method registers the type of event and an event handler directly to the DOM element in question. This method has been around the longest and in its day it was a nice abstraction around the various cross-browser issues that existed. This method is still very handy when wiring-up event handlers, but there are various performance concerns as are listed below.

The .bind() method will attach the event handler to all of the anchors that are matched! That is not good. Not only is that expensive to implicitly iterate over all of those items to attach an event handler, but it is also wasteful since it is the same event handler over and over again.

Pros
                     This methods works across various browser implementations.
It is pretty easy and quick to wire-up event handlers. The shorthand methods (.click(), .hover(), etc...)  make it even easier to wire-up event handlers. For a simple ID selector, using .bind() not only wires-up quickly, but also when the event fires the event handler is invoked almost immediately.

Cons
             The method attaches the same event handler to every matched element in the selection.
It doesn't work for elements added dynamically that matches the same selector.
There are performance concerns when dealing with a large selection.
The attachment is done upfront which can have performance issues on page load.

                 

.live():
              The .live method attaches the event handler to the root level document along with the associated selector and event information. By registering this information on the document it allows one event handler to be used for all events that have bubbled (a.k.a. delegated, propagated) up to it. Once an event has bubbled up to the document jQuery looks at the selector/event metadata to determine which handler it should invoke, if any. This extra work has some impact on performance at the point of user interaction, but the initial register process is fairly speedy.

              The good thing about this code as compared to the .bind() example above is that it is only attaching the event handler once to the document instead of multiple times. This not only is faster, but less wasteful,

Pros
              There is only one event handler registered instead of the numerous event handlers that could have been registered with the .bind() method.The upgrade path from .bind() to .live() is very small. All you have to do is replace "bind" to "live". Elements dynamically added to the DOM that match the selector magically work because the real information was registered on the document. You can wire-up event handlers before the document ready event helping you utilize possibly unused time.

Cons
               This method is deprecated as of jQuery 1.7 and you should start phasing out its use in your code.
Chaining is not properly supported using this method.
The selection that is made is basically thrown away since it is only used to register the event handler on the document.

               Using event.stopPropagation() is no longer helpful because the event has already delegated all the way up to the document.Since all selector/event information is attached to the document once an event does occur jQuery has match through its large metadata store using the matchesSelector method to determine which event handler to invoke, if any.
Your events always delegate all the way up to the document. This can affect performance if your DOM is deep.

C#:Remove Special Characters from Text box in ASP.Net

Thursday

Asp.net C# - Replace all special characters from string:        
                                                                                                 "Input string was not in correct format" more or less all the .net programmers facing this issue at least one time in his carrier. Most of the times this issue caused by special characters mingle in data base query and it may entered via input controls. For example single quotes(') in address like "Peter's Road" in that time the SQL query binding with error due to that single quotes.

                         

In this case we need to replace the  special characters from the  input values that produce this error.  In c# the "String.Replace" method used to replace the string values. Here You can replace the user input's  special characters by using the following c# method.

        public static string ReplaceSpecialCharacters(string strInput)        {
            strInput = strInput.Replace("--", "++");
            strInput = strInput.Replace('&', ',');
            strInput = strInput.Replace("%", "[%]");
            strInput = strInput.Replace('+', ',');
            strInput = strInput.Replace("_", "[_]");
            strInput = strInput.Replace("[", "[[]");
            strInput = strInput.Replace("]", "[]]");
            strInput = strInput.Replace("'", "''");
            return strInput;
         }
Just send your input control value as parameter:
string txtAddress = StringHelper.ReplaceSpecialCharacters(txtAddress.Text.Trim());

Get parameters value from .exe files using ASP.NET C#

Tuesday

                                                                                    In one of my project my clients requirements is scanning a document from local scanner and directly upload into the server through web application. But there is no direct method to access local machine from sever code in dot net, so we need some other solutions to access local .exe (to activate scanner) file from server.

Finally Google answering me "ActiveX0bject". Yes we can run .exe file from server by using ActiveX shell command. Here is the sample code to run client's side .exe file from server  by using JavaScript and ActiveX0bject.

Enable ActiveX controls in IE for run .exe files from local :
                                Before  we enable ActiveX in IE settings with the following simple steps, 
Step -1:  Open IE -->Tools --> Internet Options

Step -2:  Go to security tab choose your zone (Internet (OR) Local)  then click custom Level button

Step-3:  Just set "The Initialize and script ActiveX controls not marked as safe" as "Prompt"

          



 And the following JavaScript code is for run .exe (In my case "ImageScanner.exe")file with parameter session id, display order  and user id.

JavaScript Code 

function runScaningApp(sessId, disorder, userId){

var shell=new ActiveX0bject("WScript.shell");

var res = shell.run('"ImageScanner.exe" "'+ sessId +'" "'+ disorder+'" "'+ userId+'", '1', true);

 if (res == "0"){
   alert ("Your success message");
 }
 else{
   alert ("Sorry! Some error occurred");
 }
}

Finally the following c# code for accessing the .exe's param value. Use this code in your .exe's  .cs file 

public class scanner :system.Windows.Forms.Form, IMessageFilter{

private string sessionId;
private string disorder;
private string userId;

 public Scanner (string[args){
  sessionId = args [0];
  disorder= args [1];
  userId = args [2];
 }
}

 
 
 

RECENT POSTS

Boost

 
Blogger Widgets