Technological Guide.

Download and Install ASP.Net Ajax for Visual Studio

Thursday, June 4th, 2009

AJAX stands for Asynchronous JavaScript and XML. Microsoft ASP.NET AJAX enables us to quickly create Web pages that are rich responsive and familiar user interface (UI) elements. ASP.Net Ajax is integrated to ASP.Net 2.0 sever based development platform. ASP.NET AJAX contains various client-script libraries that incorporate cross-browser ECMAScript (JavaScript) and dynamic HTML (DHTML) technologies. By using ASP.NET AJAX, we can improve the user experience and the efficiency of our Web applications.

Few Advantages of Ajax:

  • web page is improved by performing significant parts the web page.
  • Mostly used UI elements like progress indicators, tooltips, and pop-up windows.
  • Partial-page updates that refresh only the parts of the Web page that have changed.

Availability and Download

  1. Ajax Library 3.5 for ASP.Net framework 3.5 (will be installed with Visual Studio 2008)
  2. ASP.Net 2.0 Ajax Extensions 1.0 (add on for Visual Studio 2005)

ASP.Net 2.0 Ajax Extensions 1.0 can be downloaded from

http://download.microsoft.com/download/5/4/6/5462bcbd-e738-45fa-84ca-fa02b0c4e1c2/ASPAJAXExtSetup.msi

Once you install ASP.Net 2.0 Ajax Extensions, you can install optional ASP.Net Ajax Control Toolkit. The ASP.NET AJAX Control Toolkit is an open-source project built on top of the Microsoft ASP.NET AJAX framework. The AJAX Control Toolkit contains more than 30 controls that enable you to easily create rich, interactive web pages. Some examples are Calendar control, Modal Popup control etc.

ASP.Net Ajax Control Toolkit can be downloaded from here

http://webscripts.softpedia.com/scriptDownload/AJAX-Control-Toolkit-Download-14014.html

The Ajax Control Toolkit comes in zip file which contains AjaxControlExtender.vsi file (in AjaxControlExtender folder) which is a visual studio content installer. Upon clicking the intaller it adds AjaxControl ToolKit to Visual studio in a seperate tab in Tools.

ASP.Net - How to Send PostBacks while using Ajax

Tuesday, June 2nd, 2009

Ajax enabled applications are the frequent requirement now-a-days. Ajax framework gives a desktop feel like user interface on the web page interface itself. And mostly all Asp.Net web developers show their interest to go for Ajax enabled web development rather than non-Ajax web development. Even most of the users like the Ajax enabled websites.

But while working with few controls in Ajax enabled framework, we can’t feel what we expected.

For example when we design a Reporting web page with CrystalReportViewer control using Ajax framework, we can get the report but we can’t print or export or any other functions which are provided by the CrystalReportViewer control. This is because the Ajax framework which handles the Asynchronous postbacks.

To overcome this issue, we can use a very simple technique just by registering in the Page to allow regular PostBack event for the particular CrystalReportViewer control.

Using the above code will give the Ajax user interface while allowing the CrystalReportViewer control to send postbacks.

ASP.Net - Error while exporting to excel in try - catch

Tuesday, June 2nd, 2009

Its so surprising to get error while exporting the asp.net web page into excel when we write the code in try catch block. Actually its not the problem of the try catch blcok. ERROR:Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack

Take a look at the following code.

The above code looks so formal but an error arises…

ERROR:Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack

The above error arises bcoz of the Response.End( ); in the above code which aborts the current thread since the request will be ended. To solve the error, you can either remove the try-catch block or if you need to catch the exceptions then you need to catch for ThreadAbortException besides catching for other exceptions. This ThreadAbortException will be caught for nothing.

ASP.Net - Open Visual Studio from Run Window

Sunday, May 31st, 2009

We can open Visual Studio from Run window by typing “devenv” or “devenv.exe”. Since all the people are busy they can’t feel easy to find the shortcut to Visual Stuido from Programs in the Start bar. So we can open Visual Studio either 2003 or 2005 or 2008 by typing “devenv.exe” in Run window. This works fine when we have only one version of Visual Studio. Suppose if we have installed a new version of Visual Studio in addition to the older one, then when we type “devenv.exe” in Run window, only the lattest version of Visual Studio will be opened.

Suppose Visual Studio 2005 is installed on our pc, it works fine to open Visual Studio 2005 by typing “devenv.exe” from Run window. When we install Visual Studio 2008 in addition to Visual Studio 2005, typing “devenv.exe” in Run window will oepn Visual Studio 2008. This “devenv.exe” is a windows Registry entry maintained by Visual Studio Installer. If we still wish to open Visual Studio 2005 only by typing “devenv.exe” from Run window, just we need to follow the windows Registry tweak as shown below.

Steps to perform ~
1) Open registry editor (type “regedit.exe” in windows Run window)
2) Go to this path
HKEY_LOCAL_MACHINE\SOFTWARE\MICROSOFT\
Windows\CurrentVersion\App Paths\devenv.exe

ASP.Net-How to capture Dynamic controls’ values

Sunday, May 31st, 2009

In an Asp.net web page if we add controls like textbox dynamically during runtime, we can’t capture the dynamic controls’ values (entered by user) as the controls will be disposed when the page loads and renders with the physical controls only. In order to capture these dynamic controls’ values there exists many techniques, one of them is presented in this post.

Controls like text box or lable which can be created dynamically will exist on the webpage until the next post back happens when all the controls will be submitted back to the form of the webpage. Once the subsequent post back happens the dynamic controls will be disposed and the web page will be rendered to the browser with physical controls only, so any code written to access the dynamic controls will raise an error saying object is null.

Here is the code to create and capture the dynamic controls’ values (entered by user).

A webpage is designed with a Panel to which the dynamic controls will be added during run time upon clicking the button “btnCreate”. After the user enters some data in the dynamically created textbox, the entered text will be captured in the click event of button “btnCapture” and it’ll be written to the same webpage using Response.Write.

How to access controls of MasterPage from ContentPage

Monday, May 18th, 2009

While using Master Page concept in our web project, some times we need to access the information of the MasterPage controls from the ContentPage. For example we need to access the LoginId information stored in the Lable control of the MasterPage.

Using VB code in Content Page to retrieve information

Using C# code to change the Master Page Control’s value from Content Page

Redirect to another webpage

Tuesday, May 12th, 2009

Redirecting to another webpage from the current webpage is one of the useful techniques often developers need. Redirecting to another webpage can be setup in the main domain place where it redirects to other sub folders. This Redirecting can be done in many ways. Among them here are present with two most and widely used techniques.

HTML Meta Redirect:
This is a sample web page that does an immediate redirect to another page. Useful for fixing 404 errors

JavaScript Redirect:

Configure SQL Server for ASP.Net session state

Monday, May 11th, 2009

Session state: A session is defined as the period of time that a unique user interacts with a web application. Session state is a collection of objects, tied to a session are stored on a server.

Using SQL Server for ASP.Net session state: Once you start running multiple web servers for the same web site, the default asp.net session state, InProc, is no longer useful, as you cannot guarantee that each page request goes to the same server. It becomes necessary to have a central state store that every web server accesses.
SQL Server offers you centralized storage of a session state in a Web farm. It also offers the transactional capabilities that provide reliability to most relational database systems. You can use SQL Server to save a session. This process is not as efficient as InProc and StateServer mode, because you must store the information in a different process or on a different server. However, this option may be more efficient than using the aspnet_state service, depending on the actual workload and the database configuration. Once you start saving session state to a SQL database it will also persist through web server restarts and reboots.

Installing session state: Open ASP.Net 2.0 Command prompt located at Start–>Programs–>Microsoft .Net Framework SDK v2.0–>SDK Command Prompt and type the following command

  • For Trusted Connection to SQL Server which will be authenticated with windows credentials

“aspnet_regsql -S MyPcServer1 -E -ssadd -sstype p”

  • For Untrusted connection to SQL Server with user id and password

“aspnet_regsql -S MyPcServer1 -U sa -P sa -ssadd -sstype p”

Configuring ASP.Net Web application: To switch ASP.Net to use SQL you must update the <sessionState> element of your application’s Web.config file as follows;

  • Set the mode attribute of the <sessionState> element to SQLServer.
  • Set the sqlConnectionString attribute to specify the connection string to your SQL Server

For example

<sessionState

mode=”SQLServer”

sqlConnectionString=”data source=server;user id=uid;password=pwd”

cookieless=”false” timeout=”20″ />

ASP.Net Page Refresh Problem

Tuesday, April 28th, 2009

When the user refreshes a page in ASP.Net website, the recently taken place action will be repeated again such as calling a button click event / method like adding records to a database with the same values as inserted in the earlier state.

We can overcome this issue by making use of a variable in both Session State and View State collections to store the date and time of the events being called respectively Page Load and PreRender. When a page is first time loaded (!Page.IsPostBack) the date and time is being stored into a session variable as Session["update"] = System.DateTime.Now.ToString(). After the Page Load, before sending the page to the client, a PreRender event is called in which we will assign the Session["update"] value to ViewState["update"].

And when the user clicks the button, even the page load occurs also the Session["update"] will never change as its a postback call only and it directly executes the button code where exists a check to compare the value of Session["update"] to the value of ViewState["update"]. At this point of time the Session["update"] and ViewState["update"] will have the same value as PreRender occurs after the PageLoad. So the code inside the button click will be executed and Session["update"] will be updated with the System.DateTime.Now.ToString() again and always after Button_Click(), PreRender event of the Page will be executed in which ViewState["update"] value is updated with Session["update"].

So whenever the page is being refreshed, viewstate value will become previous value (previous value is taken from viewstate hidden control) which will never match with current session value. So whenever the control goes in button click event, the match condition never gets satisfied hence code related to button click never gets executed.