Click here to Skip to main content
15,893,663 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

How can i fetch the name of controls inside the html table in asp.net.

This operation should be done inside in c# coding , not in Html code or javascript.


Thanks
Rajeshkumar
Posted
Comments
Sampath Kumar Sathiya 30-Sep-13 10:38am    
Hi,

Html table is server side control or client side control?
rajeshkool 30-Sep-13 10:53am    
Its server side control sampath

Hello there,

If the controls that you are trying to find inside the HTML Table are Server side controls, meaning have runat="server" attribute in them then you'll be able to access them in the CodeBehind just by their ID.

If they are not Server Controls, and you are not certain about the number of Rows in the Table and number of cells in each row, you could traverse through the Rows and Cells of Rows like this:

C#
foreach(HtmlTableRow row in SDTable1.Rows)
            {
                foreach(HtmlTableCell cell in row.Cells)
                {
                    ControlCollection cc =  cell.Controls;
                    if (cc != null && cc[0] != null)
                    { 
                        //Do something with it.
                    }
                }
            }
and if you are certain about the number of rows and cells, in short the Table content. you can do this:
C#
Control ctrl = SDTable1.Rows[0].Cells[0].Controls[0];


Hope it helps, let me know how it goes.

Good luck.

Azee...
 
Share this answer
 
Comments
rajeshkool 1-Oct-13 2:04am    
Hi Azee,

Thanks for your help. Its a nice way to fetch the controls.

How can i fetch the name of the control(for eg. Name of the label) from Control ctrl instance.

Thanks,
Rajesh
Hi,

If the control is not marked as
ASP.NET
runat="server"
you will not be able to access it on serverside.

Regards,
Greg
 
Share this answer
 
Comments
rajeshkool 30-Sep-13 10:51am    
Hi Gergo
I am using server controls only.
Gergo Bogdan 30-Sep-13 10:53am    
Then I don't understand the problem, please paste some sample generated HTML code, so I can see what are you referring to.
Thanks!
rajeshkool 30-Sep-13 11:24am    
Hi Gergo,
This is what i am looking for
protected void Page_Load(object sender, EventArgs e)
{
string a= SDTable1.Controls[1].ToString();


}
I want to take the name of the control in first row or first cell of html table
Gergo Bogdan 30-Sep-13 11:30am    
Why don't you give it a name, or will it be repeated? Please, place share some HTML code also.

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