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

问题描述

| 尝试以编程方式生成许多按钮时出现该错误。我没有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);
}
    

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)