簡體   English   中英

使用C#從asp.net中的代碼加載文本到textarea

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

我有一個asp.net頁面,我想將文本加載到aspx頁面中的textArea控件,進入后面代碼中的變量(C#):

代碼背后:

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>  

但它給出了錯誤

你調用的對象是空的。

你應該添加

runat="server"

屬性到文本區域。

或者,您最好使用TextBox ASP.NET控件並將TextMode屬性TextModeTextBoxMode.MultiLine 示例如下:

代碼背后:

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>  

*.aspx文件中添加runat="server" 使用Innertext屬性設置文本值。 例如

htmlTexarea.InnerHtml = "sample"

添加runat =“server”並從后面的代碼中獲取InnerText的值

  1. runat="server"添加到您的控件
  2. 檢查.designer.cs或codebehind .cs文件以獲取textarea / textbox聲明並修復它。
  3. 不要使用FindControl函數(它不是遞歸的),通過ID獲取控制權。 textarea1.Value = xxx;

嘗試轉換為HTML通用控件並設置其值或將其更改為使用asp文本框textmode = multiline

如果添加runat =“server”屬性,則應該能夠直接使用textarea1.innerText。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM