Wednesday, July 22, 2009

Call ServerSide Method from Javascript

Call ServerSide Method from Javascript

Sometimes we would require to get data from server without postback the page.


For example, we need to validate the Date of Birth field in which the value should not be greater than current date. In this scenario, we need to get the server date and then check the entered value with current date.

For doing this without postback, we can move into PageMethods concept in AJAX.

* Create a Method with System.Web.Services.WebMethod Attribute in your code behind.

[System.Web.Services.WebMethod]
public static string GetDate()
{
return DateTime.Now.ToShortDateString();
}


* Add the following scripts within <body> Tag

<script type = "text/javascript">
function callMethod() {
PageMethods.GetDate(onComplete, onError);
}

function onComplete(result) {
alert('Today : ' + result);
}

function onError(result) {
alert(result);
}
</script>


* Add ScriptManager and make sure the EnablePageMethods should be True.

<asp:ScriptManager ID = "scr" runat = "server" EnablePageMethods = "true"></asp:ScriptManager>


* Call the callMethod from your button click.

<input type = "button" value = "Validate" onclick = "callMethod();" />


Now you can get the server value without postback....

Things we should keep in mind for doing this:

* The server side method should be static method.
* EnablePageMethods should be true in ScriptManager.
* Scripts should be included within <body> tag, not in <head>.


Happy coding...

FREE Cheat Sheets for Developers

FREE Cheat Sheets for Developers

Here is very useful link in which we can download packet reference books for ASP.Net, Java etc...


you can find Core ASP.Net Refcard
here


Its free but you have to register...

Try it!

Happy surfing...

Thursday, June 18, 2009

Convert your favorite feeds (Blogs) to PDF and Portable

Convert your favorite feeds (Blogs) to PDF and get via Email frequently

   
Tabbloid is an online tool that converts the feeds (Blogs) into PDF. Also we can add feeds as much as you want and it will deliver to your email according to the delivery options you set in tabbloid .




Tabbloid

Friday, June 12, 2009

Blinking Message in window Status bar

Blinking Message in Window Status Bar

Here is the stuff regarding the blinking message displaying in browser window status bar.


<script language="javascript">
var cnt = 0;var timer = 0;
var msg = "Success within You (:-)";
var waitTimer;
var waitUpto;
// To Wait some milliseconds after the message blinking completed
function sleep(mSecs)
{
window.clearTimeout(timer);
var dt = new Date();
waitUpto = dt.getTime() + mSecs;
wait();
}
// This will start again the message blinking.
function wait()
{
var now = new Date();
if (now.getTime() > waitUpto)
{
window.status = "";
cnt = 0;
statusbar();
window.clearTimeout(waitTimer);
}
else
waitTimer = window.setTimeout("wait()", 100);
}

// To show message in blinking style in status bar.
function statusbar()
{
if(cnt == msg.length)
{
sleep(2000);
}
else
{
window.status += msg.charAt(cnt);
cnt++;
timer = window.setTimeout("statusbar()",100);
}
}
statusbar();
</script>


You can check the implementation example here

Happy scripting...