Just got it from ASP.Net Forum and found it very useful for me. Thanks to NC01!!
string resourceName = "YourJavaScriptFile.js";
if ( !this.Page.IsClientScriptBlockRegistered(resourceName) )
{
// If the file is on the server root:
// Ex: http://localhost/YourJavaScriptFile.js
string filePath = System.Web.HttpContext.Current.Request.Url. GetLeftPart( UriPartial.Authority) + "\\" + resourceName;
// If the file is on the app virtual root:
// Ex: C:/Inetpub/wwwroot/Test/YourJavaScriptFile.js
string filePath = System.AppDomain.CurrentDomain.BaseDirectory + resourceName;
// If the file is in a sub-folder inside of the app virtual root:
// Ex: C:/Inetpub/wwwroot/Test/ScriptFiles/YourJavaScriptFile.js
string filePath = System.AppDomain.CurrentDomain.BaseDirectory + "ScriptFiles/" + resourceName;
string scriptText = string.Format( System.Globalization.CultureInfo.InvariantCulture, "\n<script type='text/JavaScript' src='{0}'></script>\n", filePath);
this.Page.RegisterClientScriptBlock(resourceName, scriptText);
}
Note that RegisterOnSubmitStatement, RegisterStartupScript, RegisterClientScriptBlock, etc have changed since version 1.1 and you will get a compiler warning with the above. See http://msdn2.microsoft.com/en-us/library/system.web.ui.clientscriptmanager.aspx for more info.
I just highlighted the property of getting the BaseURL, System.AppDomain.CurrentDomain.BaseDirectory , i've been looking for it before and finally I got an answer. What I do before is put my baseURL in web.Config, but everytime I deploy it on different test site I have to change the value on web.config before publishing.
Search
Custom Search
Subscribe to:
Post Comments (Atom)
this is fantastic. i've been looking for this exact thing for the past few days!
ReplyDelete