Timeslot html table - How to get the checkbox values?

Hi, I’m new here and am not quite sure if this is the correct place to post. But I’m having difficulties with the issue below.

Let’s say I have a timeslot table, with 3 time slot - full day, morning shift and afternoon shift. And then users are able to tick among these 3 shifts for Monday - Friday. Users are only allowed to tick one checkbox for each day. So how am I going to obtain which checkbox are checked for which day?

Do I give a unique ID to each checkbox? I think this would be too troublesome, but I have no idea how else too. Any suggestion?

The table is something like this:
http://d34wpjv4rf3nwa.cloudfront.net/www1/wp-content/uploads/2010/12/Sample-Table-of-Checkboxes.png

Thanks a lot guys!

Please show the code of the page you have now.

If only one per day is allowable, use grouped Radio buttons instead. Assign each group its own name, then you simply use Request.Form(“GroupName”) to get the value (assuming for form method=“post”).

Hi everyone,

Thanks a lot for the replies. I decided to go with a repeater control instead. I will post what I did here, so anyone else might find it useful in the future. Please do not be too hard on me should you find it a bit too long winded. I’m still very new to asp. So here goes:

–FRONT PAGE - HTML PART–

<table class=“timeslot-table” id=“tblOfficeVolunteer”>
<asp:Repeater ID=“rptOfficeVolTimeslot” runat=“server”>
<HeaderTemplate>
<tr class=“summaryTableRowTitle”>
<td align=“center”>
<label><asp:Literal ID=“litDayName” runat=“server”></asp:Literal></label>
</td>
<td align=“center”>
<label><asp:Literal ID=“litFullShift” runat=“server”></asp:Literal></label>
</td>
<td align=“center”>
<label><asp:Literal ID=“litMornShift” runat=“server”></asp:Literal></label>
</td>
<td align=“center”>
<label><asp:Literal ID=“litNoonShift” runat=“server”></asp:Literal></label>
</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr class=“clsOfficeVolTimeslot summaryTableRow”>
<td>
<asp:HiddenField ID=“hdnDayValue” runat=“server” Value=‘<%# DataBinder.Eval(Container.DataItem, “DayValue”) %>’ />
<label><asp:Literal ID=“litDayName” runat=“server” Text=‘<%# DataBinder.Eval(Container.DataItem, “Day”) %>’></asp:Literal></label>
</td>
<td align=“center”>
<asp:CheckBox ID=“chkBoxFull” runat=“server” Value=‘<%# DataBinder.Eval(Container.DataItem, “FullShift”) %>’></asp:CheckBox>
</td>
<td align=“center”>
<asp:CheckBox ID=“chkBoxMorning” runat=“server” Value=‘<%# DataBinder.Eval(Container.DataItem, “MornShift”) %>’></asp:CheckBox>
</td>
<td align=“center”>
<asp:CheckBox ID=“chkBoxNoon” runat=“server” Value=‘<%# DataBinder.Eval(Container.DataItem, “NoonShift”) %>’></asp:CheckBox>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>

–END OF FRONT PAGE - HTML PART–

– CODE BEHIND–
// here is where i load the control names
protected void Page_Init(object sender, EventArgs e)
{
rptOfficeVolTimeslot.ItemDataBound += new RepeaterItemEventHandler(rptOfficeVolTimeslot_ItemDataBound);
}

    protected void rptOfficeVolTimeslot_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Header)
        {
            LocalizeRepeater(e);
        }
    }

    private void LocalizeRepeater(RepeaterItemEventArgs e)
    {
        (e.Item.FindControl("litDayName") as Literal).Text = "Day";
        (e.Item.FindControl("litFullShift") as Literal).Text = "Full Day &lt;br/&gt; (9:00 am - 5:30 pm)";
        (e.Item.FindControl("litMornShift") as Literal).Text = "Morning Shift &lt;br/&gt; (9:00 am - 1:00 pm)";
        (e.Item.FindControl("litNoonShift") as Literal).Text = "Afternoon Shift &lt;br/&gt; (1:00 am - 5:30 pm)";
    }

    public void SetTimeslotTable()
    {

        #region EventDayDataTable
        DataTable dTblDay = new DataTable();
        dTblDay.Columns.Add("DayValue");
        dTblDay.Columns.Add("DayName");

        DataRow dr = dTblDay.NewRow();
        dr["DayValue"] = "1";
        dr["DayName"] = "Monday";
        dTblDay.Rows.Add(dr);

        DataRow dr1 = dTblDay.NewRow();
        dr1["DayValue"] = "2";
        dr1["DayName"] = "Tuesday";
        dTblDay.Rows.Add(dr1);

        DataRow dr2 = dTblDay.NewRow();
        dr2["DayValue"] = "3";
        dr2["DayName"] = "Wednesday";
        dTblDay.Rows.Add(dr2);

        DataRow dr3 = dTblDay.NewRow();
        dr3["DayValue"] = "4";
        dr3["DayName"] = "Thursday";
        dTblDay.Rows.Add(dr3);

        DataRow dr4 = dTblDay.NewRow();
        dr4["DayValue"] = "5";
        dr4["DayName"] = "Friday";
        dTblDay.Rows.Add(dr4);

        DataRow dr5 = dTblDay.NewRow();
        dr5["DayValue"] = "6";
        dr5["DayName"] = "Saturday";
        dTblDay.Rows.Add(dr5);

        DataRow dr6 = dTblDay.NewRow();
        dr6["DayValue"] = "7";
        dr6["DayName"] = "Sunday";
        dTblDay.Rows.Add(dr6);
        #endregion

        List&lt;VolFormClass.TimeSlot&gt; lstTimeslot = new List&lt;VolFormClass.TimeSlot&gt;();
        foreach (DataRow dataRow in dTblDay.Rows)
        {

            VolFormClass.TimeSlot clsTimeslot = new VolFormClass.TimeSlot();
            clsTimeslot.Day = dataRow["DayName"].ToString();
            clsTimeslot.DayValue = dataRow["DayValue"].ToString();
            clsTimeslot.FullShift = false;
            clsTimeslot.MornShift = false;
            clsTimeslot.NoonShift = false;

            lstTimeslot.Add(clsTimeslot);
        }

        rptOfficeVolTimeslot.DataSource = lstTimeslot;
        rptOfficeVolTimeslot.DataBind();                   
    }

// to get the user input values from the repeater
// construct a class ,eg here, I have the Timeslot class, containing a Timeslot list
// the list is where I will store values from the repeater

    public List&lt;TimeSlot&gt; GetDayTimeSlotList()
    {
        List&lt;TimeSlot&gt; lstTimeSlot = new List&lt;TimeSlot&gt;();

        foreach (RepeaterItem rptItem in rptOfficeVolTimeslot.Items)
        {
            TimeSlot clsTimeSlot = new TimeSlot();
            clsTimeSlot.DayValue = (rptItem.FindControl("hdnDayValue") as HiddenField).Value;
            clsTimeSlot.Day = (rptItem.FindControl("litDayName") as Literal).Text;
            clsTimeSlot.FullShift = (rptItem.FindControl("chkBoxFull") as CheckBox).Checked;
            clsTimeSlot.MornShift = (rptItem.FindControl("chkBoxMorning") as CheckBox).Checked;
            clsTimeSlot.NoonShift = (rptItem.FindControl("chkBoxNoon") as CheckBox).Checked;

            lstTimeSlot.Add(clsTimeSlot);
        }
        return lstTimeSlot;
    }

// just in case you are having difficulties in constructing the class, here goes
public class TimeSlot
{
public string Day { get; set; }
public string DayValue { get; set; }
public bool FullShift { get; set; }
public bool MornShift { get; set; }
public bool NoonShift { get; set; }

    public TimeSlot()
    {
        this.Day = string.Empty;
        this.DayValue = string.Empty;
        this.FullShift = false;
        this.MornShift = false;
        this.NoonShift = false;
    }
}

// to loop through the list containing the user input
private void btnClick_Click(object sender, EventArgs e)
{
List<TimeSlot> timeSlotResult = GetDayTimeSlotList();

        foreach (TimeSlot clsTimeSlotResult in timeSlotResult)
        {
            string strDayName = clsTimeSlotResult.Day.ToString();
            string strDayValue = clsTimeSlotResult.DayValue.ToString();
            string fullDayShift = clsTimeSlotResult.FullShift.ToString();
            string mornDayShift = clsTimeSlotResult.MornShift.ToString();
            string noonDayShift = clsTimeSlotResult.NoonShift.ToString();
        }
    }

That was really long, but I hope I helped someone. Cheers!

For your information , that code is in ASP.Net and the language used is C#.

Usually, this forum expects posts for ASP 3.0, now referred to as ‘Classic ASP’, which uses either VBScript or JScript as the language.

The forum for .Net is here : Sitepoint .Net Forum

Edit:

thread moved