Malik
Malik

Reputation: 35

Get HTML Table ID in C# function?

In HTML page I've table with ID = "week1" and in C# function I'm using this as

week1.Rows.Add(TableCell);

I want to cast string in table ID. Suppose I've string

for(int i=0; i<5; i++)
{
  String abc = "week" + i;
   /*
     How to do cast this string in table ID
     like above to add rows and cell in table but above is hardcoded and I want
     to make this dynamic.
   */
}

How to cast above string in HTML Table ID ?????

Upvotes: 0

Views: 4482

Answers (3)

Saranya
Saranya

Reputation: 38

Your table should have runat="server" attribute.Only then u can access it from code behind.

Upvotes: 0

Yeronimo
Yeronimo

Reputation: 1749

If your tables reside in a panel you can look them up like this. Please note that ofc you will need runat=server for them. I assume you use HtmlTable in your form ()

for (int i = 0; i < 5; i++)
        {
            var table = (HtmlTable)pnlTables.FindControl("week" + i);

            if (table != null)
            {
                //do stuff with your table
            }
        }

Upvotes: 2

Chris Dixon
Chris Dixon

Reputation: 9167

Make sure your table in your .aspx has runat="server" (<table id="week1" runat="server">), then, in your code behind, you can simply do

week1.ID

or week1.ClientID (for the full ID in your DOM) - whichever one you're wanting.

Upvotes: 1

Related Questions