Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I would like to use ASP.net, C# and SQL to display a list of tasks with checkbox to delete a task and radio buttons to set it is done or not. I maked almost everything, But how to add the radio buttons on every road i`ve add? Now there are checkbox for delete, task name and next it would be radio button IsDone and next radio button Not done. Only one could be checked of course. Please help me!


What I have tried:

.aspx code is

<pre><%@ Page Language="C#" AutoEventWireup="true" CodeBehind="to-do-list.aspx.cs" Inherits="ToDoList.to_do_list" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
   <title></title>
</head>
<body>
   <form id="form1" runat="server">
     
       <div>

           
           <asp:CheckBoxList AutoPostBack="true" ID="ToDoListBoxes"  runat="server" OnSelectedIndexChanged="ToDoListBoxes_OnSelectedIndexChanged"></asp:CheckBoxList>
           <br /><br />
           <asp:TextBox runat="server" ID="ToDoName"></asp:TextBox> <asp:Button ID="CreateToDo" runat="server" Text="Add Item" OnClick="CreateToDo_OnClick" />
           
           

       </div>
   </form>
</body>
</html>


and the code behind is

<pre>namespace ToDoList
{
   public partial class to_do_list : System.Web.UI.Page
   {
       protected void Page_Load(object sender, EventArgs e)
       {
           if (!Page.IsPostBack)
           {
               
               PopulateList();
           }
       }
       

       protected void ToDoListBoxes_OnSelectedIndexChanged(object sender, EventArgs e)
       {

           for (int i = 0; i < ToDoListBoxes.Items.Count; i++)
           {
               if (ToDoListBoxes.Items[i].Selected)
               {
                   ToDoSQLDataContext db = new ToDoSQLDataContext();
                   ToDo_Table SelectedItem = db.ToDo_Tables.Where(p =>
                   p.ToDoID == Convert.ToInt32(ToDoListBoxes.Items[i].Value)).FirstOrDefault();

                   db.ToDo_Tables.DeleteOnSubmit(SelectedItem);
                   db.SubmitChanges();
                   PopulateList();

               }
           }

       }

       protected void CreateToDo_OnClick(object sender, EventArgs e)
       {
           ToDoSQLDataContext db = new ToDoSQLDataContext();

           ToDo_Table CurrentToDo = new ToDo_Table
           {
             
               dt=DateTime.Now,
               Name=ToDoName.Text
             
               
       };

           db.ToDo_Tables.InsertOnSubmit(CurrentToDo);
           db.SubmitChanges();
           PopulateList();
           ToDoName.Text = string.Empty;

       }


       private void PopulateList()
       {
           ToDoSQLDataContext db = new ToDoSQLDataContext();

           ToDoListBoxes.DataSource = db.ToDo_Tables;
           ToDoListBoxes.DataValueField = "ToDoID";
           ToDoListBoxes.DataTextField = "Name";

           ToDoListBoxes.DataBind();

       }
   }
}
Posted
Updated 28-Jan-20 7:39am

1 solution

A CheckBoxList will, perhaps unsurprisingly, produce a list of CheckBox controls. It won't let you add other controls within each item in the list.

I'd suggest using a ListView[^] instead.

I'd also recommend against using a checkbox to delete the row. That's not the expected behaviour for that control. It would be better to use the checkbox to indicate whether the item is completed, and a button to delete the item.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900