0

I have the following C# code:

 AddCommentForm = string.Format("<form name=\"AddComment\" method=\"post\"  runat=\"server\" id=\"add_comment_form\"><p> TITLE:  <input type =\"text\" name=\"Title\" /></p><p> Contnt <textarea name=\"Content\" ></textarea></p><p> <button type=\"submit\">Submit!</button></p></form>");
                this.Form.Action = "ViewArticle.aspx?ArticleID=" + ArticleID;

The problem is that there is an error in the second line:

System.NullReferenceException was caught

My question is how can I this error?

And why with this code it works?

 <%
     this.Form.Action = "ViewArticle.aspx?ArticleID=" + ArticleID.ToString();
 %>
 <form name="AddComment"  method="post" runat="server">
John Saunders
  • 157,405
  • 24
  • 229
  • 388
Nave Tseva
  • 808
  • 7
  • 22
  • 46
  • Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Mar 20 '13 at 23:40

1 Answers1

0
AddCommentForm = string.Format("<form name=\"AddComment\" method=\"post\" ....

Above code is not valid. AddCommentForm is a HtmlForm control - not a string. Besides, you cannot create another form tag inside a form in ASP.Net.

this.Form.Action = "ViewArticle.aspx?ArticleID=" + ArticleID.ToString();

Basically, you are Cross-Site Scripting. Although it compiles, it won't work. When you get to ViewArticle.aspx page, you'll get an error.

If you want a form inside ASP.Net, use iframe.

Win
  • 56,078
  • 13
  • 92
  • 167