AutoList in ASP.Net


By Sanjay Kumar
Printer Friendly Version
View My Articles

  

This small web application has following features: 1. Auto suggest list box [AutoList.js] 2. Auto select text. 3. Scrolling list item either one by one or on the basis of defined page size. 4. Multicolor list item. 5. Getting result in the xml form directly from SQL Server using ‘FOR XML’ and to process it to populate list box or to send back to client. 6. Client side population of list box from xml. 7. Getting result in xml form by using callback function (AJAX). 8. Client side XML tran



This small web application has following features:

  1. Auto suggest list box [AutoList.js]
  2. Auto select text.
  3. Scrolling list item either one by one or on the basis of defined page size.
  4. Multicolor list item.
  5. Getting result in the xml form directly from SQL Server using ‘FOR XML’ and to process it to populate list box or to send back to client.
  6. Client side population of list box from xml.
  7. Getting result in xml form by using callback function (AJAX).
  8. Client side XML transformation using XSLT.
  9. Client side wait/process message while processing data [ProcessMonitor.js].

 

 

Stored Procedures :

(a) sp_GetAllCategories

SET QUOTED_IDENTIFIER OFF

GO

SET ANSI_NULLS OFF

GO

 

ALTER        PROCEDURE sp_GetAllCategories

AS

SET NOCOUNT ON

--- get all categories

          Select

                   CategoryId,

                   CategoryName

          From

                   Categories

          Order By

                   CategoryName

          For XML Auto

--- get all products with related category

          Select

                   CategoryId,

                   ProductId,

                   ProductName

          From

                   Products

          Order By

                   ProductName

          For Xml Auto

 

 

GO

SET QUOTED_IDENTIFIER OFF

GO

SET ANSI_NULLS ON

GO


(b) GetProductOrdersDetail

 

SET QUOTED_IDENTIFIER ON

GO

SET ANSI_NULLS ON

GO

 

ALTER    PROCEDURE GetProductOrdersDetail @ProductID int

AS

SELECT

          Orders.OrderId,

          Convert(varchar,Orders.OrderDate,103) AS OrderDate,

          Customers.CompanyName,

          Customers.Country,

          Convert(varchar,Orders.RequiredDate,103) AS ReqDate,  

          Od.Quantity,

          Od.UnitPrice,

          Amount = (Od.Quantity * Od.UnitPrice),

          DiscountPerc = (100*convert(money,Od.Discount)),

          Discount = (Od.Quantity * Od.UnitPrice)* convert(money,Od.Discount),

          NetAmount=(Od.Quantity * Od.UnitPrice) - (Od.Quantity * Od.UnitPrice)* convert(money,Od.Discount)

FROM Orders

Inner Join

          Customers

On

          Customers.CustomerId = Orders.CustomerId

Inner Join

          [Order Details] Od

On

          Od.OrderID = Orders.OrderID

Where

          Od.ProductID = @ProductID

FOR XML AUTO

 

 

GO

SET QUOTED_IDENTIFIER OFF

GO

SET ANSI_NULLS ON

GO

 

Function to read xml result using ExecuteXmlReader :

public void ExecuteReaderGetXmlDoc(string p_strSPName , SqlParameter[]

p_arrSqlParams, ref XmlDocument p_xmlDoc )

{

SqlCommand l_objCommand;

          StringBuilder l_objStrBuilder;

          XmlReader l_objXmlReader;

          try

          {

             if (this.OpenConnection())

             {

                    l_objCommand = new SqlCommand();

                   l_objCommand.Connection = _objConnection;

                   l_objCommand.CommandType = CommandType.StoredProcedure;

                   l_objCommand.CommandText = p_strSPName;

                   if (p_arrSqlParams != null)

                   {

this.AddCommandParameter(p_arrSqlParams, ref l_objCommand);

                   }

                   l_objXmlReader =(XmlReader) l_objCommand.ExecuteXmlReader();

                   l_objXmlReader.Read();

                   l_objStrBuilder = new StringBuilder();

                   // add root node

                   l_objStrBuilder.Append("<Root>");

                   while(l_objXmlReader.ReadState != ReadState.EndOfFile)

                   {

                             // append xmlNode

                             l_objStrBuilder.Append(l_objXmlReader.ReadOuterXml());

                   }

                   // close root node

                   l_objStrBuilder.Append("</Root>");

                   p_xmlDoc.LoadXml(l_objStrBuilder.ToString());

             }

                            

          }

          catch (Exception ex)

          {

                   throw ex;

          }

          finally

          {

                   l_objCommand = null;

                   this.CloseConnection();

                   this._objConnection = null;

          }

}

 

Function to populate list from xml [ Code behind ] :

 

private void PopulateCategoriesList(XmlNodeList 

p_xmlCategoryNodeList,XmlDocument p_xmlDoc)

{

ListItem l_objListItem;

          int i = 0;

          try

          {

                    foreach(XmlNode categoryNode in p_xmlCategoryNodeList)

                   {

                             l_objListItem = new ListItem();

                             l_objListItem.Value = categoryNode.Attributes.Item(0).Value ;

                             l_objListItem.Text = categoryNode.Attributes.Item(1).Value ;

                             // set item color

                             if (i % 2 == 0)

                             {

                                       l_objListItem.Attributes.Add("class","ListItem");                                           }

                             else

                             {

                                       l_objListItem.Attributes.Add("class","ListAltItem");                              }

                             lstCategory.Items.Add(l_objListItem);

                             i += 1;

                             // remove categoryNode

                             p_xmlDoc.DocumentElement.RemoveChild(categoryNode);

                   }

          }

          catch(Exception ex)

          {

                   throw ex;

          }                 

}

 

Function to populate list from xml [ Client Side ] :                

 

 

if(this.XmlNodeList == null)

{

          alert("NodeList not assigned");

          return false;

}

var nodeListLength = this.XmlNodeList.length;

this.ListCtrl.options.length = 0;

for(var i=0; i<nodeListLength; i++)

{

          var objListItem = document.createElement("option") ;

objListItem.value = this.XmlNodeList.item(i).attributes(1).text;

          objListItem.text = this.XmlNodeList.item(i).attributes(2).text;

// set item color

          if (i%2 == 0)

          {

                    objListItem.style.background = this.ITEM_COLOR;

          }

          else

          {

                   objListItem.style.background = this.ALT_ITEM_COLOR;                                 }

          this.ListCtrl.add(objListItem);

}

this.ListCtrl.selectedIndex = -1;     

 

Getting result from code behind function using AJAX :

 

function getProductOrders(p_intProductId)

{

          try

          {

                    if(p_intProductId != "")

                   {

                             // call displayProcess() before calling send() of XmlHTTP

                             Message.displayProcess("Searching orders ");

          var strURL = "Demo.aspx?IsCallback=1&ProductId=" +

p_intProductId;

                             this.xmlhttp.Open("POST",strURL, false);                  

                             this.xmlhttp.onreadystatechange = process_OrdersResult;                                      this.xmlhttp.send(null);

                   }

                   else

                   {

                             Message.display("Please select product !");

                             return false;

                   }

          }

          catch(e)

          {

                   alert("Error in getProductOrders() : " + e.message);

          }                          

}

         

Transform xml data using XSLT    :

function transformXmlDoc()

{

          try

          {

document.getElementById("tdOrderList").innerHTML = this.xmlProductOrderDoc.transformNode(this.xslOrderDoc);                 

}

          catch(e)

          {

                   alert("Error in transformXmlDoc() : " + e.message);

          }                                                       

}

 

Checking callback on Pageload :

if (!Page.IsPostBack && !IsCallback )

{

          GetAllCategories();

}

else if (IsCallback)

{

int productId = Convert.ToInt32(Request.QueryString["ProductId"]);

          GetProductOrdersDetail(productId);

}

 

Return result back to the client in xml form :

Response.Clear();

Response.ContentType = "text/xml";

if (l_objXmlDoc.SelectNodes("//Orders").Count > 0 )

{

          Response.Write(l_objXmlDoc.OuterXml);

}

else

{

          Response.Write("<Root></Root>");

}

Response.End();

 




button
 
Article Discussion: AutoList in ASP.Net
Sanjay Kumar posted at 14-Apr-07 01:11
Original Article

 
major differeces in C++,c# & vb.net
jyotsna naidu replied to Sanjay Kumar at 30-Apr-07 04:37

hai,

             iam jyotsna . fresher in .net.what is the major differences between C++,C# and vb.net. why most of the people should select  C# to do the projects ,is there any advansed features in c#. this is one of my interview quastion. 


 
Difference between Response.Write() and Response,OutPut.Write ...
jyotsna naidu replied to Sanjay Kumar at 30-May-07 07:46

hi ,

      i want to know what is the purpose of response.outpu.write and difference between response.write and response.output.write.

 

 


promotion
Silverlight    WPF    WCF    WWF    LINQ   
JavaScript    AJAX    ASP.NET    XAML   
C#    VB.NET    VB 6.0    GDI+    IIS    XML   
.NET Generics    Anonymous Methods    Delegate   
Visual Studio .NET    Expression Blend    Virus   
Windows Vista    Windows XP    Windows Update   
Windows 2003 Server    Windows 2008 Server   
SQL Server    Microsoft Excel    Microsoft Word   
SharePoint    BizTalk    Virtual Earth   
.NET Compact Framework    Web Service   

"Everything" RSS / ATOM Feed Parser
How to send and receive messages through message queuing in .Net
How to Read text file as database
SQL Server 2005 Paging Performance Tip
Display code of web page.
Fully Scalable Excel File Importer class for .net using Microsoft Jet driver
Generic Chart Color Manager class that can be used for any charts
Helper class to style the infragistics wingrid
Using Reflection to detemine as Assembly Info in and out.
Helper class to play with Window (Owners and position)
Resolving displayname from the culture using the XmlLanguage and LanguageSpecificStringDictionary class
Manipulate file attributes in VB.NET
Forms Based Authentication Filtered Content Editor for SharePoint
How to create a Tree View of the Windows Folder and extract all the file-folder info.
How to use AssemblyInfo.cs file in win forms to provide much needed information on Assemblies
Sorting In Datagrid
Helper class to work with NativeMethods in the native api's
Silverlight Line Of Business Applications With Offline WPF Versions
C# : Database monitoring system using XML file
C# : Adding ComboBox to ListView SubItem
Sum of Numbers Captcha: Keeping it Simple
C# Create a Piechart for the specified Hard Disk Drive Utilization
Extension Methods for DataSet and DataTable that makes tasks easier
Accessing IIS Hosted WCF Services from PHP
Helper class that provides most commonly used Extension Methods for DateTime object
Helper class to work with a Status Bar in WPF.
Finding Unmatched Records in Dataset Tables Using Linq
Silverlight Toolkit: Autocomplete TextBox Stock Symbols and Chart
COOL Auto Complete textbox using javascript
Creating a Serializable Log Entry for Microsoft Enterprise Library to log to a Database
ASP.NET Searching Values in Datagrid