简体   繁体   中英

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.

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>  

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

htmlTexarea.InnerHtml = "sample"

添加runat =“server”并从后面的代码中获取InnerText的值

  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;

尝试转换为HTML通用控件并设置其值或将其更改为使用asp文本框textmode = multiline

如果添加runat =“server”属性,则应该能够直接使用textarea1.innerText。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM