ASPAlliance.com : The #1 Active Server Pages .NET Community The #1 ASP.NET Community
Search   Search

Subscribe   Subscribe

Powered by ORCSWeb Hosting


Site Stats


Powered By ASP.NET
 
Featured Sponsor

Featured Columnist


Featured Book
Professional ASP.NET Web Services
Professional ASP.NET Web Services

Find Prices
Sample Chapter


New! asp.netPRO

We publish our articles in the standard RSS format.

Powerful .NET Email Component

Code Sharing Software

A simple excersise in how to pre-set and retrieve checked checkboxes in a CheckBoxList in a Datagrid by Dennis West

I have been ask how to pre-set checkboxs in a CheckBoxList when it is a control within a Datagrid
 
thanks to: Meor Zaharin Meor Ibrahim for his help.
 
the Source code for the CheckBoxList Event Items are :
void DataGrid1_OnItemdataBound(object Sender, DataGridItemEventArgs e)
{ 
try
{
	CheckBoxList Chk = (CheckBoxList)e.Item.FindControl("checkboxlist1");
	Chk.Items[0].Selected = true; //you can set to the dataset value here
	Chk.Items[2].Selected = true;
						
	//string[] myArray = new string[3] { "Good", "Bad", "So-So" }; 
	//Chk.DataSource = myArray;
	//Chk.DataBind(); 
	//Chk.Items[0].Selected = true;
						
		}
		catch(Exception exp)
		{
				lbl.Text += exp.Message + " : ";
		}
}
    
void Check_Clicked(Object sender, EventArgs e) 
{
  //How can I get here the checkboxes that were checked?
  CheckBoxList chk = (CheckBoxList)sender;
		if (chk.SelectedIndex == -1)
		{
		lbl.Text = "0 Items" + " clicked";
		}else {
			lbl.Text = "Item " + (chk.SelectedIndex + 1) + " clicked";
		}
}

Html code:
<asp:TemplateColumn HeaderText="Selected">
	<ItemTemplate>		
			asp:CheckBoxList id="checkboxlist1" 
			AutoPostBack="True"
			CellPadding="5"
			CellSpacing="5"
			
			RepeatLayout="Flow"
			TextAlign="Right"
			OnSelectedIndexChanged="Check_Clicked"
			
			runat="server">

			<asp:ListItem>Good</asp:ListItem>
			<asp:ListItem>Bad</asp:ListItem>
			<asp:ListItem>So-So</asp:ListItem>

			</asp:CheckBoxList>

	</ItemTemplate>
</asp:TemplateColumn>
Thats it....

Over 3000 ASP.NET Solutions at http://aspalliance.com/dotnetsolutions

Dennis H. West
West Design
ASP.NET Developer
westdh@charter.net  

 Copyright © 2000-2003 ASPAlliance.com  Page Rendered at 11/8/2009 11:32:00 AM