0

我正在尝试以这种方式自定义 ASP.NET 面板的呈现...

 [DefaultProperty("ID")]
    [ToolboxData("<{0}:NFormPanel runat=server></{0}:NFormPanel>")]
    [Description("Aldammam panel.")]
    [ParseChildren(false)]
    [PersistChildren(true)]
    public class NFormPanel : Panel, INamingContainer
    {


        protected override void RenderContents(HtmlTextWriter output)
        {
            var sb = new StringBuilder();
            sb.AppendLine("<table class=\"con-table\" style=\"width: 100%;\">");
            output.Write(sb.ToString());
            if (HasControls())
            {

                this.RenderChildren(output);
            }
            sb.Clear();
            sb.AppendLine("</table>");
            output.Write(sb.ToString());

        }


    }

...但是输出首先呈现面板控件,然后呈现表格,就像这样......

 <div>
   <input name="TextBox1" type="text" id="TextBox1">
   <table class="con-table" style="width: 100%;">
   </table>
 </div>

我想要表格内的子控件,如下例所示...

<div>
  <table class="con-table" style="width: 100%;">
    <input name="TextBox1" type="text" id="TextBox1">
  </table>
</div>
4

1 回答 1

0

改用 render 方法:

protected override void Render(HtmlTextWriter output)
{
     var sb = new StringBuilder();
     sb.AppendLine("<table class=\"con-table\" style=\"width: 100%;\">");
     output.Write(sb.ToString());

     if (HasControls())
     {
         //Alternatively call base.Render() which renders the panel's DIV
         this.RenderChildren(output); //renders inner children only without panel DIV
     }

     sb.Clear();
     sb.AppendLine("</table>");
     output.Write(sb.ToString());

}
于 2013-03-11T12:57:44.177 回答