Saturday, December 24, 2011

Dynamic CheckBoxlist in Asp.net

To generate Dynamic CheckBoxlist, use CheckBoxlist Class


Write Code in page_load Event
Example


CheckBoxList chk;
Protected void page_load(Object sender,EventArgs e)
{
            chk=new CheckBoxList();
            chk.Item.Add("cricket");
            chk.Item.Add("hockey");
            chk.Item.Add("chess");
            chk.AutoPostBack=true;
            
            form1.Controls.Add(chk);
                                                   //chkEvent is a method, which get fire when checkbox get checked
            chk.SelectedIndexChanged + = new EventHandler(chkEvent); 
}


Protected void chkEvent(Object sender,EventArgs e)
{
            int i= chk.Items.Count;
            for( int j=0; j<c ; j++ )
            {
                      if ( chk.Item[j].Selected)
                      {
                                  Response.Write(chk.Item[j].Text);
                      }
            }   
}


O/P
When any checkbox is checked,it will display the Text of particular checkbox
Result
if Checkbox of Hockey is checked.
Hockey


For Button: Click to View

No comments:

Post a Comment