“Button”类型的控件“ctl02”必须放在带有runat = server的表单标签内

时间:2011-06-14 15:45:49

标签: c# asp.net controls

尝试以编程方式生成多个按钮时出现错误。我没有ctl02 ..为什么我会犯这个错误?

  Button pgs = new Button();//Create New Topic
                        pgs.Width = 20;                        
                        pgs.Command += obtainTopicsPerPage_Click;
                        pgs.CommandName = tPage.ToString();
                        pgs.Text = tPage.ToString();

                        btns.Add(tPage.ToString());
                        buttons.Add(pgs);

我创建了几个按钮并循环显示列表(按钮)。然后我弄错了:(。......为什么?

完整设计:

int maximumTopicPages;
int tPage;
int questionNumber=1;
Dictionary<string, List<DisplayAllQuestionsTable>> tPages;
List<Button> buttons = new List<Button>();
protected void Answer_Click(object sender, EventArgs e)
{
    ViewState["SeekPressed"] = "pressed";
    tPages = new Dictionary<string, List<DisplayAllQuestionsTable>>();
    string subTopic = SubTopicDropDownList.SelectedItem.Value;
    List<DisplayAllQuestionsTable> threadsByTopic = new List<DisplayAllQuestionsTable>();
    List<string> btns = new List<string>();

    foreach (var topicKeys in postsByTopic)
    {

           if (topicKeys.Key == subTopic)
            {
                foreach (var item in postsByTopic[topicKeys.Key])
                {
                    questionNumber++;
                    maximumTopicPages++;
                    threadsByTopic.Add(item);//Adds All DisplayAllTables objects
                    //if there are 20 add a button.
                    if (maximumTopicPages == 20)
                    {
                        tPages.Add(tPage++.ToString(), threadsByTopic);//Add a number to the page each time, with a DisplayTable object  
                        //new Button
                        Button pgs = new Button();//Create New Topic
                        pgs.Width = 20;                        
                        pgs.Command += obtainTopicsPerPage_Click;
                        pgs.CommandName = tPage.ToString();
                        pgs.Text = tPage.ToString();

                        btns.Add(tPage.ToString());
                        buttons.Add(pgs);
                        maximumTopicPages = 0;
                        threadsByTopic.Clear();
                    }

                }//number of questions per page
                if (!tPages.ContainsKey((questionNumber / 20).ToString()))
                {
                    tPages.Add((questionNumber / 20).ToString(), threadsByTopic);//If A button is missing add it.
                }
            } 

将按钮添加到表中的方式:

    void MyButtonTable()
{

    TableRow myTableRow = new TableRow();
         HtmlForm form = new HtmlForm();

    form.Attributes.Add("runat", "server");


    Page.Controls.Add(form);

    foreach (var item in buttons)
    {
        TableCell myTableCell = new TableCell();
        form.Controls.Add(item);
        myTableCell.Controls.Add(item);
        myTableRow.Cells.Add(myTableCell);

    }

    Table2.Rows.Add(myTableRow);
    Page.Controls.Add(Table2);
}

4 个答案:

答案 0 :(得分:2)

您是否在之后将按钮添加到页面? 此外,如果您没有为按钮指定ID,它们将以ctlXXX

的形式自动给出

答案 1 :(得分:1)

.aspx文件中包含什么内容?具体来说,'按钮'控制是什么?我的猜测是,它是一个占位符或面板或类似的东西。在这种情况下,您需要将其添加到.aspx文件中:

...
<body>
<form runat="server">
...
</form>
</body>
...

那应该解决它。

ASP.NET需要拥有服务器管理的<form>标记才能在页面上使用服务器端控件。如果您的网页上某处已有<form>标记,则只需将runat="server"添加到该标记即可修复该标记。 (假设您尝试将动态创建的按钮添加到占位符或面板或其他内容的'按钮'控件本身位于<form>...</form>标记之间。)

答案 2 :(得分:0)

你必须检查“按钮”(我认为是占位符)是否位于div或带有runat =“server”的标签内

<强>更新

如果我理解你可以尝试这样的事情:

HtmlForm form = new HtmlForm();

form.Attributes.Add("runat", "server");
form.Controls.Add(buttons);

Page.Controls.Add(form);

(未测试的)

答案 3 :(得分:0)

它的工作......

请将您的新按钮控件添加到来自

 protected void btnsubmit_Click(object sender, EventArgs e)
    {
        Button objButton = new Button();
        objButton.Text = "New Button";
        objButton.ID = "randomButton";
        form1.Controls.Add(objButton);                   
    }

此处 form1 - &gt;表单名称可用于.aspx文件,objButton是按钮对象。

相关问题