Insert (inject) client script into page in ASP.NET 2.0

November 28, 2007

Insert  (add) javaScript to code behind page as RegisterClientScriptBlock in ASP.NET 2.0

Some time we need to implement client-side functionality to page. To add that client-side functionality to page we need to add (insert) JavaScript (client-side script) into page.

For example if some user is ideal for certain time of period we should pop-up an alert window asking if user would like to continue.

You can insert strings of client side script into application or in some cases you can have separate file (include file). We will discuss both scenarios in our example.

The parameters of RegisterClientScriptBlock are:

1.       Type: The type of calling page.

2.       Key: The key to identify the script.

3.       Script: Content of the script to be sent to the client side.

4.       AddScript: Indicate whether the script should be enclosed within a <script> bloc.

Main.aspx -

<body>   
<form id=”form1″ runat=”server”>
    <div>       
      
<asp:Button ID=”Button1″ runat=”server” OnClick=”Button1_Click” Text=”Call Client Script” />
    </
div>
   
</form>
</body>

Main.aspx.cs

public partial class Main: System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{  
if (!Page.IsPostBack)
    
{
       
string script = “<script> alert(‘current date and time is: “ + DateTime.Now + “‘)</script>”;
       this.Page.ClientScript.RegisterClientScriptBlock(typeof(Page), “MyKey”, script);     
}
  
}
// using external client-side file  
protected void Button1_Click(object sender, EventArgs e)
   
{
    
string scriptUrl = “include.js”;
this.Page.ClientScript.RegisterClientScriptInclude(typeof(Page), “MyKey”, scriptUrl);
}
}

Include.js
alert(“Hello from Javascript”);

When converting from .net 1.1 to 2.0 you get following warning:

‘System.Web.UI.Page.RegisterClientScriptBlock(string, string)’ is obsolete: ‘The recommended alternative is ClientScript.RegisterClientScriptBlock(Type type, string key, string script). http://go.microsoft.com/fwlink/?linkid=14202

To resolve this issue use

Page.ClientScript.RegisterClientScriptBlock(typeof(Page), “MyKey”, script);

-          Harpreet Virk 


Working with Array in ASP.NET (C#)

November 10, 2007

Today we will discuss about One-Dimensional Arrays and and how we can use arrays with ASP.NET.  Arrays are basically lists of sequentially accessible data. They provide a means of accessing and storing data in a way that allows multiple related elements to be manipulated via numeric index.

SampleArray.aspx

<form id=”form1″ runat=”server”>
<
div>
<
asp:Label ID=”Label1″ runat=”server” Width=”306px”></asp:Label><br />
<
asp:DropDownList ID=”DropDownList1″ runat=”server” Width=”306px”>
</
asp:DropDownList><br />
<
asp:DropDownList ID=”DropDownList2″ runat=”server” Width=”306px”>
</
asp:DropDownList> <br />
<
asp:Label ID=”Label2″ runat=”server” Width=”306px”></asp:Label><br />
<
asp:DropDownList ID=”DropDownList3″ runat=”server” Width=”306px”>
</
asp:DropDownList><br />
<
asp:Label ID=”Label3″ runat=”server” Width=”306px”></asp:Label><br />
<
asp:Label ID=”Label4″ runat=”server” Width=”306px”></asp:Label></div></form>
 

SampleArray.aspx.cs

namespace WebApplication1
{
public partial class SampleArray : System.Web.UI.Page
{
//Declaring and Initializing Arrays
static private string[] names = new string[6] { “Paul”, “Harpreet”, “John”, “Andy”, “Jeff”, “Mike” };
static private string[] productNames = {
“Blue Shirt”, “Red Shirt”, “Blue Pent”, “Red Pent” };
protected void Page_Load(object sender, EventArgs e)
{
foreach (string name in names)

{
//Show array items on lable
Label1.Text += (name) + “<br>”;
//populate Dropdownlist with array
DropDownList1.Items.Add(name);
}
//Sort array reversely
Array.Reverse(names);
foreach (string strReverseArray in names)
{
DropDownList2.Items.Add(strReverseArray);
}
// Find array element by array index
Label2.Text = names.GetValue(3).ToString();
//Sort the array
Array.Sort(names);
foreach (string strSort in names)
{
DropDownList3.Items.Add(strSort);
}
//Pass array as a parameter
showNames(names);
//use FindAll to search element
string[] blueProducts = Array.FindAll(productNames, IsBlueProduct);
foreach (string product in blueProducts)
{
Label4.Text += (product) +
“<br>”;
}
}
 private void showNames(string[] names)
{
int counter = 1;
foreach (string personName in names)
{
Label3.Text += counter+“. “+ (personName) + “<br>”;
}
counter++;
}
static bool IsBlueProduct(string productName)
{
if (productName.ToUpper().Contains(“BLUE”))
return true;
else
return false;
}
}
}
-    Harpreet Virk