Friday, September 4, 2009

Advanced CheckboxList With images

While implementing flights page i came across very interesting Challenge to Dynamically display Airlines list with checkbox .
Asp .net provides Checkboxlist control which do this very efficiently .
But it does not provide functionality to display images as a text .
Here we go how to implement Custom CheckboxList which can accomodate differnt differnt needs
Step 1 :
override the checkboxlists RenderItem method as follows .

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.IO;
using System.Security.Permissions;


namespace Trip.App
{
[AspNetHostingPermission(SecurityAction.Demand, Level =AspNetHostingPermissionLevel.Minimal)]
public class AdvancedCheckBoxList:CheckBoxList
{
//this method constructs customised HTML as per the need .
protected override void RenderItem(ListItemType itemType, int repeatIndex, RepeatInfo repeatInfo, HtmlTextWriter writer)
{
Image img = new Image();
String imgDetails = Items[repeatIndex].Text;
String[] imgInfo = imgDetails.Split('');
img.ImageUrl = imgInfo[0];
img.ToolTip = imgInfo[1];
Items[repeatIndex].Text = "";
//Displays Checkbox
base.RenderItem(itemType, repeatIndex, repeatInfo, writer);
img.RenderControl(writer);
writer.Write("
");
}
}
}
here i just constructed the HTMl i need to render .

Step 2 :
Add tag prefix and add the Custom Checkboxlist on u r page

<%@ Register TagPrefix="AdvCbl" Namespace="Trip.App" Assembly="web" %>
RepeatDirection="vertical" RepeatLayout="Table"
CellSpacing="3" CellPadding="3" CssClass="ctrl" BorderWidth="0" />

Step 3:
Page Load Code

protected void PopulateAirlinesWithBestPrice(List Airlines)
{
foreach (AirVendor airLinesObj in Airlines)
{
ListItem item = new ListItem();
item.Value = airLinesObj.Code.ToString();
item.Text = "../images/air/24x24/" + airLinesObj.Code.ToString() + ".gif" + airLinesObj.Name.ToString();
//Attributes["URL"]
item.Selected = true;
// item.Attributes["ToolTip"] = airLinesObj.Name.ToString();
cblAirlinesFilter.Items.Add(item);
}
cblAirlinesFilter.RepeatColumns = 1;
cblAirlinesFilter.RepeatDirection = RepeatDirection.Vertical;
cblAirlinesFilter.DataBind();
}

Tuesday, March 24, 2009

Ups and Downs

Suddenly started feeling so lonely ,so boared .I am confused is it the same rashmi who is always full of enthusiasam ,ready to accept life as it is and always hopeing for the best .
why this happens that for no reasons we suddenly become upset ?
why the world around us looks so much boaring .
On the otherhand some time there is one more interesting phase in life where without reason we feel good about everything . "Kuch toh hua hai kuch ho gaya hai "
life is full of mysteries