Tuesday, October 6, 2009

Calling a Javascript Function From Aspx.cs File

Javascript Methods are client side codes, so you can not call them in your server side code.

// FALSE FALSE FALSE

Yes, You can call the javascript function from the aspx.cs file. Try the following steps,

Suppose there is a javascript function in your aspx page as shown,

function revealModal(divID)
{
window.onscroll = function()
{
document.getElementById(divID).style.top = document.body.scrollTop;
};
document.getElementById(divID).style.display = "block";
document.getElementById(divID).style.top = document.body.scrollTop;
}
}

In order to call this method from .cs file, you have to use following method,

string str = @"<script language=javascript>revealModal('modalPage')</script>";
this.ClientScript.RegisterStartupScript(typeof(MyPage), "JAVASCRIPT", str);

where MyPage is the name of the class/page

Always find results on Fukat Ka Gyan !!! to save your time…

No comments:

Post a Comment