≡ Menu




How to use Check boxes in DataList

 This is html code for  datalist

 <asp:DataList ID=”dlist” runat=”server” RepeatColumns=”3″ OnItemDataBound=”dlist_ItemDataBound”                                      Width=”681px” ItemStyle-HorizontalAlign=”Left”>

<ItemTemplate>       

  <asp:CheckBox ID=”chkBlogSites” runat=”server” />

</ItemTemplate>

</asp:DataList>

   

IN Code Behind OnItemDatabound Event look like

 protected void dlist_ItemDataBound(object sender, DataListItemEventArgs e){

if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)

{

// Here we get the reference of check box

   CheckBox lit = (CheckBox)e.Item.FindControl(“chkBlogSites”);

   if (lit != null)

{

//bind it with database field, in my case i bind name feild of table

      lit.Text = DataBinder.Eval(e.Item.DataItem, “Name”).ToString() ;      lit.Checked = true;

         }

      }

    }

     Here the user define method where i get the value of selected textboxes 

 private void GetCheckedBlogs(){//DataListItem _obj = null;//naviagate in datalist item to fine which item is checked foreach (DataListItem _obj in dlist.Items)

     {

//check other than first row where names are mentioned

if (_obj.ItemIndex > -1)

{ 

   CheckBox _checkBox = (CheckBox)_obj.FindControl(“chkBlogSites”);

   if (_checkBox.Checked == true) 

     {

        Response.write(_checkBox.Text);

      }

   }

}

}

{ 0 comments… add one }

Leave a Comment