Click here to Skip to main content
15,887,585 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hey All,
trying to access controls from HtmlTable but getting this error
Specified argument was out of the range of valid values.

what is the right way to fetch any control from html table with ID
will deeply appreciate your help.
Have a good one

What I have tried:

C#
HtmlTable tbl = new HtmlTable();
            HtmlTableRow tr= new HtmlTableRow();
            HtmlTableCell td1 = new HtmlTableCell();
            HtmlTableCell td2 = new HtmlTableCell();
            TextBox t1 = new TextBox();
            t1.ID = "t1_id";
            TextBox t2 = new TextBox();
            t2.ID = "t2_d";
            t1.Text = "hello";
            t2.Text = "friend";
            tbl.Border = 2;
            td1.Controls.Add(t1);
            td1.Controls.Add(t2);
            tr.Controls.Add(td1);
            tr.Controls.Add(td2);
            tbl.Controls.Add(tr);
            form1.Controls.Add(tbl);
            for (int i = 0; i <= tbl.Rows.Count - 1; i++)
            {
                for (int j = 0; j <= tbl.Rows[i].Cells.Count - 1; j++)
                {
                    TextBox tbox = (TextBox)tbl.Rows[i].Cells[j].Controls[j];   //causing error
                    Response.Write(tbox.ID);
                                    }
            }
Posted
Updated 19-Oct-20 2:12am
Comments
BillWoodruff 19-Oct-20 4:45am    
isn't this the same question you asked here, recently: https://www.codeproject.com/Questions/5282729/How-do-I-add-dynamic-controls-to-htmltable-using-A
Aftab Iqbal Clips 19-Oct-20 4:59am    
that was about adding controls to HtmlTable.but now i want to access themfrom HtmlTable
F-ES Sitecore 19-Oct-20 6:12am    
The solution is roughly the same solution I gave to your other question.
Aftab Iqbal Clips 19-Oct-20 7:50am    
that doesn't work at that time so i have to use another approach to accomplish the task.anyways leave it there.
can you please have a look at this problem.

JavaScript
for (int j = 0; j <= tbl.Rows[i].Cells.Count - 1; j++)
{
    TextBox tbox = (TextBox)tbl.Rows[i].Cells[j].Controls[j];   //causing error

Index j is not valid on Controls.
 
Share this answer
 
Comments
Aftab Iqbal Clips 19-Oct-20 4:52am    
yes, but how can i fix that
Richard MacCutchan 19-Oct-20 4:57am    
Think about what you are doing. How many controls are there in a single cell?
Aftab Iqbal Clips 19-Oct-20 5:14am    
it has 1 textbox cell in each row
i am thinking since last 4 hours, i tried this
TextBox tbox = (TextBox)tbl.Rows[i].Cells[j].Controls[0];
and this
TextBox tbox = (TextBox)tbl.Rows[i].Cells[j].Controls;
but couldn't figured it out
Richard MacCutchan 19-Oct-20 5:25am    
If it contains a single TextBox then that is not an array, so using an index value to address it makes no sense. Reread the C# documentation on scalar and array variables.
Aftab Iqbal Clips 19-Oct-20 5:40am    
yes bro it doesn't make any sense but when i use only controls without giving any index the it gives me syntax error do you have any solution?
You say there is only one textbox per cell, but in your code you add both boxes to the same cell

C#
td1.Controls.Add(t1);
td1.Controls.Add(t2);


Maybe you meant to write this instead

C#
td1.Controls.Add(t1);
td2.Controls.Add(t2);


If that is the case then to refer to the only control in a collection just use the 0 index;

C#
TextBox tbox = (TextBox)tbl.Rows[i].Cells[j].Controls[0];


Alternatively the below is a bit more of a robust way of handling the data and will cater for zero, one or more textboxes in a cell.

C#
for (int i = 0; i <= tbl.Rows.Count - 1; i++)
{
    for (int j = 0; j <= tbl.Rows[i].Cells.Count - 1; j++)
    {
        var textBoxes = tbl.Rows[i].Cells[j].Controls.OfType<TextBox>();
        foreach(TextBox tbox in textBoxes)
        {
            Response.Write(tbox.ID);
        }
    }
}
 
Share this answer
 
Comments
Aftab Iqbal Clips 19-Oct-20 9:11am    
right on target my teacher.
thank you and all other great experts on this forum.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900