Monday, March 15, 2010

How to get value of Checkbox list.

Code in aspx

<asp:CheckBoxList ID="chkLstAminities" runat="server" DataTextField="cAmenityName"
DataValueField="nAmenityId" RepeatColumns="3" CellSpacing="10" Width="100%" CellPadding="2"
TabIndex="1" />

Code in aspx.cs


protected void Page_Load(object sender, EventArgs e)
{

DataTable hotelAmenitiesColl = new DataTable();
hotelAmenitiesColl.Columns.Add("AmenityShortDescription");
hotelAmenitiesColl.Columns.Add("nAmenityId");

DataRow dr = null;

dr = hotelAmenitiesColl.NewRow();
dr["nAmenityId"] = "1";
dr["AmenityShortDescription"] = "A1";
hotelAmenitiesColl.Rows.Add(dr);

dr = hotelAmenitiesColl.NewRow();
dr["nAmenityId"] = "2";
dr["AmenityShortDescription"] = "A2";
hotelAmenitiesColl.Rows.Add(dr);

dr = hotelAmenitiesColl.NewRow();
dr["nAmenityId"] = "3";
dr["AmenityShortDescription"] = "A3";
hotelAmenitiesColl.Rows.Add(dr);


chkLstAminities.DataSource = hotelAmenitiesColl;
chkLstAminities.DataTextField = "AmenityShortDescription";
chkLstAminities.DataValueField = "nAmenityId";
chkLstAminities.DataBind();


//Get the Value from the CheckBox List


foreach (ListItem lst in chkLstAminities.Items)
{

string chkValue = lst.Value;
string chkText = lst.Text;
//check for selection
bool bIsSelect = lst.Selected;


}
}

No comments:

Post a Comment