简体   繁体   English

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

[英]Control 'ctl02' of type 'Button' must be placed inside a form tag with runat=server

I get that error when trying to generate a number of buttons programmatically.尝试以编程方式生成多个按钮时出现该错误。 I have no ctl02.. Why do i get that mistake?我没有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);

I create a few buttons and loop through the list (buttons).我创建了几个按钮并遍历列表(按钮)。 Then i get that mistake:(. ... why?然后我得到了那个错误:(。......为什么?

Full design:完整设计:

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.
                }
            } 

Way the buttons are added to the table:按钮添加到表格的方式:

    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);
}

Are you adding your buttons to the Page afterwards?之后您是否将按钮添加到页面? Also, if you do not specify an ID to your buttons, they will be given one automatically in the form of ctlXXX此外,如果您没有为按钮指定 ID,它们将自动以 ctlXXX 的形式提供一个

What is in the.aspx file? .aspx 文件中有什么? Specifically, what is the 'buttons' control?具体来说,什么是“按钮”控件? My guess is, it is a placeholder or panel or something similar.我的猜测是,它是一个占位符或面板或类似的东西。 In that case, you need to add this to your.aspx file:在这种情况下,您需要将其添加到 your.aspx 文件中:

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

That should fix it.那应该解决它。

ASP.NET needs to have the <form> tag managed by the server in order to use server side controls on your page. ASP.NET 需要由服务器管理<form>标记,以便在您的页面上使用服务器端控件。 If your page already has a <form> tag on it somewhere, you can just add runat="server" to that tag and it will fix it.如果您的页面在某处已经有<form>标签,您只需将runat="server"添加到该标签即可修复它。 (That assumes the 'buttons' control that you're trying to add the dynamically created button into -- the placeholder or panel or whatever -- is itself between the <form>...</form> tags.) (假设您尝试将动态创建的按钮添加到的“按钮”控件——占位符或面板或其他任何东西——本身就在<form>...</form>标签之间。)

Its working....它的工作......

Please add your new button control into from请将您的新按钮控件添加到from

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

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

You have to check if "buttons" (I think is a placeholder) is inside a div or a tag with runat="server"您必须检查“按钮”(我认为是占位符)是否在 div 或带有 runat="server" 的标签内

update更新

If I understand you can try something like this:如果我理解你可以尝试这样的事情:

HtmlForm form = new HtmlForm();

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

Page.Controls.Add(form);

(untested) (未经测试)

暂无
暂无

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

相关问题 类型为&#39;TextBox&#39;的控件&#39;ctl07&#39;必须放置在带有runat = server的表单标记中。 - Control 'ctl07' of type 'TextBox' must be placed inside a form tag with runat=server. 类型为&#39;TextBox&#39;的控件&#39;ctl00_txtDebug&#39;必须放置在带有runat = server的表单标记中 - Control 'ctl00_txtDebug' of type 'TextBox' must be placed inside a form tag with runat=server 类型为“ RadioButton”的控件“ ctl00”必须置于带有runat = server的表单标记中 - Control 'ctl00' of type 'RadioButton' must be placed inside a form tag with runat=server 类型为“ DataControlLinkBut​​ton”的控件“ DataLoading1_gdvDataGroups_ctl01_ctl00”必须置于带有runat = server的表单标记中 - Control 'DataLoading1_gdvDataGroups_ctl01_ctl00' of type 'DataControlLinkButton' must be placed inside a form tag with runat=server 类型为“ HtmlEditorExtender”的控件必须放在带有runat = server的表单标签中 - Control of type 'HtmlEditorExtender' must be placed inside a form tag with runat=server &#39;Button&#39; 类型的控件 &#39;ContentPlaceHolder1_Button1&#39; 必须放置在带有 runat=server 的表单标签内 - Control 'ContentPlaceHolder1_Button1' of type 'Button' must be placed inside a form tag with runat=server 为&lt;%= @ Html.DropDownList%&gt;获取异常{“类型为“按钮”的控件&#39;按钮1”必须放置在带有runat = server的表单标记中。”} - Getting exception for <%=@Html.DropDownList%> {“Control 'Button1' of type 'Button' must be placed inside a form tag with runat=server.”} 必须放置在带有 runat=server 的表单标签内 - Must be Placed Inside a Form Tag With runat=server 类型为&#39;TextBox&#39;的控件&#39;MainContent_forenameTxt&#39;必须放置在带有runat = server的表单标签中 - Control 'MainContent_forenameTxt' of type 'TextBox' must be placed inside a form tag with runat=server 必须将带有'LinkBut​​ton'类型的控件xxx放在带有runat = server的表单标签内 - Control xxx of type 'LinkButton' must be placed inside a form tag with runat=server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM