[Mono-aspnet-list] HtmlInputText throws System.NullReferenceException when rendering to a HtmlTextWriter (on Mono, not when using MS .NET)

Jan van der watt janvanderwatt at gmail.com
Wed Apr 15 00:33:31 EDT 2009


Hi,
I am building HTML elements dynamically (in an ASPX file) to be added to
existing page contents via an Ajax call. I basically construct the
containers, add the children controls, and render them to a string, which is
then returned as the Ajax response.

This all works well except for the HtmlInputText control which throws
System.NullReferenceException when rendering to the HtmlTextWriter (i.e.
other types of controls like HtmlInputButton and HtmlTable work fine).

Any ideas on what goes wrong, or what I should rather be doing? It doesn't
look like this has been reported as a bug yet.

The code snippet is attached, but also copied here, in case it gets stripped
by the mail client. I added an alternate Page_Load override to prove that
the control works fine under normal circumstances, but I do need the
alternate version to work as it isn't always possible to use the working
approach (which is only used here to illustrate the problem, anyway).

[HtmlInputTextException.aspx]
<%@ Page Language="C#" %> <%@ Import Namespace="System.IO" %> <script
runat="server"> //
===========================================================================
// MAIN ENTRY // protected void Working_Page_Load(object sender, EventArgs
e) { // This code does not throw an exception HtmlInputText user_token_input
= new HtmlInputText("text"); main_content.Controls.Add(user_token_input); }
protected void Page_Load(object sender, EventArgs e) { StringWriter sw = new
StringWriter(); HtmlTextWriter hw = new HtmlTextWriter(sw); HtmlInputText
user_token_input = new HtmlInputText("text"); user_token_input.ID =
"really_unique_id"; // This code throws an exception here:
user_token_input.RenderControl(hw); // System.NullReferenceException: Object
reference not set to an instance of an object // at
System.Web.UI.HtmlControls.HtmlInputText.Render
(System.Web.UI.HtmlTextWriter writer) [0x00000] // at
System.Web.UI.Control.RenderControl (System.Web.UI.HtmlTextWriter writer)
[0x00000] // at ASP.test_htmlinputtextexception_aspx.Page_Load
(System.Object sender, System.EventArgs e) [0x00000] // at
System.Web.UI.Control.OnLoad (System.EventArgs e) [0x00000] // at
System.Web.UI.Control.LoadRecursive () [0x00000] // at
System.Web.UI.Page.ProcessLoad () [0x00000] // at
System.Web.UI.Page.ProcessPostData () [0x00000] // at
System.Web.UI.Page.InternalProcessRequest () [0x00000] // at
System.Web.UI.Page.ProcessRequest (System.Web.HttpContext context) [0x00000]
string html = sw.ToString(); Response.Write(html); // prevent the rest of
the page to be returned as well, i.e. the page only returns: // <input
name="really_unique_id" type="text" id="really_unique_id" /> Response.End();
} </script> <div id="main_content" runat="server"> </div>
Best regards

Jan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/pipermail/mono-aspnet-list/attachments/20090415/8d4feb9a/attachment.html 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: HtmlInputTextException.aspx
Type: application/octet-stream
Size: 2077 bytes
Desc: not available
Url : http://lists.ximian.com/pipermail/mono-aspnet-list/attachments/20090415/8d4feb9a/attachment.obj 


More information about the Mono-aspnet-list mailing list