Passing Dictionary From JavaScript to ASP.NET Web Service

The ASP.NET project that I’m currently working on uses AJAX to save data to a Web Service.  The web service is generic.  The web page should be able to pass address, phone numbers, sales info and etc to the web service.  Base on the key that is also passed to the web service, the web service should know how to save the data.
 
The first version of the client code that called the web service gathered all the data from the form and created a string that was pipe delimited.  Example: key=person|keyid=333323|firstname=John|lastname=Handcock.   When this information arrives at the web service it must be parsed and saved. A lot of red flags seem to pop up with this design.
 
Here’s the web service signature or the first version
[ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet=false)]
public string Save(string input)
 
There will always be a few fields that will be passed (key and keyID) to the web service.  The problem is that data fields (firstname, lastname, address and etc) could be different.  I figured out that I could add .NET Dictionary type to as a parameter to web service call.  The problem now is how do I gather and pass this information from the client javascript.
 
Here’s the the new save method web service
[ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet=false)]
public string Save(string key, string keyID, Dictionary<string, string> inputNameValue)
 
I could use the following JavaScript to pass data to the web service and it works, but the data values (first, last) need to be created dynamically. 
MoFormSave.Save(key, keyID, {"first":"john","last":"handcock"})
 
I tried to create an array that accepts an NameValue object and pass the object to web service, but this errors when I call the web service.  I finally figure out I could do the following and pass the object to the web service.
 
   var nameValue = {}; //new, empty object
   nameValue[‘first’] = ‘John’;
   nameValue[‘last’] = ‘Handcock’;
   MoFormSave.Save(key, keyID,  nameValues)
 
 
Resources:
Quick guide to somewhat advanced JavaScript – tour of some OO features
 
 

JavaScript Modal While Processing

On the current ASP.NET project that I’m working on, I’m using AJAX to save the user input to a WebMethod(WebService).  While the save is running the user needs to be prompted that their data is being save.  At this time the user should not be able to do any other action on the page until the save has completed. 
 
Below are the resource that I used to solve this problem. All the resources but the first were fairly complex.  The first resource did a very good job of  solving my problem.  I listed the other resource because they provided some additional information.
 
Resources:
Modal-style pops in Javascript and CSS (The simplest and it works; works in IE7 and FireFox 3)
http://weblogs.asp.net/jeff/archive/2006/10/25/Modal_2D00_style-pops-in-Javascript-and-CSS.aspx
 
Lightbox Gone Wild (JavaScript Libary) (Looks fairly complex)
http://particletree.com/features/lightbox-gone-wild/
 
 
Javascript Modal Dialog Tutorial (Discusses issues that browses have)
http://luke.breuer.com/tutorial/javascript-modal-dialog.aspx
 
 
Modal Dialog Box – modalDialog.js (JavaScript Library)
http://www.applicationgroup.com/tutorials/modalDialog/modalDialog.asp

Cool Tools and Tips

Every time I rebuild my computer I can never remember the tools that I installed and the custom changes i make to the environemnt.  Also I added a few links to others who have similar lists.
 
 
 
 
David Yardy.

ASP.NET Debugging Stop Unexpectedly

My environment: Visual Studio 2008 (VS.NET), Vista, IIS7, ASP.NET. 
I have a situation when I’m debugging ASP.NET and after about 90 seconds of stepping through code the debugger suddenly stops and detaches.
 
Based on info from others, it seems that the underling problem is Vista and Internet Information Server 7 (IIS7).  There are two setting in IIS7 for Application Pools that can create this problem: Ping Enabled and Ping Maximum Response Time.  The default for Ping Maximum Response Time is 90 sec.  By changing these values should resolve the problem.  The references below probably provides a better overview of the problem.
 
Reference:
 
My web application times out when debugging in IIS7

Page.ResolveUrl(“~/ClientScripts/Validation.js”) – Web Application Root Operator

I have the situation where I need to load a JavaScript dynamically.  All the JS files are in the ClientScript directory (http://www.somewebsite.com/ClientScripts/Validation.js).  The ASPX page that references the client scripts could be anywhere, multiple folders (directories) down in the site or in the application root.  I would like a simple way of referencing the the application root.  The magic to this is the ~ (Tidle). The ~ resolves to the root of the current application. 
 
The following line allows me to reference the application root
this.ClientScript.RegisterClientScriptInclude("Validation.js", Page.ResolveUrl("~/ClientScripts/Validation.js"));
 
And here is the results in the HTML file.
<script src="/ClientScripts/Validation.js" type="text/javascript"></script>

ASP.NET AJAX WebServices and ScriptServices

I’ve been working with ASP.NET AJAX for the past week and the following resources allowed me to get up to speed pretty fast.  The following two books that I list are pretty good.   Both of the them cover some different information.  I believe the Professional ASP.NET 2.0 AJAX is a little easier to read and the examples are much better. 
 
Video – How Do I: Use an ASP.NET AJAX ScriptManagerProxy?
 
Video – How Do I: ASP.NET AJAX Enable an Existing Web Service?
 
Video – How Do I: Make Client-Side Network Callbacks with ASP.NET AJAX?
 
Video – How Do I: Choose Between Methods of AJAX Page Updates?
 
MSDN Article – Using ASP.NET Session State in a Web Service
 
Book – Introducing Microsoft ASP.NET AJAX (Dino Esposito)
 
Book – Professional ASP.NET 2.0 AJAX 
 
 

AJAX and JavaScript in ASP.NET & Visual Studio 2008

I have once again started working with AJAX.  About 8 months ago I was pretty deep into ASP.NET and AJAX, but since then I’ve done little with AJAX.  I’ve been finding many good reference for AJAX and do not have a common place to keep all these reference.  So for convince I’m going to keep the references here. 
 
 
Create Control Extenders
Using Visual Studio 2008, ASP.NET 3.5, and AJAX
(I went through the example in this article and everything seemed to work fine)
 
CodeProject – ASP.NET AJAX Controls and Extenders
4.36 out of 5. 
(This article looks very promissing)
 
Building ASP.NET 3.5 AJAX Extender Controls
 
MSDN – JScript IntelliSense Overview
 
WebCast – ASP.NET AJAX Support in Visual Studio 2008
(Good video)
 
WebCast – IntelliSense for Jscript and ASP.NET AJAX (in Visual Studio 2008)
 
JScript IntelliSense: A Reference for the “Reference” Tag 
/// <reference name="MicrosoftAjax.js" />
 
 
 

When Leaving Page, Prompt The User That Changes Have Not Been Saved

I’m in the process of create a few input forms in ASP.NET (Name, address, and etc).  If the user makes a change to the page and tries to navigate away from the current page without previously clicking save, the current page should prompt the user that there are changes to the page and does the user want to continue to navigate to a different page.  The prompt should be triggered by any of the following:  selecting a link, closing the window, entering a new URL in the address bar, selecting back, hitting refresh or other means. 
 
To solve this problem I first tried using onunload, but could not find any way of stopping the user from going to another page.

[sourcecode language=”xml”]
body onunload=”ConfirmExit”
[/sourcecode]

After a little research I found a method window.onbeforeunload.  window.onbeforeunload provides the developer a way to notify the user that there has been changes that have not been save before navigating to a different page.  In the following example I use ASP.NET instead of strait HTML.  The code behind loops through every webcontrol that’s in the form and wires up the control’s onchange JavaScript event to call InputChange function. If the user changes any of the controls the isDirty variable is set to true. 
 
As you can see in the following code the window.onbeforunload is set to ConfirmExit function. This is the part that is used to notifies the user. Any time the user tries to exit the page the ConfirmExit function will be called.  In the ConfirmExit function, if the isDirty variable is true, the user should be prompted with a message to confirm if they want to leave the page.  If isDirty is false the the user is redirect without a prompt.  
 
If the user clicks save button, I do no not want him to be prompted that the data has not been save. So I set the following for the button control.

[sourcecode language=”javascript”]
OnClientClick=”isDirty=false”
[/sourcecode]

 
I tested the following code and confirm it works in IE7 and FireFox 2.0

[sourcecode language=”csharp”]
public partial class TestPromptSave : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack == false) {
SetupJSdataChange(form1);
}
}

private void SetupJSdataChange(Control parentControl) {
foreach (Control control in parentControl.Controls) {
//Response.Write(control.ID + “<br>”);
if (control is WebControl) {
WebControl webControl = control as WebControl;
webControl.Attributes.Add(“onchange”, “InputChanged(this)”);
}
}
}
protected void Page_Save(object sender, EventArgs e) {
}
}
[/sourcecode]

HEre some more

[sourcecode language=”xml”]
<%@ Page Language=”C#” AutoEventWireup=”true” CodeFile=”TestPromptSave.aspx.cs” Inherits=”TestPromptSave” %>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head runat=”server”>
<title>Untitled Page</title>
</head>
<body >
<script type=”text/javascript” language=”javascript”>
var isDirty = false;
window.onbeforeunload = ConfirmExit;

function InputChanged(control)
{
isDirty = true;
}
function ConfirmExit()
{
if(isDirty == true){
return “You have attempted to leave this page. If you have made any changes to the fields without clicking the Save button, your changes will be lost. Are you sure you want to exit this page?”;
}
}

</script>

<form id=”form1? runat=”server” >
<a href=”RedirectPage.aspx”>RedirectPage.aspx</a>
<br /><br />
<table>
<tr>
<td>
First Name
</td>
<td>
<asp:TextBox ID=”txtFirstname” runat=”server” />
</td>
</tr>
<tr>
<td>
Last Name
</td>
<td>
<asp:TextBox ID=”txtLastName” runat=”server” />
</td>
</tr>
<tr>
<td>
State
</td>
<td>
<asp:DropDownList ID=”ddlState” runat=”server” >
<asp:ListItem Text=”" Value=”" />
<asp:ListItem Text=”CO” Value=”CO” />
<asp:ListItem Text=”TX” Value=”TX” />
<asp:ListItem Text=”FL” Value=”FL” />
<asp:ListItem Text=”OK” Value=”OK” />
</asp:DropDownList>
</td>
</tr>
</table>
<br />
<asp:Button ID=”butSave” runat=”server” Text=”Save” OnClientClick=”isDirty=false” onclick=”Page_Save” /> &nbsp; | &nbsp;
<asp:Button ID=”butCancel” runat=”server” Text=”Cancel” />
</form>
</body>
</html>
[/sourcecode]

Resource:
MSDN – onbeforeunload Event
 
4 Guys From Rolla.Com – Prompting a User to Save When Leaving a Page
http://www.4guysfromrolla.com/webtech/100604-1.shtml
 
The IE OnBeforeUnload Event Handler
 
Reminding Users to Submit Forms
 
IE runs onbeforeunload twice for no reason?
 
Warn users about unsaved changes in a web form

SQL Server Searching Object Text

I had a situation where soft deletes were going to be implemented in the database, but for the past year hard deleted were being implemented.   On every table the IsDeleted column was created.  The newer Stored Procedures, Views, and Functions included IsDeleted in there WHERE statement, but all the db object that were created prior to implementing the IsDeleted needed to be changed to include the IsDeleted. 
 
The following was my first attempt to identify DB objects that did not include IsDeleted.  This seemed to return the correct data. But, there seem to be a problem.  There were situations where the results that were returned included duplicate names.  Come to find out the [Text] column that is returned from syscomments is NVARCHAR(4000).  If the [Text] of DB object exceeded 4000 characters an additional records was created every additional 4000 characters.   There were a few stored procedures that surpassed 4000 characters and in-turn had duplicate Object_names returned in the results .  This is not that big of a deal, but I wasn’t getting back the results I expected. 
 

SELECT OBJECT_NAME(id), *

FROM syscomments

WHERE

(

OBJECTPROPERTY(id, ‘IsScalarFunction’) = 1

OR OBJECTPROPERTY(id, ‘IsTableFunction’) = 1

OR OBJECTPROPERTY(id, ‘IsProcedure’) = 1

OR OBJECTPROPERTY(id, ‘IsView’) = 1

)

AND [Text] NOT LIKE ‘%IsDeleted%’

 
I found a solution to my problem by referencing sys.objects table and using the function OBJECT_DEFINITION, which returns the Transact-SQL source text of the definition of a specified object.  OBJECT_DEFINITION is not limited to NVARCHAR(4000), but instead is NVARCHAR(MAX).  I probably could have referenced INFORMATION_SCHEMA.Tables, but decided not to mess with it today and just wanted to get it to work.
 

SELECT

Name, *

FROM

sys.objects

WHERE

(

  OBJECTPROPERTY(object_id, ‘IsScalarFunction’) = 1

  OR OBJECTPROPERTY(object_id, ‘IsTableFunction’) = 1

  OR OBJECTPROPERTY(object_id, ‘IsProcedure’) = 1

  OR OBJECTPROPERTY(object_id, ‘IsView’) = 1

)

AND OBJECT_DEFINITION(object_id) NOT LIKE ‘%IsDeleted%’

 
 
 
References:
SQL Server OBJECT_DEFINITION (Transact-SQL)
 

SQL Server sysobjects

 

Searching Syscomments Accurately (View the Comments section)

 

SQL Server Custom Shortcuts

Querying the INFORMATION_SCHEMA  (But has info about SQL Server Shortcuts)
This is where I originally found info about SQL Server shortcuts.
 
Assigning shortcuts to commonly run tasks in SQL Server 2005 Management Studio
 
SELECT * FROM, sp_helptext ShortCut for SQL Query Analyzer
 
 
SQL SERVER – Query Analyzer Shortcuts (SQL Server default shortcut keys)
This is probably my favorite SQL Server web site