【问题标题】:Name 'of table' does not exist in the current context当前上下文中不存在名称“表”
【发布时间】:2018-11-13 23:25:28
【问题描述】:

我有这个带有行的表,现在我正在处理一个动态添加行的按钮。我认为我的代码是正确的,但我的 Visual Studio 给出了一个错误,提示“当前上下文中不存在名称 'kost_tbl'。”

这是我的前端表:

<table id="kost_tbl" runat="server" border="1" width="100%">
                <tr>
                    <td width="12%" height="30px">1</td>
                    <td width="22%">2</td>
                    <td width="22%">3</td>
                    <td width="22%">4</td>
                    <td>Totaal</td>
                </tr>
                <tr>
                    <td height="25px" style="background-color:#ddd;">1.1 </td>
                    <td>1.2</td>
                    <td>1.3</td>
                    <td>1.4</td>
                    <td>1.5</td>
                </tr>

广告一行的函数是这样写的:

protected void btnAddCost_Click(object sender, EventArgs e)
{

int totalRows = 1;
int totalColumns = 5;

for (int r = 0; r <= totalRows; r++)
{
    System.Web.UI.HtmlControls.HtmlTableRow tableRow = new HtmlTableRow();
    for (int c = 0; c <= totalColumns; c++)
    {
        System.Web.UI.HtmlControls.HtmlTableCell cell = new HtmlTableCell();
        TextBox txtBox = new TextBox();
        txtBox.Text = "Row: " + r + ", Col:" + c;
        cell.Controls.Add(txtBox);
        //Add the cell to the current row.
        tableRow.Cells.Add(cell);

        if (c == 5)
        {
            kost_tbl.Rows.Add(tableRow);
        }
    }
}
}

错误是关于最后一行代码的,这就是他失败的地方,并说它不存在,尽管它具有相同的 id 和 runat="server"。

关于可能是什么问题的任何想法? (我在 ASP.net 中使用 C#)

【问题讨论】:

  • 那个函数在哪里?
  • 糟糕,抱歉没有复制整个内容。现在已经编辑好了。

标签: c# asp.net html-table row


【解决方案1】:

改变这一行:

kost_tbl.Rows.Add(tableRow);

收件人:

Table kost_tbl = Page.FindControl("kost_tbl") as Table; 
kost_tbl.Rows.Add(tableRow);

【讨论】:

    猜你喜欢
    • 2013-10-02
    • 2014-04-22
    • 2017-06-17
    • 2015-09-11
    • 1970-01-01
    • 1970-01-01
    • 2022-12-18
    • 2020-11-02
    相关资源
    最近更新 更多