0

I have a standard HTML <table inside an ASP:Panel control. Inside this HTML table is a <TR row And inside this TR row are severale tables.

    <asp:panel ID="PanelPreStart" runat="server" Visible="false" Enabled="false" Width="500px" >
        <table runat="server" id="tblPreStart">
    ...
    ...

      <tr id="trRiskMgt" runat="server" visible="false">
            <td colspan="2">
                <strong><big>** RISK MANAGEMENT **</big></strong>
                    <br /><br />
                <table id="tblHOC_MON" runat="server" visible="false">
..
..
                </table>

                <br />

                <table id="tblRMP_TUE" runat="server" visible="false" style="width: 100%; height: 114px; color: black; background-color: white;">
                    <tr>
...
... etc

How do I iterate through the trRiskMgt row to get to all the tables (tblHOC_MON, etc)?

I've tried this but it does not work. I get this error:

CS1579: foreach statement cannot operate on variables of type 'System.Web.UI.HtmlControls.HtmlTableRow' because 'System.Web.UI.HtmlControls.HtmlTableRow' does not contain a public definition for 'GetEnumerator'

    foreach (HtmlTableRow trow in trRiskMgt)
    {
        foreach (HtmlTable tbl in trow.Cells)
        {
            foreach (HtmlTableRow row in tbl.Controls)
            {
                foreach (HtmlTableCell cell in row.Cells)
                {
                    foreach (Control ctrl in cell.Controls)
                    {
                        //CONTROL IS TEXBOXT: DISABLE CONTROL (NOT HIDE!)
                        if (ctrl is TextBox)
                        {
                            TextBox txt = (TextBox)ctrl;
                            txt.Enabled = wsDisable;
                        }
                    }
                }
            }
        }
    }

Ideas for both JavaScript and C# code behind appreciated.

Thank you

[UPDATE] I've managed to get around it by hardcoding one of the above tables into the code...

    foreach (HtmlTableRow row in tblHOC_MON.Rows)
    {
        foreach (HtmlTableCell cell in row.Cells)
        {
            foreach (Control ctrl in cell.Controls)
            {
                //CONTROL IS TEXBOXT: EXTRACT VALUES//
                if (ctrl is TextBox)
                {
                    TextBox txt = (TextBox)ctrl;
                    txt.Enabled = false;
                }
            }
        }
    }

This is obviously not ideal, as I will need to repeat this code multiple times for every table in the TR row.

But it will do for now.

6
  • For this to work, you have to specify runat="server" on all the tags you are trying to access from C#. Commented Oct 13, 2015 at 4:23
  • Look more closely. They already do have that. Commented Oct 13, 2015 at 4:25
  • No, you look more closely! <td colspan="2"> see? Commented Oct 13, 2015 at 4:27
  • Also, when you say "but it does not work" - be more specific. Commented Oct 13, 2015 at 4:42
  • 1
    But the TR and the TABLE tags do have it. Are you saying everything inside the row has to have a server reference? Commented Oct 13, 2015 at 4:43

1 Answer 1

1

Try inner html concept.

Example: to iterate particular html element say "table row" in your case, write below code on your controller:

trRiskMgt.InnerHtml = Server.HtmlEncode("your data");// elementId.InnerHtml

If you haven't specified any data it will show the single row in this case. AS the data will increase it will iterate the row.

You can further google HtmlEncode or Decode for better understanding.

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

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.