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
JavaScript, jQuery
This little function converts HTML characters (for example brackets < >) to entities (like < >).
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
HowTo, JavaScript
- Get webservice.htc (eg. from here),
- 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,
- Attach WebService behaviour to an element using the STYLE attribute f.e.:
<body id="webServiceCaller" STYLE="behavior:url(webservice.htc)"></body>
- Establish friendly name for WebService:
<script language="JavaScript" type="text/javascript">
function loadService()
{
webServiceCaller.useService("http://address.to.your.service/YourService.wsdl", "MyService");
}
</script>
- Load service with page load:
<body id="webServiceCaller" STYLE="behavior:url(webservice.htc)" onload="loadService()">
//...
</body>
- Create handler for WebService result:
function handleResult (result)
{
if (!result.error)
{
alert("Success! Result: " + result.value);
}
else
{
alert("Failed! Error: " + result.errorDetail.string);
}
}
- Call WebService method:
You don’t have to specify result handler, you can insteed handle the onresult event in webServiceCaller.
- 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
JavaScript, WebService
- 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('<%= UpdatePanel1.ClientID %>', '');
}
Development
AJAX, asp.net, JavaScript, Problems