繁体   English   中英

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

[英]Control 'ctl02' of type 'Button' must be placed inside a form tag with 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);
}

之后您是否将按钮添加到页面? 此外,如果您没有为按钮指定 ID,它们将自动以 ctlXXX 的形式提供一个

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

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

那应该解决它。

ASP.NET 需要由服务器管理<form>标记,以便在您的页面上使用服务器端控件。 如果您的页面在某处已经有<form>标签,您只需将runat="server"添加到该标签即可修复它。 (假设您尝试将动态创建的按钮添加到的“按钮”控件——占位符或面板或其他任何东西——本身就在<form>...</form>标签之间。)

它的工作......

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

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

这里form1 -> form name available into.aspx 文件和 objButton 是按钮 object。

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

更新

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

HtmlForm form = new HtmlForm();

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

Page.Controls.Add(form);

(未经测试)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM