protected void Page_Load(object sender, EventArgs e)
{
Button b = new Button();
b.Text = "1";
this.Controls.Add(b);
}
子页面如下所示:
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder2" Runat="Server"/>
母版
<div align="center" runat="server" >
<form id="form1" runat="server">
<asp:ContentPlaceHolder id="ContentPlaceHolder2" runat="server">
</asp:ContentPlaceHolder>
</form>
</div>
如此处所示,按钮控件位于
内为什么我会这样做:
http://localhost:2427/WebSite10/AllQuestions.aspx
Control 'ctl01' of type 'Button' must be placed inside a form tag with runat=server.
描述:执行当前Web请求期间发生了未处理的异常。请查看堆栈跟踪,以获取有关错误及其在代码中的起源位置的更多信息。
异常详细信息:System.Web.HttpException:类型为“Button”的控件“ctl01”必须放在带有runat = server的表单标记内。
答案 0 :(得分:0)
这些行上的某些内容应该有效,这有助于渲染控件
Button b = new Button();
b.Text = "Click Me";
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
b.RenderControl(hw);
Response.Write(hw.InnerWriter);
或者您也可以通过PlaceHolder
<asp:PlaceHolder ID="holder1" runat="server"></asp:PlaceHolder>
Button b = new Button();
b.Text = "Click Me";
holder1.Controls.Add(b);
答案 1 :(得分:-1)
尝试将其添加到内容占位符而不是页面本身