user1230825
user1230825

Reputation: 27

load text into textarea from code behind in asp.net using C#

I've one asp.net page and I want to load text into the textArea control which is in aspx page from into a variable in code behind (C#):

Code behind:

System.Web.UI.HtmlControls.HtmlTextArea Output1 = 
    (System.Web.UI.HtmlControls.HtmlTextArea)(FindControl("textarea1"));       
Output1.Value = Output.ToString();

ASP:

<div style ="width: 78%; float: right; height: 85px; display: block;" 
    class="message_text_box_left">

    <textarea id="textarea1" name="textarea1" cols="30" rows="3" 
        class="message_text_box" title="Share your Idias here..." 
        tabindex="1" onkeyup="addrow_fun();"></textarea>                        
</div>  

but it is giving error like

Object reference not set to an instance of an object.

Upvotes: 1

Views: 18384

Answers (6)

Juan Andres Zafra
Juan Andres Zafra

Reputation: 1

Add runat="server" and get value with InnerText from code behind

Upvotes: 0

user1055073
user1055073

Reputation: 11

Add runat="server" in *.aspx file. Use Innertext property to set the text value. E.g.

htmlTexarea.InnerHtml = "sample"

Upvotes: 1

Uwe Keim
Uwe Keim

Reputation: 40736

You should add the

runat="server"

attribute to the text area.

Or, preferable you should use the TextBox ASP.NET control and set the TextMode property to TextBoxMode.MultiLine. Example follows:

Code behind:

Output1.Text = Output.ToString();

ASP:

<div style ="width: 78%; float: right; height: 85px; display: block;" 
    class="message_text_box_left">

    <asp:TextBox ID="Output1" Rows="3" 
        CssClass="message_text_box" ToolTip="Share your ideas here..." 
        TextMode="MultiLine" />                        
</div>  

Upvotes: 3

Asken
Asken

Reputation: 8051

If you add the runat="server" attribute you should be able to use the textarea1.innerText directly.

Upvotes: 0

user1028089
user1028089

Reputation:

Try casting to HTML generic control and set it's value or change it to use an asp textbox textmode= multiline

Upvotes: 0

Rafał Warzycha
Rafał Warzycha

Reputation: 561

  1. Add runat="server" to your control
  2. Check your .designer.cs or codebehind .cs file for textarea/textbox declaration and fix it.
  3. Do not use FindControl function (it is not recursive), get control by ID. textarea1.Value = xxx;

Upvotes: 0

Related Questions