在c#函数中获取HTML表ID

本文关键字:HTML ID 获取 函数 | 更新日期: 2023-09-27 18:09:49

在HTML页面中,我的表ID =" week1",在c#函数中,我使用它作为

week1.Rows.Add(TableCell);

我想在表ID中强制转换字符串。假设字符串

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.
   */
}

如何在HTML表ID转换以上字符串?????

在c#函数中获取HTML表ID

如果您的表位于面板中,您可以像这样查找它们。请注意,ofc您将需要runat=server。我假设你使用HtmlTable在你的表单()

for (int i = 0; i < 5; i++)
        {
            var table = (HtmlTable)pnlTables.FindControl("week" + i);
            if (table != null)
            {
                //do stuff with your table
            }
        }

确保.aspx中的表具有runat="server" (<table id="week1" runat="server">),然后,在后面的代码中,您可以简单地执行

week1.ID

week1.ClientID(用于DOM中的完整ID) -您想要的任何一个

您的表应该有runat="server"属性。只有这样你才能从后面的代码访问它。