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.
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..
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