1

I have a table cell that I fill with CheckBoxes and Labels at runtime.

List<string> lstUserPool = new List<string>();
DataTable dt = GetData("SELECT UserName FROM eData ORDER BY UserName;", "Data Source = lewcomp1\\COMPLIANCE; Initial Catalog = ComplianceData; Integrated Security = True;");

for (int i = 1; i < dt.Rows.Count; i++)
{
    CheckBox cb = new CheckBox();
    cb.ID = "cb" + dt.Rows[i]["Username"].ToString();
    Label lbl = new Label();
    lbl.ID = "lbl" + dt.Rows[i]["Username"].ToString();
    lbl.Text = dt.Rows[i]["Username"].ToString();
    lbl.Font.Size = new FontUnit("18px");

    if (IsOdd(i))
    {
        cellUsersPoolLeft.Controls.Add(cb);
        cellUsersPoolLeft.Controls.Add(lbl);
        cellUsersPoolLeft.Controls.Add(new LiteralControl("<br/>"));
    }

    if (IsEven(i))
    {
        cellUsersPoolRight.Controls.Add(cb);
        cellUsersPoolRight.Controls.Add(lbl);
        cellUsersPoolRight.Controls.Add(new LiteralControl("<br/>"));
    }
}

Later, I'd like to loop through these Checkboxes to check what is Checked and what is not. I've tried various examples found on SO but with no luck. It's almost as if the checkboxes are not in the TableCell that I'v added them to. Both of the below loops do not find any checkbox controls:

foreach (Control ctl in cellUsersPoolRight.Controls)
{
    if (ctl is CheckBox)
    {

    }
}
//foreach(var checkBox in cellUsersPoolRight.Controls.OfType<CheckBox>())
//{
//    if (checkBox.Checked)
//    {
//        naz.Add(checkBox.ID);
//    }
//}

2 Answers2

0

Sorry, I cannot write in comments yet.

On which event are you trying to add the control and on which are you trying to read from it?

Take a look at this post to read more. check the order of events on page, and if you are using the correct event in the life cycle.

Ashkan S
  • 10,464
  • 6
  • 51
  • 80
  • It's a image button click event – Mitchel Stuart Fountaine Jul 15 '16 at 07:36
  • 1
    ok :) In web applications the whole process will happen on each post back, so you cannot add something to your controls and expect it to be there when next post back happens. To overcome this issue, you have to add your data to a data container and then bind your data source to a control, or add them yourself from your data source. – Ashkan S Jul 15 '16 at 08:48
0

I suggest you use a Repeater and Databind a Checkbox and Label to ItemTemplate in the repeater. This will easily allow you to get the data by looping through the repeater. If you need additional data add Hiddenfield within the repeater to store them.

 <asp:Repeater ID="UserRepeater" runat="server">
                    <HeaderTemplate>
                      <table>
                    </HeaderTemplate>
                    <ItemTemplate>
                      <tr>
                        <td>
                          <asp:CheckBox  ID="UserCheckBox" runat="server" Checked='<%# Convert.ToBoolean(Eval("IsUserChecked")) %>' ToolTip='<%# Eval("UserId") %>'
                            onmouseover="title='';" />
                        </td>
                        <td>
                          <asp:Label ID="UserNameLabel" runat="server" Text='<%# Eval("UserName") %>'></asp:Label>
                        </td>
                      </tr>
                    </ItemTemplate>
                    <FooterTemplate>
                      </table>
                    </FooterTemplate>
                  </asp:Repeater>

Then in your codebehind

//initialize repeater data
userRepeater.DataSource = dt;
userRepeater.DataBind();

Then in postback of your imagebutton_click you can get the items from the repeater

    foreach (RepeaterItem ri in UserRepeater.Items)
              {
                CheckBox userCheckBox = ri.FindControl("UserCheckBox") as CheckBox;
              }
Enkode
  • 4,515
  • 4
  • 35
  • 50
  • I quite like this approach, first time working with a Repeater. Thanks! Do you know how I could alter the ItemTemplate to allow for two columns of data instead of 1? – Mitchel Stuart Fountaine Jul 15 '16 at 07:49
  • It is very powerful and after I used it a few times it is my go to control for lists. It is perfect for lists of checkboxes. I have some great javascript I use for checking them all or unchecking them all with jquery. That is also quite useful feature. – Enkode Jul 15 '16 at 07:51
  • It already has 2 columns. Look 1 column / td is for the checkbox and 1 column /td is for the Username. If you want to add more just add the td markup and the additional data. – Enkode Jul 15 '16 at 07:53
  • What I mean is instead of having one long entry of data scrolling down my page I'd like to have the datasource split in half, in what would be 4 columns. I tried adding an additional 2 but it simply replicated the datasource – Mitchel Stuart Fountaine Jul 15 '16 at 07:56
  • 1
    That is quite a bit trickier. I normally just set the height of the div container for the repeater and allow for scrolling. You could set up 2 repeaters and 2 datasources and then basically page the datasources individually, Take first 50% then take last 50%. The best solution is to ditch the table method, make each item a div with fixed width and float them left. Give the container for the repeater a fixed height and the items will flow left and create columns to fit the space. This should be responsive. This is basically a second question. – Enkode Jul 15 '16 at 08:03
  • 1
    @MitchelStuartFountaine this is what you need. CSS Only http://stackoverflow.com/questions/14745297/how-to-display-an-unordered-list-in-two-columns – Enkode Jul 17 '16 at 23:33