Pages

Debug Javascript in a funny way

Debugging the javascript code is possible, we don't require any special IDE for debugging, Visual Studio is quiet suffcient. There are two methods to achieve it.
Before starting the debugging process, enable debugging in Internet explorer. we can do that from Internet options.
  • Open Internet option dialog box.
  • Select Advanced tab.
  • In Browsing group, uncheck the Disable script Debugging (Internet Explorer) check box
  • Also the uncheck the Check box next to that for other browsers.
Method One : Write the javascript part in a separate js file, and use the Braek point. so when you run the application, the debugger will prompt near the Break point.

Method Two : Write a line of junk code before one line of your required Break point, when you run the code, it prompts for the error and confirms with you for debugging, click yes and it point to the junk line of code, move the cursor to the next line and use the VS IDE in Break mode as usual.

In VS Debug Mode, we can use the immediate widow and visualize values for varables. But the watch functionalies will not work, but still the intellisence browser will work in VS2005, so that we can explore the objects and their members easily.

To avoid this ODD way, we have wait for the VS 2008 release. The New Javascript Debugging and also our favorite Intellisence is added for javascript also in that edition.

Substitute Update Control with User Control Template

The update control of Ajax is known to everybody, this is dependent of Built-In server controls, so it increases the view state and overload the server.
Let us use the JavaScript to substitute the same scenario without using the server controls.
To create a example, we need to create webpage, web service, and user control with a small class for template purpose.
The user control code looks like this

<%@ Control Language="C#" AutoEventWireup="true"
Codebehind="patient.ascx.cs" Inherits="AJAXEnabledWebApplication.patient"%>
<asp:Repeater runat="server" ID="sample">
<ItemTemplate>
<div>
<table width="100%">
<tr>
<td width="60%"><%# Eval("PatientName") %></td>
<td width="20%"><%# Eval("sex") %></td>
<td width="20%"><%# Eval("DOB") %></td>
</tr>
</table>
</div>
</ItemTemplate>
</asp:Repeater>


Here we are using the repeater control for binding the dataset values. The column names are used in the td for data binding.
The code behind part of user control

public partial class patient : System.Web.UI.UserControl
{
public object data;

protected void Page_Load(object sender, EventArgs e)
{
sample.DataSource = (DataSet)data;
sample.DataBind();
}
}


The public member data used here to bind the grid. This is written in generic way to bind any sort of data. The data will be assigned value later when we load the control in the page.
Let us see what is written in template.

using System;
using System.Web;
using System.Web.UI;
using System.IO;
using System.Reflection;

namespace AJAXEnabledWebApplication
{
public class VuMgr
{
public static string RenderVu(string path,object data)
{
Page tempPage = new Page();
UserControl cntl = (UserControl)tempPage.LoadControl(path);
if (data != null)
{
Type cntltype = cntl.GetType();
FieldInfo field = cntltype.GetField("data");
if (field != null)
{ field.SetValue(cntl, data); }
else { throw new Exception( "Error : " + path + " does not have a public Data property"); }
}
tempPage.Controls.Add(cntl);

StringWriter output = new StringWriter();
HttpContext.Current.Server.Execute(tempPage, output, false);

return output.ToString();

}
}
}

Here System.Reflection is used to bind the data as we have seen before. The method will be called by passing the user control path [virtual path] and the dataset as paramters. The dataset is binded to the field data on runtime. There is an exception written, it will thrown when the member used to bind the dataset is not defined as public. A page will be created on runtime and user control will be loaded on to it and we render all the contents of the page as a string and returns back that value. The service returns the same to client and this string is placed in the of the div element. We have done. You can see a tabular form of data without using any post back, update panel and server controls.[Except Script Manager]
Below is the sample web service code


[System.Web.Script.Services.ScriptService]
public class ServiceA : System.Web.Services.WebService
{

[WebMethod]
public string HelloWorld()
{
return "Hello World";
}

[WebMethod]
public string GetSampleControl()
{

DataSet ds = Data.getdata();
return VuMgr.RenderVu("patient.ascx",ds);
}
}


The HTML code of web page will be

<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true">
<Services>
<asp:ServiceReference Path="ServiceA.asmx" />
</Services>
</asp:ScriptManager>
<div>
<%--<img src="Water lilies.jpg" onclick="TestFunction()" height="100" width="150" />--%>
<input type="button" name="ServiceButton" id ="ServiceButton" value="Click Me" onclick="TestFunction()" />
</div>
<div id="ucSample">
</div>

<script language="javascript" type="text/javascript">
function TestFunction()
{
ServiceA.GetSampleControl(responseFunction);
}
function responseFunction(response)
{
$get("ucSample").innerHTML = response;
}
</script>
</form>
</body>


That's it. Run the application by setting the web page as a start page. click the button to see the tabular Values.. Have fun..

as operator in C#

The as operator is like a cast. it only performs the reference conversions and boxing convertions, the conversion of user defined is not possible, which has to done by casting explicitly. usage of 'as' operator never raises a exception instead it yields null on conversion failure.

Internally it is equivalent to
expression is type ? (type)expression : (type)null


consider a example,


object [] myobject = new object[5];
myObjects[0] =
new MyClass2();
myObjects[1] = "hello";
myObjects[2] = 123;
myObjects[3] = 123.4;
myObjects[4] = null;
......
for (int i=0;
i<myObjects.Length; ++i)
{

string s = myObjects[i] as string;
Console.Write ("{0}:", i);
if (s != null)
Console.WriteLine ( "'" + s + "'" );
else

Console.WriteLine ( "not a string" );
}


output


0:not a string
1:'hello'
2:not a string
3:not a string
4:not a string

Online Resources Available, Use It and Celebrate

Some of the Resource Sites, where we can learn..
[Contribution is not Mine, original link]

ASP.NET Quickstart Tutorial
http://www.asp.net/QuickStart/aspnet/doc/whatsnew.aspx

Virtual Labs
http://msdn.microsoft.com/virtuallabs/asp2/http://msdn.microsoft.com/virtuallabs/fritzonion/
“How Do I” Video Series
ASP.NET HOW DO I Video Series: Caching (Part 1)
ASP.NET HOW DO I Video Series: Caching (Part 2)
ASP.NET HOW DO I Video Series: Create a Full-Featured Customer Login Portal
ASP.NET HOW DO I Video Series: Data
ASP.NET HOW DO I Video Series: Form Building for a "Contact Us" Page
ASP.NET HOW DO I Video Series: Localization
ASP.NET HOW DO I Video Series: Master Pages and Site Navigation
ASP.NET HOW DO I Video Series: Membership and Roles
ASP.NET HOW DO I Video Series: Profiles and Themes
ASP.NET HOW DO I Video Series: Tips and Tricks
ASP.NET HOW DO I Video Series: Web Parts and Personalization

How-To MSDN Articles
How To: Configure the Machine Key in ASP.NET 2.0
How To: Connect to SQL Server Using SQL Authentication in ASP.NET 2.0
How To: Connect to SQL Server Using Windows Authentication in ASP.NET 2.0
How To: Create a Service Account for an ASP.NET 2.0 Application
How To: Encrypt Configuration Sections in ASP.NET 2.0 Using DPAPI
How To: Encrypt Configuration Sections in ASP.NET 2.0 Using RSA
How To: Instrument ASP.NET 2.0 Applications for Security
How To: Improve Security When Hosting Multiple Applications in ASP.NET 2.0
How To: Perform a Security Deployment Review for ASP.NET 2.0
How To: Prevent Cross-Site Scripting in ASP.NET
How To: Protect Forms Authentication in ASP.NET 2.0
How To: Protect From Injection Attacks in ASP.NET
How To: Protect From SQL Injection in ASP.NET
How To: Use ADAM for Roles in ASP.NET 2.0
How To: Use Authorization Manager (AzMan) with ASP.NET 2.0
How To: Use Code Access Security in ASP.NET 2.0
How To: Use Forms Authentication with Active Directory in ASP.NET 2.0
How To: Use Forms Authentication with Active Directory in Multiple Domains in ASP.NET 2.0
How To: Use Forms Authentication with SQL Server in ASP.NET 2.0
How To: Use Health Monitoring in ASP.NET 2.0
How To: Use Impersonation and Delegation in ASP.NET 2.0
How To: Use Membership in ASP.NET 2.0
How To: Use the Network Service Account to Access Resources in ASP.NET
How To: Use Protocol Transition and Constrained Delegation in ASP.NET 2.0
How To: Use Regular Expressions to Constrain Input in ASP.NET
How To: Use Role Manager in ASP.NET 2.0
How To: Use Windows Authentication in ASP.NET 2.0

Authentication and Authorization
How To: Connect to SQL Server Using SQL Authentication in ASP.NET 2.0
How To: Connect to SQL Server Using Windows Authentication in ASP.NET 2.0
How To: Create GenericPrincipal Objects with Forms Authentication
How To: Protect Forms Authentication in ASP.NET 2.0
How To: Use Authorization Manager (AzMan) with ASP.NET 2.0
How To: Use Forms Authentication with Active Directory
How To: Use Forms Authentication with Active Directory in ASP.NET 2.0
How To: Use Forms Authentication with Active Directory in Multiple Domains in ASP.NET 2.0
How To: Use Forms Authentication with SQL Server 2000
How To: Use Forms Authentication with SQL Server in ASP.NET 2.0
How To: Use Windows Authentication in ASP.NET 2.0

Code Access Security
How To: Create a Custom Encryption Permission
How To: Use Code Access Security in ASP.NET 2.0
How To: Use Code Access Security Policy to Constrain an Assembly

Code Review
How To: Perform a Security Code Review for Managed Code (Baseline Activity)

Communications Security
How To: Call a Web Service Using Client Certificates from ASP.NET
How To: Call a Web Service Using SSL
How To: Set Up SSL on a Web Server
How To: Set Up Client Certificates
How To: Use IPSec for Filtering Ports and Authentication
How To: Use IPSec to Provide Secure Communication Between Two Servers
How To: Use SSL to Secure Communication with SQL Server 2000

Configuration
How To: Create a Custom Account To Run ASP.NET
How To: Encrypt Configuration Sections in ASP.NET 2.0 Using DPAPI
How To: Encrypt Configuration Sections in ASP.NET 2.0 Using RSA

Cryptography
How To: Create a DPAPI Library
How To: Create an Encryption Library
How To: Store an Encrypted Connection String in the Registry
How To: Use DPAPI (Machine Store) from ASP.NET
How To: Use DPAPI (User Store) from ASP.NET with Enterprise Services

Deployment Review
How To: Perform a Security Deployment Review for ASP.NET 2.0

Impersonation and Delegation
How To: Implement Kerberos Delegation for Windows 2000
How To: Use Impersonation and Delegation in ASP.NET 2.0

Input and Data Validation
How To: Prevent Cross-Site Scripting in ASP.NET
How To: Protect From Injection Attacks in ASP.NET
How To: Protect From SQL Injection in ASP.NET
How To: Use Regular Expressions to Constrain Input in ASP.NET

Patching and Updating
How To: Implement Patch Management

SQL Server 2000
How To: Connect to SQL Server Using SQL Authentication in ASP.NET 2.0
How To: Connect to SQL Server Using Windows Authentication in ASP.NET 2.0
How To: Protect From SQL Injection in ASP.NET
How To: Use Forms Authentication with SQL Server in ASP.NET 2.0
How To: Use SSL to Secure Communication with SQL Server 2000

Threat Modeling
How To: Create a Threat Model for a Web Application at Design Time

ATLAS (ASP.NET AJAX)
Quickstart Developer Tasks
http://ajax.asp.net/

“How Do I?” Videos
http://ajax.asp.net/default.aspx?tabid=47&subtabid=478#howdoi

Preview Videos
http://ajax.asp.net/default.aspx?tabid=47&subtabid=478#atlas

Samples
http://go.microsoft.com/fwlink/?LinkId=62605

Control Toolkit
http://atlas.asp.net/default.aspx?tabid=47&subtabid=477

Enjoy by doing R&D