Archive

Posts Tagged ‘JavaScript’

Dynamiczne podpinanie zdarzeń w jQuery

January 23rd, 2009

Po przeładowaniu strony, zdarzenia w jQuery nie zawsze się podpinają. Trzeba to zrobić tak:

// Podpięcie zdarzeń po postbacku:
var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_endRequest(bindEvents);
 
// Po załadowaniu się strony.
$(document).ready(bindEvents);
 
// Podpięcie zdarzeń.
function bindEvents()
{
	// kliknięcie na tabeli:
	$("table#tableElements").click(clickOnTable);
}

Obsłużenie zdarzeń po np. kliknięciu na dynamicznie dodane nowe elementy:

// Ustawiam zdarzenia na tabeli. I wtedy nieważne czy się dodało dynamicznie nowe elementy.
function clickOnTable(event)
{
	var target = $(event.target);
 
	if (target.is('a.lnkEditMenu')) return clickEditMenu(target);
	if (target.is('a.lnkDeleteElement')) return deleteElement(target);
	if (target.is('a.lnkEditElement')) return editNoteElement(target);
};

źródło: part 1, part 2

Development ,

HTML encoding in JavaScript

January 15th, 2009

This little function converts HTML characters (for example brackets < >) to entities (like &lt; &gt;).

function escapeHTMLEncode(str)
{
	var div = document.createElement('div');
	var text = document.createTextNode(str);
	div.appendChild(text);
	return div.innerHTML;
}

Source: http://sanzon.wordpress.com

Development ,

How to call WebService via JavaScript

January 13th, 2009
  1. Get webservice.htc (eg. from here),
  2. Copy it to the same directory as the Web page that uses the behavior. By placing the webservice.htc file in the same directory as your HTML page that calls it, you avoid any DHTML behavior-related cross-domain security issues,
  3. Attach WebService behaviour to an element using the STYLE attribute f.e.:
    <body id="webServiceCaller" STYLE="behavior:url(webservice.htc)"></body>
  4. Establish friendly name for WebService:
    <script language="JavaScript" type="text/javascript">
    function loadService()
    {
    	webServiceCaller.useService("http://address.to.your.service/YourService.wsdl", "MyService");
    }
    </script>
  5. Load service with page load:
    <body id="webServiceCaller" STYLE="behavior:url(webservice.htc)" onload="loadService()">
    //...
    </body>
  6. Create handler for WebService result:
    function handleResult (result)
    {
    	if (!result.error)
    	{
    		alert("Success! Result: " + result.value);
    	}
    	else
    	{
    		alert("Failed! Error: " + result.errorDetail.string);
    	}
    }
  7. Call WebService method:
    • Asynchronous
      function myMethodAsync()
      {
      	callID = webServiceCaller.MyService.callService(handleResult, "MyMethod", "Asynchronous Call");
      }

      result.id should be identical to the integer returned by the myMethodAsync() method.

    • Synchronous
       function myMethodSync()
       {
      	var co = webServiceCaller.createCallOptions();
      	co.funcName = "MyMethod";
      	co.async = false;
      	var oResult = webServiceCaller.echo.callService(co, "Synchronous Call");
      	handleResult (oResult);
       }

      For synchronous call you can use onserviceavailable event f.e. for disable buttons or other controls.

    You don’t have to specify result handler, you can insteed handle the onresult event in webServiceCaller.

  8. Whole code listing:
    <header>
     <script language="JavaScript" type="text/javascript">
     function loadService()
     {
    	webServiceCaller.useService("http://address.to.your.service/YourService.wsdl", "MyService");
     }
     
     function handleResult (result)
     {
    	if (!result.error)
    	{
    		alert("Success! Result: " + result.value);
    	}
    	else
    	{
    		alert("Failed! Error: " + result.errorDetail.string);
    	}
     }
     
     function myMethodAsync()
     {
    	callID = webServiceCaller.MyService.callService(handleResult, "MyMethod", "Asynchronous Call");
     }
     </script>
    </header>
    <body id="webServiceCaller" style="behavior:url(webservice.htc)">
     <button id="callAsynch" onclick="myMethodAsync()">Call Asynchronously</button>
    </body>

Based on: Web Services: Calling Service Methods
About WebServices: MSDN: About the WebService Behavior

Development ,

Problems, tips and tricks in ASP.NET and AJAX

January 12th, 2009
  • SecurityTrimming doesn’t  work with Menu control and when Windows authentication is used. Menu becomes invisible or only root node is visible.

Create root node as

<siteMapNode url=”home.aspx” title=”Home” roles=”*”>.

If it still doesn’t work try to add roles attribute to other siteMapNodes.

  • How to refresh an UpdatePanel using JavaScript?

Use __doPostBack() method in JavaScript code:

function someFunction ()
{
 
__doPostBack('UpdatePanel1', '');
 
// or try this if code above doesn't work:
// __doPostBack('&lt;%= UpdatePanel1.ClientID %&gt;', '');
 
}

Development , , ,