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" %>
CellSpacing="3" CellPadding="3" CssClass="ctrl" BorderWidth="0" />
Step 3:
Page Load Code
protected void PopulateAirlinesWithBestPrice(List
{
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();
}
No comments:
Post a Comment