Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

html

c#

asp.net

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);  }  
like image 952
PW2 Avatar asked Dec 22 '10 10:12

PW2


People also ask

What is the input type for textarea in HTML?

The <textarea> tag defines a multi-line text input control. The <textarea> element is often used in a form, to collect user inputs like comments or reviews. A text area can hold an unlimited number of characters, and the text renders in a fixed-width font (usually Courier).

What attribute are valid for textarea input?

The <textarea> element also accepts several attributes common to form <input> s, such as autocomplete , autofocus , disabled , placeholder , readonly , and required .

Can we use value attribute in textarea?

From the MDN documentation: " <textarea> does not support the value attribute".


2 Answers

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; 
like image 93
tentonipete Avatar answered Sep 19 '22 23:09

tentonipete


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.

like image 37
m0sa Avatar answered Sep 18 '22 23:09

m0sa