form RenderControl doesn't work in win app

  • Thread starter Ken Boone via .NET 247
  • Start date
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?
 
N

Norman Yuan

Web app and Win app are different things, you cannot simply use the same
code run in both apps without knowing how they run so much differently.

Ken Boone via .NET 247 said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top