Friday, June 25, 2010

DataTable To JSON Array

In this post I will discuss about converting any data table in the JSON array. JSON can be very effectively used for Ajax operations since data handling becomes more easier in case of JSON. Here I will not discuss how to use JSON in Ajax. I will simply provide code for converting Data Table into JSON Array.
Remember to add System.Web.Script.Serialization and System.Globalization; namespaces
Let us assume a demo table as below (DOWNLOAD)

For this table JSON output will be as below

Add following functions to build JSON string from an object

Above functions will be responsible to convert an object into JSON variable, now we will write one function which will break the table and call above functions, which in turn update the string builder.

In page load we will simply call following code, this will convert table into JSON string and write it on Page.

In above code I have registered JAVA script block as well, this just for demo purpose; if we put following code we will get an alert with first employee ID.

Download Demo Code Here --> DataTableToJSONArray.zip
Submit this story to DotNetKicks

Read more...

Tuesday, June 22, 2010

Export Grid To Excel

This is simple code to export gridview data in export to excel.
Here I assume that Data table name is “_sampleData” and it is already there. To execute the code I also assumed that button names “Button1” is also present and I have called one function named “GridExportToExcel” on click event of that button.

Following is the function to export Gridview control to excel function has two parameters one is gridview object and name of the excel file. This function generates excel file in “.xls” format.



Download Demo Code here --> ExportToExcelDemo.zip Submit this story to DotNetKicks

Read more...

Monday, June 21, 2010

News Feed - Web User Control

This is very simple code to create web user control to display News Feeds. Using this user can change properties of News Feeds and set his/her own url, number of news to display.

This code is developed in ASP.NET using C#, JavaScript and Simple HTML.

Lets step by step develop web user control for feeds

--------------------- Step 1 --------------------------------
Create one Web User control Named NewsFeed. Copy Past following code in C# file i.e. in ascx file. Just check out Properties in the code.



--------------------- Step 2 --------------------------------
Here we will use google code to read feeds and retrieve data. For more information you can check Google Code.

Put following Code in HTML view of same Web User Control. Following code will include google JSAPI file in your code, and it will load Google Ajax Feed API.



--------------------- Step 3 --------------------------------

We will use following code to set properties of Web User control in javaScript. Check out variables "feedUrl" , "feedNumber"; these variables are set in ASCX file. "feeddiv" is a div tag set in above code. We will display our feed in this div tag.



--------------------- Step 4 --------------------------------

Following code takes care of Feeds retrieval, and manipulate HTML which we are suppose to display in Div tag discussed above.

"display" feed function automatically get called once we retrieve feeds using googleFeed.

"errMessage" is set in ASCX page. Which can be set as property of Web User Control.


 

This is very simple code to develop. Any one with basic understanding of HTML, JavaScript and ASPX can develop web user control to display Feeds.

Download Demo Code here --> NewsFeeds.zip Submit this story to DotNetKicks

Read more...