I have a website that I configured to use https using IIS and have setup the links to point to the https version of the site. However, IIS does not automatically redirect the http version of the site to https and instead display error messages on the screen. This is bad as other sites had linked to the http version of prior to setting up https.
January 30, 2015
January 8, 2015
In this post, I will show how to read a single file from a password protected zip file and then display it on a web browser. This scenario might be more required in a windows application but since the focus is to read from the zip file than to display it on the web browser, I will go ahead with browser.
To read from the zip file, I am using a NuGet package - SharpZipLib. This library can ne used to read a password protected zip file. If you are not familiar on how to add or manage NuGet packages, then check the NuGet Docs. After the package is added, I will use the ICSharpCode.SharpZipLib.Zip to read the zip file.
December 30, 2014
Xml files can be loaded into a GridView control in asp.net. To load xml files, it can be usually binded to the GridView by creating the xml data source and then setting the data source property of the GridView. However, if xml files contains nodes, then it does not work and instead produces error -
The data source for GridView with id 'GridView1' did not have any properties or attributes from which to generate columns. Ensure that your data source has content.
To bind the xml file with nodes, it's easier to create a dataset and then to bind it to the gridview from code behind. Here is an example of how that would work.
December 17, 2014
Asp.net provide option to upload files like images using FileUpload control but how can we find out certain properties of the image like file size and dimensions. In this post, I will show how to find those properties.
First, in the design view, I got a FileUpload control and a Button. The image will be uploaded when the button is clicked.
In the code-behind, I am using System.Drawing.Image to get image dimension and ContentLength property to get the file size as shown below.
August 20, 2014
I have this asp.net web forms application through which users can log on and view a number of records from a database and then make modification as needed. However, as the number of records have increased for users; for some users with a large number of records, the site is failing with the message "form data is not valid".
Since the issue is happening only for large accounts, I was somewhat certain that it's related to the number of form elements that can be posted back to the server. Microsoft introduced an upper limit to the number of the form elements that can be posted back to the server - http://weblogs.asp.net/scottgu/archive/2011/12/28/asp-net-security-update-shipping-thursday-dec-29th.aspx. So, to fix the issue, I added a new app setting in web.config file called "aspnet:MaxHttpCollectionKeys" and set its value to a number - 5000. And that's it - the site started working fine again.
Just to be clear though, I think this is a temporary fix and the application should be modified anyway as sooner or later that new limit might be reached as well.
July 18, 2014
I deployed an asp.net application but when I ran the application, it failed with the message "Could not load file or assembly 'Microsoft.ReportViewer.WebForms, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified." . I re-checked the web.config file and it had the following line under assembly section
July 16, 2014
Here is a scenario in an application where the user logs out which takes them back to the login page but when the user hits the back button, he/she can view the previous page. During the log out event, Session is cleared and caching is disabled but the previous page is still shown. So, how can we work around this?
The browser history is an option to go back to previously visited pages (both forward & backward). The browser caches psges visited and the history buttons - backward and forward can be considered as a pointer that is linked to the page previously visited by the user.
So, to fix the issue, we need to make sure the pages don't get cached. Since the pages are behind authentication - it makes perfect sense not to cache the pages at all. We can implement the caching or not caching in the following ways in asp.net.
In the above code, you can paste the code in the master page for the authenticated pages or alternatively, if there is no fixed master page for authenticated pages - then, it needs to be places on all all required pages.
July 10, 2014
May 29, 2014
In the previous post, I showed how to save an image to a database. In this post, I will show how to retrieve an image and display it in an asp.net page. Note that the image is saved in bytes format rather than as a file and displaying it in an asp.net Image control requires more work. Therefore, in this post, I have used the html img element to display the image.
May 26, 2014
May 7, 2014
This will cause the browser to load the specified page. The page may or may not be part of the existing application. This will result in a http get and the new page will not have access to any data on the current page. This can be triggered by any control that performs postback or even on Page_Load event.
Response.Redirect can be set in the following way.
PostBackUrl property can only be set on an asp.net button control. The UseSubmitBehavior property needs to be set to true. This will result in a http post and the new page can retrieve current page's data using Request.Form and Page.PreviousPage. The user will notice the change in the once the button is clicked. PostBackUrl can contain pages from current application or any other pages as well.
PostBackUrl is set in the following way.
To retrieve data,
Server.Transfer performs a http post and data can be obtained similar to PostBackUrl, that is, using both Request.Form and Page.PreviousPage. Server.Transfer can only transfer to a page in the current application. Also the url does not change in the browser. Like Response.Redirect, this can be triggered by any control that performs postback or even on Page_Load event.
Server.Transfer can be used in the following way.
To retrieve data,
May 6, 2014
I wrote this jQuery validation for a site and it all worked in my test page but for some reason it stopped working in the real page. Since the real page is a currently live, I started creating and testing the validation on the dummy page first. After some debugging for finding the issue, I realised that the jQuery is not running within the actual page since the controls and html elements are within the Update Panel.
May 1, 2014
Most (if not all) web sites will have a default start page for example, default.aspx, index.aspx, home.aspx, etc. The default start page kicks in when someone goes to a url that does not refer to the page (no file extension) but possibly to a folder - for example, example.com/ or example.com/test . Without the default page, the user will be shown or not shown the directory listing page depending on the IIS configuration. For security reasons, it's better not to show end users the directory listing page. I once saw an application with directory listing that was showing the log folder and files.
April 29, 2014
Data attributes are a cool way to store extra data for html elements. Previously, when we needed to store additional data for an html element we would store it hidden elements whereas now with data attributes, we can store it within the html element itself as shown below.
Since data attributes are standard html attributes, we can use it within asp.net as well. Here's an example on how we can load country / capital data within a ListView control, store the capital information as data attribute and then display the capital when the user hovers over the country using jQuery.
April 22, 2014
The Handler basically gets the files uploaded and the saves it to a specified folder. In this instance, I am saving it to the UserFiles folder within the application. Make sure you have already created the folder with the correct permissions (read / write attributes). Here's the source code for the handler.
April 16, 2014
Enum in C# is a quick way to store constants. We can also assign value like int or chars to the constants. In other words, we can have a basic key - value sort of scenario with enums. Okay, so, how can I use a enum and bind it to a dropdownlist in asp.net.
To analyse what we need to do, we basically need to pull the names and the values from the enum and say a particular key has a particular value. Enums do not have a way to export the data to a list but it can export the list of names and list of values separately. The lists produced are in the same order and so we can say list[value] belongs to list[key].
April 4, 2014
April 1, 2014
Ok, this one is interesting and not something that I prefer doing but on one the tasks that I was working on - I had to call a method on an aspx page from a user control within the page. It's not good practice to call page's methods from user controls as the user control should not have to know the page where it can be used. So, how can we do it.
March 31, 2014
In asp.net, there are different way to combine urls with the easiest way to simply add multiple strings and the better way to use classes and methods already from asp.net. In this post, I will show different way to add multiple url parts to form a valid url.
In asp.net there are few classes with urls can be constructed like the Uri class. However, I actually prefer the use of UriBuilder class as it provides more options to specify the parameters like host, query string, anchor links, port number and so on. This is much easier class to work with than to build urls with other classes. So, below is the source code for two ways to build urls - one with concatenating strings and another with UriBuilder class.
March 30, 2014
I can do lot of cool things with asp.net but sometimes I don't like the idea of postbacks, specially when working on large pages. Imagine a page with a number of controls and every postback will send the controls data back and and forth and recreate the controls. So, how can I write asp.net and call methods on code-behind from aspx pages without using postbacks.