April 2005 Entries

Loading pages in IFRAME dynamically from codebehind - ASP.NET
Click here for a Video version of this Article Most of us who develop Web Applications would have used an IFRAME during some stage of our lives. IFRAME's are an easy way by which you can embed another page within your original page such that you can show some important information like Stock position/Weather from another site without worrying about the changes happening to that site and updating the same. The Frame can also be used to show another page from your own application. ASP.NET also provides ......

Posted On Monday, April 25, 2005 8:03 AM | Comments (292)

ASP.NET 2.0: The New Compilation Model
One of the excellent features that .NET introduced was the separation of design and code in separate sections by virtue of the code-behind functionality. A little briefing about the above model for the benefit of those who are new to .NET. In traditional ASP, one has to embed his code as well as HTML design within a single ASP Page. That means, the page would look like a chunk of codes and very difficult in debugging and even while developing, for that matter. In .NET, the concept of Codebehind files, ......

Posted On Monday, April 25, 2005 8:02 AM | Comments (0)

ASP.NET 2.0 : Accessing controls in Previous Page
Hi, In my earlier article, I explained how the PostBackUrl property has made the job of posting the page to a different page easy in Whidbey. There is a more effective way of accessing the Controls in the previous page. Its using the PreviousPage property of the Page. Say we have a page Default.aspx with a Textbox "Text1" and a Button "Button1". There are two ways on how we can access the controls in Default.aspx from another page. 1. Setting the PostBackUrl property of the Button to the New Page, ......

Posted On Monday, April 25, 2005 8:02 AM | Comments (14)

ASP.NET 2.0 : Accessing controls in Previous Page
Hi, In my earlier article, I explained how the PostBackUrl property has made the job of posting the page to a different page easy in Whidbey. There is a more effective way of accessing the Controls in the previous page. Its using the PreviousPage property of the Page. Say we have a page Default.aspx with a Textbox "Text1" and a Button "Button1". There are two ways on how we can access the controls in Default.aspx from another page. 1. Setting the PostBackUrl property of the Button to the New Page, ......

Posted On Monday, April 25, 2005 8:01 AM | Comments (5)

ASP.NET 2.0 : Passing data between Pages
Hi, One of the hinderances we had in earlier verions of ASP.NET (1.1 & 1.0 versions) were that, to pass data between pages, we need to use QueryString, Server.Transfer, Sessions etc.,. This was a little surprising for people who come from ASP background who are used to <form method="post" action="newpage.asp"> They can easily get values using Request.Form and posting to new page was as simple as specifying in the action tag the page where they want to post. However, in ASP.NET, by default ......

Posted On Monday, April 25, 2005 8:01 AM | Comments (10)

Button Click Events not firing ? - ASP.NET
Hi, You may be unable to see the button click events firing on certain machines though it may work in some other machines. Mostly when you are developing in your local system, the events do work well. But once you move the code to production, the button click events may not fire at all. This might rise after you install the Microsoft .NET Framework Service Pack 1, which will stop the PostBack Events on client side validation. To resolve this issue, the aspnet_client folder needs to be reinstalled. ......

Posted On Monday, April 25, 2005 8:00 AM | Comments (85)

Using SQL Server Stored Procedure for implementing Custom Paging
HiAll of us would have implemented Paging in our applications.Paging is particularly useful if you have lots of records to be displayed on a page and you can't get them displayed in one stretch. Say we have 1000 records to be displayed in a page. In this scenario, we cannot show up all the records in a single stretch in the page. Hence we need to implement Paging functionality whereby users can see a set of records and then click on a Button/Link to view the next set of records.Paging can be implemented ......

Posted On Monday, April 25, 2005 8:00 AM | Comments (31)

What's New in ASP.NET 2.0?
.NET 1.x versions brought revolution in the Programming World and came up with features which were a blessing to developers with most of the plumbing work like Authentication, Sessions, Caching were automatically managed by .NET. .NET 2.0 version has more to be happy about. For now I will just list the new features in Whidbey and in the subsequent articles explain about each of the features. 1. CLR Integration with SQL Server 2. New Validation Controls, Validation Groups 3. Improved Caching 4. More ......

Posted On Monday, April 25, 2005 7:59 AM | Comments (0)

Moving forward to Whidbey .....
Dear All, Effective from now on, my articles would be focussed on Whidbey, the New Features, Advantages over earlier versions and common problems faced across. However, I would continue responding to your queries which are posted either as comments or mailed to me directly, regarding your doubts, issues with 1.1 version. I thank you all for taking the time to read my articles and look forward to your continued support as always. Cheers and Happy Programming ......

Posted On Monday, April 25, 2005 7:59 AM | Comments (3)

Building Secure Web Applications using ASP.NET 2.0 ("Whidbey")
In .NET Framework 2.0 ("Whidbey"), ASP.NET has undergone a lot of enhancements. Building secure web applications is one of the aspects that deserves good attention. There are many new features introduced such as Login Control, Membership API and Personalization API which helps in increasing the productivity of developers.In .NET 1.1, you can leverage the Forms Authentication to take your anonymous users to the login page. The "Login Page" is your call and you need to code the logic for validating ......

Posted On Monday, April 25, 2005 7:58 AM | Comments (1)

Building Secure Web Applications - ASP.NET
Security is the matter of the moment now! Building secure web applications is an integral part of today's web development owing to the alarmingly increasing number of hacking threats. Some of the key things to keep in mind while building secure web applications are 1. Never expose open SQL Statements in your Code. A statement "select username from users where username='"+ txtUserName.txt +"' and password ='" + txtPassword + "' " can be easily hacked by a malicious user to read as follows:- select ......

Posted On Monday, April 25, 2005 7:57 AM | Comments (7)

Managing Admin / User Accounts for your web applications
No website is complete without a subscription and personalization. Most of the sites provide users to register themselves and have their own accounts, personalized pages etc., On the top of all, it requires an administrator to manage the users and the contents of the site. The first question that comes when developing such applications is to have separate login pages for users and administrators. I find many people asking question on how to have separate Login Pages for Administrators, Premium Users, ......

Posted On Monday, April 25, 2005 7:57 AM | Comments (1)

Including Search for your website documents -Part 1
Search is an integral part of any website and particularly if your site has a large number of documents, providing a search functionality would enhance the quality of the website and easy access to documents. Microsoft Indexing Services can be used effectively to implement search for your websites with less coding and configuration. Indexing Services scan the Documents and provide an easy way for searching within documents. This built-in feature can be made use of effectively for implementing search ......

Posted On Monday, April 25, 2005 7:56 AM | Comments (0)

Including Search for your website documents -Part 2
If you have come to this article without reading the Part 1, please check Part 1 Once you have created the Catalog for your Search, you can use the Catalog for searching. Using the Catalog in your Application 1. In your Search Page, have a Textbox (TextBox1) and a Button (Button1). 2. In the click event of the Button, put the following code. string strCatalog = <Put the Name of the Catalog here>; strQuery = "Select Filename from Scope() where FREETEXT('" + TextBox1.Text + "')"; string connstring ......

Posted On Monday, April 25, 2005 7:55 AM | Comments (2)

Unable to debug .net 1.1 applications after installing .net framework 2.0
.NET Framework 2.0 (CodeName: Whidbey) is the buzz word around and most of us would have installed the BETA versions to try our hands. However, after you install .NET Framework 2.0, if you continue to use Visual Studio.NET 2003 you may be unable to debug the applications developed in .NET 1.1. You may receive the error "Unable to start debugging on the webserver..." The reason is, as a part of installation, the framework registers the 2.0 version with the IIS using the aspnet_regiis.exe -i utility ......

Posted On Monday, April 25, 2005 7:54 AM | Comments (12)

DataReader, DataAdapter & DataSet - When to use?
ADO.NET offers two central Data Access Components. In this Article we will discuss about their features and when to use what.ADO.NET provides two central Data Access Components. The excellent thing is that, they are common across all Databases, be it SQL Server or other competitive databases. Its only the namespace to be used, that differs, while using a Database other than SQL Server. The two Data Access Compnents are: 1. DataSet 2. The .NET data provider, which is a set of components including ......

Posted On Monday, April 25, 2005 7:54 AM | Comments (76)

Fragment Caching - Caching parts of a Page
Caching is one of the recommended techniques to improve the performance of web applications by avoiding repeated requests to the server. It serves the purpose for faster response as well as reducing the load to the Server. Output caching can be enabled easily by just including the following code in the Top of the aspx page <%@ OutputCache Duration="1200" VaryByParam="None" %> This would result in the whole page being cached. However, there will be situations where we would like to cache only ......

Posted On Monday, April 25, 2005 7:53 AM | Comments (5)

SmartNavigation to maintain scroll position across postbacks
Smart navigation is deprecated in Microsoft ASP.NET 2.0 and is no longer supported by Microsoft Product Support Services. This article describes how to implement the smart navigation features in ASP.NET 2.0. For more information check http://support.microsoft.co... Thanks to Deepak for posting this in the comments. This article describes about using the Smart Navigation property to maintain the scroll position of pages across postbacks. All of us know that asp.net controls postback to the ......

Posted On Monday, April 25, 2005 7:52 AM | Comments (13)

Output Caching based on File Dependency
In this article we will see how we can add output caching to our aspx pages and invalidate the cache based on change of a flat file like xml, txt etc., Basically, caching serves the purpose for serving repeated request from the cache such that the number of requests to the Server and the time taken can be drastically reduced. Say, there are about 100 requests for a page in regular intervals. At that time caching would be very useful since it will serve all the requests from the cache and reducing ......

Posted On Monday, April 25, 2005 7:52 AM | Comments (2)

You may receive the Error Message "Server Error in '/application name' Application" while browsing an asp.net application
You may receive the following error message while browsing an asp.net application "Server Error in '/application name' Application ---------------------------... Runtime Error Description: An application error occurred on the server. The current custom error settings for this application prevent the details of the application error from being viewed remotely (for security reasons). It could, however, be viewed by browsers running on the local server ......

Posted On Monday, April 25, 2005 7:51 AM | Comments (83)

Forms Authentication - Redirecting users to a Page other than Default.aspx
In this article I will explain how to redirect users to a specific page rather than the generic default.aspx upon successful authentication of the user. While using ASP.NET Forms authentication, if we try to access a protected page, the user would be taken to the login.aspx page with the ReturnUrl parameter having the path for the originally requested page. Once, the user's credentials are verified, the RedirectFromLoginPage method can be used to take the user back to the originally requested page. ......

Posted On Monday, April 25, 2005 7:50 AM | Comments (22)

Handling Page Load Events and PostBack Issues
A number of queries, articles from developers rise on this issue. When you bind a dropdownlist or other databound controls to a datasource during the page_load event, the process is triggered each time the page is loaded. In the followign code, we call a method which will populate a dropdownlist on the Page_Load Event:- private void Page_Load(object sender, System.EventArgs e) { BindDropDownList1(); } We select an item in the dropdownlist after the page has loaded and the list has been populated. ......

Posted On Monday, April 25, 2005 7:50 AM | Comments (8)

You may receive the error "Parser Error Message: Could not load type 'WebApplication1.Global'." when browsing an asp.net page
You may receive the following error when browsing an asp.net application. Parser Error Message: Could not load type 'WebApplication1.Global'. Source Error: Line 1: <%@ Application Codebehind="Global.asax.cs" Inherits="'WebApplication1.... %> This error occurs when you create a new web application in asp.net using visual studio.net and without compiling the application, you try to browse a page in the application. This occurs because of the Application DLL not having been formed. asp.net ......

Posted On Monday, April 25, 2005 7:49 AM | Comments (72)

You may receive the error "It is an error to use a section registered as allowDefinition='MachineToApplication' beyond application level" in asp.net
You may get this error when trying to browse an asp.net application. The debug information shows that "This error can be caused by a virtual directory not being configured as an application in IIS." However, this error occurs primarily out of 2 scenarios. 1. When you create an new web application using visual studio.net, it automatically creates the virtual directory and configures it as an application. However, if you manually create the virtual directory and it is not configured as an application, ......

Posted On Monday, April 25, 2005 7:48 AM | Comments (29)

The Cool asp:xml control for displaying xml data.
How many of us are aware that we can use the <asp:xml> server control to just specify the xml file and the xslt or other stylesheet file and then without a single line of code, .net does the transformation for rendering in an aspx page? For the benefit of those who are not aware, the following is the code for accomplishing the same:- <asp:Xml id="Xml1" runat="server" DocumentSource="file path to xml file" TransformSource="file path to xslt file"> Put the above code in the aspx page as ......

Posted On Monday, April 25, 2005 7:47 AM | Comments (5)

Selecting Distinct Values using XPath Expression
Hi, XPath is one of the flexible standard for querying an xml document. Let us consider the following CD xml as example:- <?xml version="1.0" encoding="ISO-8859-1"?> <catalog> <cd country="USA"> <title>Empire Burlesque</title> <artist>Bob Dylan</artist> <price>10.90</pric... </cd> <cd country="UK"> <title>Hide your heart</title> <artist>Bonnie Tyler</artist> <price>9.90</price... </cd> <cd ......

Posted On Monday, April 25, 2005 7:46 AM | Comments (11)

Assigning the text value for a Password Textbox
Hi, Setting the text property for a textbox with textmode=single and multiline is pretty straight forward by giving TextBox1.Text = "The text we want to set"; However, if we set the Textmode property to Password, then we cannot simply set the text using the TextBox.text. In practical scenarios, we may not want to set the password for obvious reasons that password is one which we retrieve from the user and it shoult not be set. Also, even if we set some password, the user cannot read it as it is just ......

Posted On Monday, April 25, 2005 7:45 AM | Comments (10)

Setting Role cookie at the Application Level in Role Based Authorization - Forms Authentication
Hi, When using Role Based Authorization, we can set the Roles of a user at the application level by specifying it in the Global.asax's Application_AuthenticateReq... method. As soon as a user is authenticated, it will fetch his roles from the database and assign it to him so that we can use the User.IsInRole("RoleName") to check his role and perform actions based on the same. You can find many resources on the above topic on how to set the roles. However, one disadvantage is that on every page you ......

Posted On Monday, April 25, 2005 7:42 AM | Comments (4)

ASP.NET Application Deployment - Performance Enhancement
Hi, Before deploying ASP.NET applications, carrying out the following steps would ensure the enhanced performance of the application:- 1. In Visual Studio.NET, select the Mode as Release (the drop down in the top center next to the > start icon) and then Build the Application. This would enhance the performance as in the DEBUG mode, the performance would be slow since the DEBUG information has to be written in the pdb file. (If you build in Release mode, the PDB file is not generated). The resultant ......

Posted On Monday, April 25, 2005 7:41 AM | Comments (9)

ASP.NET Deployment - Files to be Deployed
This article refers to ASP.NET v1.x version. For updated version for ASP.NET 2.0, please check http://geekswithblogs.net/r... Many of us have this question of what are the files we need to publish when using FTP to move files to production server, for deploying an ASP.NET Web application. The question obviously arises because when we use Visual Studio.NET to create a web application, there are many files created such AssemblyInfo.cs, Global.asax, Web.config, Proj ......

Posted On Monday, April 25, 2005 7:40 AM | Comments (20)

Denying access to view XML, XSL Files
Hi, We all know that XML is a standard format most of the sites use for managing content. Most of the sites use xml for storing data and use XSL and other stylesheets for displaying the data. The XML files can be browsed as well as a standard web page and the XML format is rendered in the browser. However, if we would like to deny users from viewing our xml format and data by directly browsing the xml file, we can use the HTTP Handlers effectively. The HTTP Handlers provide the flexibility to handle ......

Posted On Monday, April 25, 2005 7:39 AM | Comments (3)

Redirecting users to Custom "Not Authorized" page while implementing Role Based Authorization.
Hi, When using Forms Authentication with Role Based Authorization, we can restrict users based on their role for different directories/pages. However, if an user who doesn't have authorization to view a page, tries to view the same, he will be directed to the Login page with a ReturnUrl parameter, despite the user already being logged in. That doesnt give any idea to the user as to why he is getting directed to the same login page while he has already logged in and doesnt give him the message that ......

Posted On Monday, April 25, 2005 7:38 AM | Comments (11)

"Page cannot be found" when browsing aspx pages in Windows Server 2003 with IIS 6.0
Hi, You may get a Page cannot be found message when you browse aspx pages in a Windows Server 2003 environment. That is because in Windows 2003, all the webservice extensions are "Prohibited" by default to ensure security. To resolve this, do the following steps:- 1. From your Run command, type inetmgr and press enter. 2. Expand the appropriate nodes in the IIS to locate the "Webservice Extensions" Node 3. Click on the same. 4. You will find a list of "prohibited" extensions in the right. 5. Click ......

Posted On Monday, April 25, 2005 7:38 AM | Comments (230)

How to get the SelectedValue of all List controls that are present in a Page
This article describes how to get the selected values of all list controls present in a page using just a single method. One of the common requirements I see as required by developers is to create a Questionarre which consists of many questions with Radio Button Options as answers. To loop through all the radiobuttons and get the selected value of them, its tedious if you write code for each and every radiobutton. The following code accomplishes the task. private void Button1_Click(object sender, ......

Posted On Monday, April 25, 2005 7:37 AM | Comments (2)

"Parser Error Message: Access is denied: Source Error: Line 198: "
Hi, One of the stunning errors you may get while trying to browse your page is Description: An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify your configuration file appropriately. Parser Error Message: Access is denied: 'MyWebApp'. Source Error: Line 198: <add assembly="*"/> As you can see the debug information is quite unclear and it would leave no trace of idea as to where the problem ......

Posted On Monday, April 25, 2005 7:37 AM | Comments (2)

Unable to Start Debugging on the Webserver. The Project is not configured to debug
Hi, You get this error "Unable to start debugging on the webserver. the project is not configured for debugging" occasionally when your aspnet account doesnt have debug permissions. This can happen, if the vs.net installation was not made properly or some reasons when the aspnet account fails to have the necessary rights. To resolve this, re-register the aspnet account with the IIS, by typing the following command from the command prompt:- %windir%\Microsoft.NET\Fram... ......

Posted On Monday, April 25, 2005 7:36 AM | Comments (21)

Securing PDFs, DOCs, in Forms Authentication
Please note this article refers to ASP.NET v 1.x versions. In ASP.NET 2.0, this is taken care automatically. When using Forms Authentication, any anonymous request for secured pages will be redirected to Login page. However, when the request is for static file types like Word, PDF etc., the redirection won't happen and the content will be served. There are many instances when we want to secure our DOCs, PDFs to only authenticated users. The problem is that, these static files are handled by the IIS ......

Posted On Monday, April 25, 2005 7:35 AM | Comments (3)

Excluding Registration Page from Forms Authentication
All of us would agree that ASP.NET Forms Authentication is very useful and effective in implementing a Form based authentication for websites. With the methods and properties it provides, it becomes quite easy for us to implement authentication (contrary to the classic asp, where one has to write chunk of codes individually in all the pages) Well, all of us would have a login.aspx which would be the default login page for the app and any unauthorised request for other pages, would redirect to the ......

Posted On Monday, April 25, 2005 7:35 AM | Comments (12)

Selected Item in Dropdownlist in an Usercontrol
Hi, A Common requirement we have would be to have a Dropdownlist of items which would be common across all the pages (top navigation or in the left navigation). Retrieving the selected item in a dropdownlist which resides in the same page, is pretty straight forward dropdownlist1.selecteditem.... However, when it is in a usercontrol, its pretty difficult to retrieve it since you will get the "Object Reference not set to an instance" error if you try the normal method. Even if you create an instance ......

Posted On Monday, April 25, 2005 7:33 AM | Comments (22)

Filling a Dropdownlist dynamically, with Validation Control Enforced
Hi, Many of the people new to ASP.Net would be excited about the Dropdownlist control which is very useful in appliactions. Howevever, much as it is easy to add items to the dropdownlist with static asp:ListItem tags, adding values to the dropdownlist dynamically is a bit complex and involves a little bit of coding. Also, one of the common issues faced by developers is how to enforce Required Field Validator for Dropdownlist. Herebelow is the code snippet for a Dropdownlist with validator and dynamically ......

Posted On Monday, April 25, 2005 7:31 AM | Comments (9)

"Unable to find script library '/aspnet_client/system-web/1-1-4322/webvalidation.js'"
Hi, One of the common errors that may occur when publishing your application to live server is that you may get the following error. Unable to find script library '/aspnet_client/system-web/... placing this file manually or reinstall by running 'aspnet-regiis-c' This would occur particularly if you are using validation controls in your application. The aspnet_client folder contains the WebUIValidation.js which is required to handle the asp.net validation controls. If the ......

Posted On Monday, April 25, 2005 7:31 AM | Comments (53)

"Visual Studio .NET has detected that the specified Web server is not running ASP.NET version 1.1. You will be unable to run ASP.NET Web applications" - Updated
A very common but weird error we get when trying to create / open asp.net applications using visual studio.net is "Visual Studio .NET has detected that the specified Web Server is not running ASP.NET version 1.1. You will be unable to run ASP.NET Web applications or services." This error occurs primarily due to following reasons 1. IIS installed after Visual Studio .NET is installed. 2. ASPNET Account removed / doesnt have sufficient permissions. 3. Script Mappings not updated in the IIS 4. Proxy ......

Posted On Monday, April 25, 2005 7:30 AM | Comments (323)