在C#中调用JS相同的方法asp.net

Call the same method of JS in C# asp.net

我想在 C# 中多次调用 Javascript 方法。 我有这个代码。

for (int i = 0; i < numFa; i++)
{
    ClientScript.RegisterStartupScript(this.GetType(), 
        "addrow", "addRow('tblPets');", true);
}

该方法在 table 中添加一行,id 为 tblPets。因此,如果 numFa = 2 它只添加一行而不是两行。

根据 MSDN link ClientScript.RegisterStartupScrip

A startup script is uniquely identified by its key and its type. Scripts with the same key and type are considered duplicates. Only one script with a given type and key pair can be registered with the page.

所以您的代码只有一个问题,即在您注册相同密钥 (addrow) 的每次迭代中。

您可以通过在每次迭代中提供不同的键值来解决此问题。试试下面的代码

 for (int i = 0; i < numFa; i++)
        {
            ClientScript.RegisterStartupScript(this.GetType(),
                 string.Format("addrow{0}", numFa), "addRow('tblPets');", true);
        }

希望对您有所帮助。