简体   繁体   中英

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?

  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

What is in the.aspx file? 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:

...
<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. 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. (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.)

Its working....

Please add your new button control into 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.

You have to check if "buttons" (I think is a placeholder) is inside a div or a tag with 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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