1

I have something like this:

<table id="tableId" runat="server">
<thead>
    <tr>
        <th>Example thead1</th>
        <th>Example thead2</th>
   </tr>
</thead>
<tbody id="tb0">
    <tr>
        <td>Example 1</td>
        <td>Something</td>
   </tr>
</tbody>
<tbody id="tb1">
    <tr>
         <td>Example 2</td>
         <td>Something</td></tr>
</tbody>
<tbody id="tb2">
    <tr>
        <td>Example 3</td>
        <td>Something</td>
   </tr>
</tbody>
</table>

I use multiple "tbody" with different IDs so I can delete it or create it anytime I want.

What I'd like to do is getting each row from multiple "tbody" from the table "tableId".

In C#, if I use the command "tableId.Rows[0].Cells[0].InnerHtml", I get the result: "Example thead1".

But if I use "tableId.Rows[3].Cells[0].InnerHtml", I can't get the "Example 3" as available in table row, instead of it I get an error which says that row doesn't exist or it's out of index.

3 Answers 3

0

I have tested the your code. and found that tableId.Rows[3].Cells[0].InnerHtml will give always Example 3 ..that's is correct as per your aspx markup code.

i think you have place row index incorrect i.e. something like below

tableId.Rows[4].Cells[0].InnerHtml which not exist in your table structure.

that's why you get error of Specified argument was out of the range of valid values.Parameter name: index

Hope this will helps you...happy coding...

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

1 Comment

I'm still having the same problem, even if I try the tableId.Rows[1].Cells[0].InnerHtml. Only the 1st row is working tableId.Rows[0].Cells[0].InnerHtml..
0

Make the tbody elements server controls.

<tbody id="tb2" runat="server">
    <tr>
        <td>Example 3</td>
        <td>Something</td>
   </tr>
</tbody>

Then toggle the visibility as you need:

tb2.Visible = true / false;

As per comment:

Probably the designer doesn't create a reference to tb1, tb2, tb3 controls because they are inner controls of tableId. If so:

var tb2 = tableId.FindControl("tb2");
tb2.Visible = ...

3 Comments

that didn't work, because when the page loads the tbodys "tb0, tb1, tb2, etc" are not created yet, so I get instantly this error: "CS0103: The name 'tb0' does not exist in the current context"
when I tried what you said I was trying to save tableId.Rows[1].Cells[0].InnerHtml to a string, and you were right, using the "var tb2 = tableId.FindControl("tb2"); tb2.Visible = ..." made my page load error gone, but I still can't save the tableId.Rows[1].Cells[0].InnerHtml to a string, and I still get this error: Specified argument was out of the range of valid values.Parameter name: index
But I also tried to access the only row I can access, which is the first one: tableId.Rows[0].Cells[0].InnerHtml. This time I couldn't save the Example thead1 to a string and I got this error about this line of code "tb0.Visible = true;": Object reference not set to an instance of an object.
-1

You could use JQuery, something like this....

$('tbody', '#tableid');

But I guess depends on what you want to do with them, and where

JQuery docs:

Selectors: http://api.jquery.com/category/selectors/

Traversing: http://api.jquery.com/category/traversing/

2 Comments

I didn't understand it.. I'm trying to access the rows by using C#, so I can save it later to a MSSQL DB
JQuery is client side so if you want results on the server you will need to use a Json call (or something similar). Here is a post which covers the basics. stackoverflow.com/questions/3926098/… I was just suggesting another way to ‘crack your nut’ ;0)

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.