[英]Control 'ctl07' of type 'TextBox' must be placed inside a form tag with 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.