Thursday 13 March 2014

Difference between SelectedIndex, SelectedValue and SelectedItem in ASP.NET

Controls that uses Collections to store data usually have these three properties. Example of these controls are ListBox, Dropdown, RadioButtionList and CheckBoxList. I have seen that alot of people gets confused with these three properties like how and where to use them.
Each of above controls holds two kinds of data. One is DataValueField and other is DataTextField. DataValueField is something that is not shown to user. DataTextField is shown to the user.

SelectedIndex-
SelectedIndex property always returns the index of selected item in the control. Index always starts from 0. The first item in the control has 0 index and second item has 1 index and so on.

SelectedValue-
Well, this is the property with which people are confused the most. Most of them thinks that SelectedValue property returns the text of the item we have selected. Yes it returns the text BUT only when you have not specified the value separately. The value in these controls is something which is not shown to the user.
Let's take an example, you are adding items into ListBox1 dynamically like-

ListBox1.Items.Add("iknowledgeboy");

When you do like this then the text and value of this item becomes "iknowledgeboy". So, when you say-

ListBox1.SelectValue; 

then you get "iknowledgeboy" for sure. BUT when you are binding your ListBox1 with Database then you need to specify DataTextField and which is DataValueField of ListBox1. At that time, the value of each item in ListBox1 or Dropdown or CheckBoxList or RadioButtonList is usually integer. So, then if you say-

ListBox1.SelectedValue; 

then you won't get the text that you are seeing rather you will get that integer value.

SelectedItem-
It is best method of retrieving the text as well as value. SelectedItem property helps us to get both Text and Value. The problem that people face with this property is they just simply writes-

ListBox1.SeletedItem;

Well, this will give you error for sure. Because it not returning you the text but it is returning you the ListItem. So, if you want to get the text of selected item then you have to write it like this-

ListBox1.SelectItem.Text;

and to get the value of same you have to write-

ListBox1.SelectItem.Value;

This is how SelectedItem works.
Hope you find this usefull.

Thursday 6 March 2014

Creating Thumbnail Image by keeping Aspect Ratio using C#

I am using a large picture (High Resolution) and then I will create its thumbnail.

Suppose that I need a thumbnail whose width should be maximum 200px. Now, while creating a thumbnail of a large image we also need to provide its height. So, how do we get to know about its height by keeping its aspect ratio also. Well, it can be achieved using very simple mathematical equation.

Lets do all this by writing code. I am doing this on a click of a button.

protected void Button1_Click(object sender, EventArgs e)
{
    //----------        Getting the Image File
    System.Drawing.Image img = System.Drawing.Image.FromFile(Server.MapPath("~/profile/Avatar.jpg"));
     
    //----------        Getting Size of Original Image
    double imgHeight = img.Size.Height;
    double imgWidth = img.Size.Width;

    //----------        Getting Decreased Size
    double x = imgWidth / 200;
    int newWidth = Convert.ToInt32(imgWidth / x);
    int newHeight = Convert.ToInt32(imgHeight / x);

    //----------        Creating Small Image
    System.Drawing.Image.GetThumbnailImageAbort myCallback = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
    System.Drawing.Image myThumbnail = img.GetThumbnailImage(newWidth, newHeight, myCallback, IntPtr.Zero);

    //----------        Saving Image
    myThumbnail.Save(Server.MapPath("~/profile/NewImage.jpg"));
}
public bool ThumbnailCallback()
{
    return false;
}
Original Image

Thumbnail Image