K
Ken Boone via .NET 247
I am trying to render the html for a page with a form on it. When I try it in an ASP.NET app, it works fine. When I use the same code in a Win App, it doesn't. Here's how to replicate what I've done.
----------------
The ASP.NET app:
----------------
1. Make a new web project.
2. Add a Button and a TextBox to default WebForm1.aspx.
3. Make TextBox.TextMode = MultiLine.
4. In Button1_Click, add this code:
System.IO.StringWriter sw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw);
System.Web.UI.Page p = new System.Web.UI.Page();
System.Web.UI.HtmlControls.HtmlForm hf = new System.Web.UI.HtmlControls.HtmlForm();
p.Controls.Add(hf);
p.RenderControl(htw);
TextBox1.Text = sw.ToString();
5. Run the project and click the button. You'll get the following output in TextBox1:
<form name="_ctl0" method="post" action="WebForm1.aspx" id="_ctl0">
<input type="hidden" name="__VIEWSTATE" value="" /></form>
----------------
The WinForm app:
----------------
1. Now make a WinForm project.
2. Add a Button and a TextBox to the default Form1.cs.
3. Rename the TextBox to TextBox1 and make TextBox1.MultiLine = True.
4. In button1_Click, add the same code as above.
5. Add a reference to System.Web.
6. Run the project and click the button. You'll get an error on the line after the RenderControl line:
An unhandled exception of type 'System.NullReferenceException' occurred in system.web.dll
Additional information: Object reference not set to an instance of an object.
Any ideas?
----------------
The ASP.NET app:
----------------
1. Make a new web project.
2. Add a Button and a TextBox to default WebForm1.aspx.
3. Make TextBox.TextMode = MultiLine.
4. In Button1_Click, add this code:
System.IO.StringWriter sw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw);
System.Web.UI.Page p = new System.Web.UI.Page();
System.Web.UI.HtmlControls.HtmlForm hf = new System.Web.UI.HtmlControls.HtmlForm();
p.Controls.Add(hf);
p.RenderControl(htw);
TextBox1.Text = sw.ToString();
5. Run the project and click the button. You'll get the following output in TextBox1:
<form name="_ctl0" method="post" action="WebForm1.aspx" id="_ctl0">
<input type="hidden" name="__VIEWSTATE" value="" /></form>
----------------
The WinForm app:
----------------
1. Now make a WinForm project.
2. Add a Button and a TextBox to the default Form1.cs.
3. Rename the TextBox to TextBox1 and make TextBox1.MultiLine = True.
4. In button1_Click, add the same code as above.
5. Add a reference to System.Web.
6. Run the project and click the button. You'll get an error on the line after the RenderControl line:
An unhandled exception of type 'System.NullReferenceException' occurred in system.web.dll
Additional information: Object reference not set to an instance of an object.
Any ideas?