调用Web服务时会出现异常,该服务在运行时动态构建HTML元素,并将呈现的元素作为字符串返回。
RenderControl()方法调用发生以下错误:
System.Web.HttpException:无法找到带有id' txtCertificate'的控件与Label' lblCertificate'。
相关联StringWriter stringWriter = new StringWriter();
HtmlTextWriter writer = new HtmlTextWriter(stringWriter);
// Create System.Web.UI.WebControls.Panel (container)
Panel pnlFRow1 = new Panel();
pnlFRow1.CssClass = "f-row";
// Create System.Web.UI.WebControls.Textbox
TextBox txtCertificate = new TextBox();
txtCertificate.ID = "txtCertificate";
txtCertificate.CssClass = "f-input f-input-full";
// Create System.Web.UI.WebControls.Label
Label lblCertificate = new Label();
lblCertificate.ID = "lblCertificate";
lblCertificate.CssClass = "f-label f-label-full";
lblCertificate.AssociatedControlID = txtCertificate.ID;
lblCertificate.Text = "Certificate:";
Panel pnlCertificate = new Panel();
pnlCertificate.CssClass = "f-label f-label-full";
// Binding child controls to parent controls
pnlFRow1.Controls.Add(lblCertificate);
pnlFRow1.Controls.Add(pnlCertificate);
pnlCertificate.Controls.Add(txtCertificate);
// Render control
pnlContent.RenderControl(writer);
// Return rendered HTML
return writer.InnerWriter.ToString();
我尝试在pnlFRow1.Controls.Add(lblCertificate);
行之后放置行pnlCertificate.Controls.Add(txtCertificate);
,认为这可能是订单重要的问题,但这会导致同样的错误。
AssociatedControlID
属性是必须的,以便Label
WebControl呈现为实际的<label>
元素,并且必须在输入控件之前显示。