1

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.

2
  • check if 'Output1' did have a value or is null which probably meant it wasn't able to find the control or the control doesn't exists. Commented Feb 29, 2012 at 8:01
  • 1
    Are you Finding the control at the right place if its within a panel or something you will need to Find it within that control (parent) Commented Feb 29, 2012 at 8:05

6 Answers 6

3

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>  
Sign up to request clarification or add additional context in comments.

5 Comments

thank you for your respond. that i have alredy done, still getting error.
The FindControl method of the page you are calling is not recursive. I.e. if your control is contained within other controls that have the runat="server" attribute set, FindControl returns null.
Yes But, it is return the error Unable to cast object of type 'System.Web.UI.HtmlControls.HtmlInputText' to type 'System.Web.UI.HtmlControls.HtmlTextArea'.
@user1230825 I've added an example.
No My Friend, I can not use asp control, I have to use only html textarea here.
1

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

htmlTexarea.InnerHtml = "sample"

Comments

0
  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;

Comments

0

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

Comments

0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.