Saturday, April 30, 2011

ASP.NET Client-Side Javascript Programmatically

MethodDescription
RegisterClientScriptBlock
Adds a script block to the top of the page. You create the script as a string, and then pass it to the method, which adds it to the page. You can use this method to insert any script into the page. Note that the script might be rendered into the page before all the elements are finished; therefore, you might not be able to reference all the elements on the page from the script.
RegisterClientScriptInclude
Similar to the RegisterClientScriptBlock method, but adds a script block that references an external .js file. The include file is added before any other dynamically added script; therefore, you might not be able to reference some elements on the page.
RegisterStartupScript
Adds a script block into the page that executes when the page finishes loading but before the page's onload event is raised. The script is typically not created as an event handler or function; it generally includes only the statements you want to execute once.
RegisterOnSubmitStatement
Adds script that executes in response to the page's onsubmit event. The script is executed before the page is submitted, and gives you an opportunity to cancel the submission.

Tuesday, April 26, 2011

SQL-Server Search Entire Databases

This will display all occurrences of the text getSubcategory in all databases in SQL-Server:
 
SELECT distinct(OBJECT_NAME(id)) FROM syscomments WHERE text like '%getSubcategory%'
 
 

Sunday, April 24, 2011

Execute JavaScript Function From ASP.NET Codebehind

Calling a JavaScript function from codebehind is quiet simple, yet it confuses a lot of developers. Here's how to do it. Declare a JavaScript function in your code as shown below:

JavaScript
<head runat="server">
   <title>Call JavaScript From CodeBehind</title>
   <script type="text/javascript">
       function alertMe() {
           alert('Hello');
       }
   </script>
</head>
 
In order to call it from code behind, use the following code in your Page_Load

C#
protected void Page_Load(object sender, EventArgs e)
{
   if (!ClientScript.IsStartupScriptRegistered("alert"))
   {
       Page.ClientScript.RegisterStartupScript(this.GetType(),
           "alert", "alertMe();", true);
   }
}
 
VB.NET 
If (Not ClientScript.IsStartupScriptRegistered("alert")) Then
   Page.ClientScript.RegisterStartupScript(Me.GetType(), "alert", "alertMe();", True)
End If
 
The Page.ClientScript.RegisterStartupScript() allows you to emit client-side script blocks from code behind. More info here http://msdn.microsoft.com/en-us/library/system.web.ui.clientscriptmanager.registerstartupscript.aspx

To call the JavaScript code on a button click, you can add the following code in the Page_Load
btnCall.Attributes.Add("onclick", " return alertMe();");