How do I reference the input of an HTML <textarea> control in codebehind?

C#Htmlasp.net

C# Problem Overview


I'm using a textarea control to allow the user to input text and then place that text into the body of an e-mail. In the code behind, what is the syntax for referencing the users input? I thought I could just use message.Body = test123.Text; but this is not recognized.

HTML:

<textarea id="TextArea1" cols="20" rows="2" ></textarea>

CodeBehind:

foreach (string recipient in recipients)
{         
  var message = new System.Net.Mail.MailMessage("[email protected]", recipient);
  message.Subject = "Hello World!";         
  message.Body = test123.Text;                
  client.Send(message); 
} 

C# Solutions


Solution 1 - C#

You are not using a .NET control for your text area. Either add runat="server" to the HTML TextArea control or use a .NET control:

Try this:

<asp:TextBox id="TextArea1" TextMode="multiline" Columns="50" Rows="5" runat="server" />

Then reference it in your codebehind:

message.Body = TextArea1.Text;

Solution 2 - C#

You need to use runat="server" like this:

<textarea id="TextArea1" cols="20" rows="2" runat="server"></textarea>

You can use the runat=server attribute with any standard HTML element, and later use it from codebehind.

Solution 3 - C#

First make sure you have the runat="server" attribute in your textarea tag like this

<textarea id="TextArea1" cols="20" rows="2" runat="server"></textarea>

Then you can access the content via:

string body = TextArea1.value;

Solution 4 - C#

You should reference the textarea ID and include the runat="server" attribute to the textarea

message.Body = TextArea1.Text;  

What is test123?


Solution 5 - C#

Missed property runat="server" or in code use Request.Params["TextArea1"]

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPW2View Question on Stackoverflow
Solution 1 - C#tentonipeteView Answer on Stackoverflow
Solution 2 - C#m0saView Answer on Stackoverflow
Solution 3 - C#Ali View Answer on Stackoverflow
Solution 4 - C#MalachiView Answer on Stackoverflow
Solution 5 - C#Andrei AndrushkevichView Answer on Stackoverflow