New Default button and the focus API in ASP.Net 2.0

Hi

One of the cool new features of ASP.NET 2.0 is the Focus API. With the help of the Focus API we can declaratively or programmatically set the focus on a specific control.

The page.SetFocus method is used to set the focus to a particular control. The method takes the ID of a control as a parameter. The ID passed is set the focus on. We can also use the DefaultFocus property of the Form element to define which element will have the focus. Also we can use the focus method of the individual control directly to set the focus.

Similar to the default focus you can also use the Defaultbutton property of the form to define the Default button for the form. The form will submit that button on the click of the enter key inside any input control inside of the Form.

Continued...

How to save and retrieve images from a folder in the server

Hi

Recently 2-3 people asked about how to upload and retrieve images from a folder in the web. Hence I decided to make a post on this.

The idea is to allow the user to upload the images in a folder and then show that image at a particular position. This is particularly required in an ECommerce Site. Where someone will upload the images of the Category, Product etc. The images are then stored in a folder in the web and displayed at the correct position.

First we store the Virtual path to that folder in the Web.config file. This will be helpful if we need to change the virtual path any time. We can just change it in the web.config file and there is no change in the code for that.

Just add the following line in the appsetting section

Continued...

How to read the trace values in Asp.Net

Hi

When we use trace in our web application to keep the trace of the application we also need to view the trace values. If you are using the trace.axd, you will find a View Detail Link to see the details of each request.

The trace detail has many sections to it.

Request Detail

Session Id - The Session Id for this request

Time of Request - The time the request was made

Status Code - The returned status code for this request

Request Type - GET | POST

Request Encoding - Encoding for the request

Response Encoding - Encoding for the response

Continued...

Logging trace values in the Asp.Net Application

Hi

It’s very important to be able to trace your application very easily. We can enable the trace for the entire Application. Enabling Trace at the application level has the effect of enabling Page-level Trace for every page within that application

[Note: This will work provided there is no page-level directive to explicitly disable trace]

ASP.NET runtime also collects several additional statistics, such as the state of the control hierarchy, the contents of session and application state, the query string input values, and other characteristics of a request's execution, when the Application level tracing is Enabled. We can collect the specifics for a specified number of request and also keeps them for either first (Specified number of request or the last specified number of request).

Continued...

How to work with recursive-queries in SQL Server 2000

Hi

Many a time we need a table with hierarchy data. We cannot define the level of the hierarchy at the beginning but is supposed to decide by the data. At these points its best to use a table which stores the hierarchy and display data from that table. One of the better hierarchy is example is the product catalog for an online store.

But SQL Server is a relational database, not a hierarchical database. So, we have to store the data in normalized, relational tables, but come up with programming techniques to process this data in a hierarchical manner. We do not have any built in support in SQL server 2000 for this.

To this we first create a  Category table

Continued...

Converting a string to proper case in Dot Net

Hi

To convert a string to proper case we will have to use the System.Globalization namespace.

To do this we will use the following code.

string myString = "VikRAM’s WebSite HaVinG a post oN pRoper cAsE";

TextInfo TI = new CultureInfo("en-US",false).TextInfo;

Response.Write (TI.ToTitleCase( myString ));

[Note: Generally, title casing converts the first character of a word to uppercase and converts the rest of the letters to lowercase.]

Hope this helps
Thanks
Vikram


How can we use the new beta version of the ASP.Net Ajax

Hi

Microsoft has released their new ASP.NET AJAX Beta release. With this version the Micorsoft.web.ajax.dll is installed in the GAC. So that means we do not have to keep the dll in the Bin folder. But this is a real big problem for people like me who use shared hosting. The hosting provider may not be kind enough to install this version on the server. Which means that I cannot upgrade and use this version.

Why did not Microsoft provide us with the dll. Putting the assembly in the GAC is a good thing but it can be a problem for many. They should have allowed people to have the option to run the assembly from the bin folder also

Continued...

Deploying a windows application in Dot net

Hi

When we create a windows application, deployment is one of the most important issues with it. In dot net deploying a windows application is a very easy task. We can use the one click deployment while the application is in the development phase. But when we need to send the application to the user, the one click deployment is not useful.

We can use the visual Studio’s set-up project to easily create a professional deployment package. I this article I will show how can we create a simple deployment package.

For this purpose we first create a project. The project has only one windows form, which has a button. When you click the button the Application show the current time.

Continued...

Easily Create a Single Instance Windows Application

Hi

Many a time we do not want the user to create the second instance of the (Windows) Application on the same machine. For this we need to restrict the user from opening more than one instance of the application.

The notion of Global and mutex has been introduced by Microsoft to solve this problem. Things like Citrix might fail in condition were you have multi user session (terminal service)

We use the following code to restrict the use of two instance of an application.

  Mutex instanceLock = new Mutex(false, "Name of the Assembly");

Continued...

Adding the clustrMaps to the site so that people can see form where and how many the people are comming and viewing the blog

Hi

After a few days I have added a new feature to the site. My friend (Farghana) talked to me about this nice utility tool about showing the number of visitors coming to your site and from where they are coming. I have used the ClustrMaps.

Adding it to the site is very easy. You have to register to this site and then they will give you the HTML code that needs to be added. They give you three type of html code, Standard, for people with restriction in Hosting and w3c XHTML 1.1 validated.

Its good to show everyone how many people are coming to the site and where they are coming from

Hope this helps
Thanks
Vikram


Passing objects of a class trough the socket

Hi

When we are passing data with the help of Sockets we have to pass the data in byte format. But when we want to use the data we normally use the class structure. So how to work with classes and sockets. Lets say we want to pass a class structure through sockets.

For our example we will take a simple class MessageClass

Our class has three fields and three properties for those fields namely messageType, message, counter

Continued...

Adding and using a resource in dot net

Hi

Many a time we need to add some resources to the windows application, which will be used, by the application during its execution. Like many a time we want to give some sound (.wmv files) with the application, which will be played at certain points in the execution of the Application.

Continued...

Using the new Splitter control in Dot Net 2.0

Hi

A new addition in the Dot net 2.0 Windows control is the splitter control. The splitter control is a combination of many controls with their dock settings. This control can save many steps in the development of a composite control. One of the more common tasks while designing a form is to use controls, which can share the vertical and horizontal space.

Earlier splitter control allowed the application to adjust the division of space separating sections of a container control like panel control. Now we can use the splitter control to eliminate these multiple combined steps.

Continued...

BackgroundWorker Component for Multi Threaded programming in Dote Net 2.0

Hi

The word Multi threaded programming was a deadly thought earlier. It meant some trouble to me for sure. But now in dot net 2.0 multi threaded programming has been made very vary easy.

The new background worker component makes thing very easy. We can use this component for time-consuming processes and the process that can cause the user interface to act very slowly. The background worker runs those on dedicated separate threads. Some of the good example of uses of background worker would be Database transaction, reading large XML file (or any large file), working with web services, working with sockets, file download and upload etc.

Continued...

Microsoft Visual Studio Code Name “Orcas” CTP

Hi

You can get the CTP development tool for Dot Net 3.0 here

Microsoft Visual Studio Code Name “Orcas” Community Technology Preview

Hope this helps
Thanks
Vikram


How and when to use the new Hidden control in asp.net 2.0

Hi

Another new addition to the ASP.NET controls in Asp.Net 2.0 is the hidden Field Control. This control enables a developer to store a non-displayed value.

When we need to persist some value during the posts to server, we can use the hidden fields. It renders a <input type= "hidden"/> element in the HTML. We normally use the view state to store these values. But for some reason, if the view state is not available, then we can use HiddenField Control to store these values. Although this is not visible to the user in the browser, but the HiddenField is rendered in the client browser. Hence it is not suitable storing values, which are security sensitive.

Continued...

Using Bulleted list to display a list of name's or HyperLink

Hi

Another new inclusion in the list of controls in asp.net is the Bulleted List control.

Lets say if we want to show the list of values (For me a good use was to view all the values from the word file for Autosuggest Box), this control will be of great use. You can also format the bullets. To specify individual items that we want to appear in the list we can use the List Item tag also.

BulletedList controls can display list of items with a variety of bullet styles. Use the

BulletedList.BulletSyle property to control the style of the bulleted list.

Here is an example of Bulleted List control

Continued...

A look at the anonymous methods in C#
Hi

You must be aware of delegates if you have worked in C# before. Even then let me describe delegates my way. Delegates are objects that encapsulate the reference to functions. Implementation of Event Handler code is one of the best examples of delegates.

When we double click on a button on the form to add the handler for that button’s click event, the windows form generate two separate codes. The actual event Handler and a hidden code (found in the designer code) to wire-up the button clicks event.

The code outputted is like this.

private void button1_Click(
  object sender, System.EventArgs e)
{ }

and

Continued...

How can we show the full code file to the user

Hi

Yesterday I was thinking what do I have to do if I wanted to show some of my code file directly to the user. The answer was rather very simple.

Use a simple function, which will show the text of the file. You will need the pass the path of the file to the function. This path should be the absolute path and hence you should use the Server.MapPath while calling this function.

Here is the code to the work

Protected string GetCode(string filename)

Continued...

What is the way to rename a file on the web server in code

Hi

A few days back I got a question regarding what is the best way to rename a file in the server (say an uploaded File) using Asp.Net 2.0. According to me the best way to rename a file is by using the System.IO namespace and moving the file to the same location with a different Name.

In C#

Using System.IO
File.Move(@"C:\Dir1\SomeFile.txt", @"C:\Dir1\RenamedFileName.txt")

In VB

Imports System.IO
File.Move("C:\Dir1\SomeFile.txt", "C:\Dir1\RenamedFileName.txt")

[Note: You might get permission error if you do not have the permission to make changes in the folder ]

Hope this helps
Thanks
Vikram


New Auto complete property in windows textbox and DropdownList in dot net 2.0
Hi

A new feature added to the Textbox and the DropdownList control in Dot net 2.0 is Auto complete. This allows the user to select the value from previously entered values. User’s recent history is used to display the values.

To enable the AutoComplete property we can use one the three values.

  • Suggest - Appends the rest of the suggested string to the existing entry. The suggested string appears highlighted.
  • Suggest - Displays a drop-down list populated with one or more suggested completion strings.
  • SuggestAppend - Implements both the Suggest and Append options.

The type of Auto complete values displayed can be controlled by AutoCompleteSource property

Hope this helps
Thanks
Vikram


How to work with file system in asp.net 2.0

Hi

When I made the search Box into and Auto Suggest box I also had to read and write a text file from the code. Hence I decided to blog about it. To deal with the file system we have to use the System.IO Namespace.

To read a file we require an object of the StreamReader Class. Then we use the OpenText method of the File class (System.IO) to open the file. We have to pass the path of the file as the parameter. We read the file in an object of StringBuilder Class. The code looks like this

Continued...

Post-Cache Substitution for optimizing the cached pages

Hi

The partial page caching approach works very well for scenarios where most of the page content is dynamic and/or it is easy to encapsulate cached content into isolated user controls. The concept does not work well for the reverse scenario, when most of page content should be cached and only a very small portion is dynamically built. For example, consider a page displaying static data but only the username displayed is dynamic. In this case it might not be possible to cache the entire page because of the username, which is dynamic.

Continued...

Adding the words for auto suggest box in the cache objects.

Hi

Now I have updated the process of getting the data from the word file (used in Auto-Suggest Box) read the blog here.

I have cached the string of words in the cache objects for 60 seconds and also put a dependency on the text file.

Here is the how the code looks like

System.Web.HttpContext.Current.Cache.Insert

("Words", sb.ToString(),
New System.Web.Caching.CacheDependency(

Continued...
 
Copyright © 2006 - 2010 Vikram Lakhotia