Can you add child controls to a HtmlGenericControl?
I have a very simple operation where I try to add a HtmlGenericControl to another HtmlGenericControl :
protected void Page_Load(object sender, EventArgs e)
{
HtmlGenericControl div = new HtmlGenericControl("div");
HtmlGenericControl p = new HtmlGenericControl("p");
div.Controls.Add(p);//exception occurs
}
An exception is thrown in the debugger only - the page itself renders as though nothing had happened. However something definitely does happen, as in a larger portion of code, controls are not being rendered as they should.
The actual exception is:
{InnerText = '((System.Web.UI.HtmlControls.HtmlContainerControl)(div)).InnerText' threw an exception of type 'System.Web.HttpException'}
I can't understand why this is happening, as you should be able to add controls to each other in this manner.
Note that from the first answer, I tried again with IDs but it still doesn't work:
I have some more information. When using a Placeholder control as the parent, there is no exception:
protected void Page_Init()
{
PlaceHolder ph = new PlaceHolder();
HtmlGenericControl p = new HtmlGenericControl("p");
ph.Controls.Add(p);//n开发者_开发百科o problem
}
"An exception is thrown in the debugger only - the page itself renders as though nothing had happened."
That's fine in that InnerText won't work if there are child controls; that is by design because it doesn't want to render the whole hierarchy of controls at that time for various performance, lifecycle, and side affect reasons.
"... something definitely does happen, as in a larger portion of code, controls are not being rendered as they should."
What does that mean? What is rendering incorrectly in the "larger portion" of code? My guess is that is a problem somewhere else and not related to what you have in your question here.
So I think you're fine. Ignore the exception in InnerText on the parent control. You need to ask a new Question regarding whatever issue you are having with the "larger portion" of your code.
i think an easy solution to avoid this error can be to render the child HtmlGenericControl into a string and then adding it to the parents inside the InnerHtml attribute. something like this:
HtmlGenericControl parent = new HtmlGenericControl("div");
HtmlGenericControl child = new HtmlGenericControl("div");
StringWriter stringWriter = new StringWriter();
HtmlTextWriter twr = new HtmlTextWriter(stringWriter);
child.RenderControl(twr);
string rendered=stringWriter.ToString().Replace("\r","").Replace("\n","").Replace("\t","");
parent.InnerHtml = rendered;
you need to use a panel first. then you can create child HtmlGenericControl and then parent HtmlGenericControl and then you can add the parent control into the panel. for example:
<asp:Panel ID="panel" runat="server" ></asp:Panel>
//parent
HtmlGenericControl div = new HtmlGenericControl("div");
//child
HtmlGenericControl p = new HtmlGenericControl("p");
// adding child control to parent control.
div.Controls.Add(p);
div.Visible = true;
//adding parent control to panel
panel.Controls.Add(div);
Just my small assumption:
protected void Page_Load(object sender, EventArgs e) {
HtmlGenericControl div = new HtmlGenericControl("div");
div.InnerText = "";HtmlGenericControl p = new HtmlGenericControl("p");
div.Controls.Add(p);//exception occurs }
I just did a little MSDN research. Apparently you are not allowed to use more than one HtmlContainerControl.
Exception----------Condition
HttpException---There is more than one HTML server control.
Although you may create more than one HtmlContainerControl ... Still looking into this matter. Any way, no exception will be thrown when executing this piece of code
try
{
HtmlGenericControl div = new HtmlGenericControl("div");
div.ID = "div1";
HtmlGenericControl p = new HtmlGenericControl("p");
p.ID = "p1";
div.Controls.Add(p);
div.InnerText = "sep";
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
System.Diagnostics.Debug.WriteLine(ex.StackTrace);
}
Perhaps it is only a debugger message for VS.. not quite sure about this... I am going to re-edit my answer later ..
If you try to do this
string text = div.InnerText;
you'll get next exception
Cannot get inner content of id1 because the contents are not literal.
which tells that you can not see generated html in this way.
you may
create asp:Panel and it rendered as div exactly and add all controls you want inside it
or
div.InnerHtml += p.InnerHtml
this will add your render html of p to the div
I am incorrect, please see comment by Jon Adams.
I would suspect it has something to do with the controls not having IDs set yet. I could be wrong... but here's how to set IDs, if that is the problem.
HtmlGenericControl and id
精彩评论