1

I have an html table in an aspx page (C#) that has columns like

 1.CheckBox  2.Text  3.Text  4.TextBox

I want to iterate through the table one row at a time and process (run a stored procedure based on column2) based on whether the checkbox is checked or not. How will I be able to do that?

4 Answers 4

6

Assuming you're using the Table server control then it's just:

foreach (TableRow row in table.Rows)
    {
        var checkBox = (CheckBox)row.Cells[0].Controls[0]; //Assuming the first control of the first cell is always a CheckBox.

        if (checkBox.Checked)
        {
            var col2 = (TextBox)row.Cells[1].Controls[0];

            /* Do Stuff With col2 */
        }
        else
        {
            /* Do Stuff */
        }
    }

If you're using just a regular html table (with runat="server") as well as html form controls then just change TableRow to HtmlTableRow, CheckBox to HtmlInputCheckBox, and TextBox to HtmlInputText. All of those controls are in the System.Web.UI.HtmlControls namespace.

Sign up to request clarification or add additional context in comments.

2 Comments

Could you modify this to check if a checkbox exists in the first column of the table?
Yes. var checkBox = row.Cells[0].Controls[0] as CheckBox; Then on the next line: if (checkBox != null && checkBox.Checked) {}
1

I already had some VB.NET code handy that can do this. It just took a little tweaking. It could be ported over to C# easily.

Protected Sub Page_Load()
    FindCheckBoxes(MyTable)
End Sub

Protected Sub FindCheckBoxes(ByRef ParentControl As Control)
    For Each ctrl As Control In ParentControl.Controls
        If TypeOf ctrl Is CheckBox Then
            If DirectCast(ctrl, CheckBox).Checked Then
                ' do something
            Else
                ' do something else
            End If
        ElseIf ctrl.HasControls Then
            FindCheckBoxes(ctrl)
        End If
    Next
End Sub

This is flexible enough to find checkboxes inside of anything (not just a table). However, in your particular scenario you may prefer to use something like noblethrasher's answer.

My answer is a recursive method of crawling through the tree, finding every single checkbox. But noblethrasher's is a simple, straightforward, and more efficient algorithm if you know which column to look for the checkbox and you know it's not buried inside additional containers.

Comments

0

Check out the HtmlTable class and its Rows property.

http://msdn.microsoft.com/en-us/library/system.web.ui.htmlcontrols.htmltable(VS.71).aspx

I'm sure that can help you on the way!

Comments

0

If you add a runat="server" attribute to the table tag in html, you should be able to see the table as a collection of Control objects and you should be able to iterate through.

This seems like the worst possible way to achieve dynamic data, however. Perhaps with some background we could help you find a better way...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.