我在动态创建页面方面遇到了一些问题:
p = New Page();
Page myPage = new Page();
FormAtt ctrl = (FormAtt)myPage.LoadControl("path/to/my/file.ascx"); // here lies the gridview of evil
myPage.Controls.Add(ctrl);
问题是我收到了
Control ... must be placed inside a form tag with runat=server
好的,所以我发现我需要覆盖VerifyRenderingInServerForm
方法才能调用无格式页面,但是如何覆盖VerifyRenderingInServerForm
,因为我没有ASPX文件。
答案 0 :(得分:3)
您可以尝试使用已覆盖VerifyRenderingInServerForm
的自定义类:
public partial class MyCustomPage : System.Web.UI.Page
{
public override void VerifyRenderingInServerForm(Control control)
{
}
protected void Page_Load(object sender, EventArgs e)
{
var p = new MyCustomPage();
FormAtt uc = (FormAtt)p.LoadControl("path/to/my/file.ascx");
p.Controls.Add(uc);
}
}
答案 1 :(得分:1)
与您之前的问题类似。您需要先添加 HtmlForm 。 ASP.Net需要一个表单标签才能向页面添加控件。
Page myPage = new Page();
HtmlForm form = new HtmlForm();
FormAtt ctrl = (FormAtt)myPage.LoadControl("path/to/my/file.ascx");
form.Controls.Add(ctrl);
myPage.Controls.Add(form);
答案 2 :(得分:1)
我这样做是为了渲染组件
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
Page p = new Page();
System.Web.UI.HtmlControls.HtmlForm form = new System.Web.UI.HtmlControls.HtmlForm();
p.Controls.Add(form);
form.Controls.Add(gv);
form.RenderControl(htmlWrite);
string j = stringWrite.ToString();