简体   繁体   中英

Get textarea in codebehind

I am trying to get a textarea value from code behind using the following code.

  HtmlTextArea bodytextarea = new HtmlTextArea();
    bodytextarea = (HtmlTextArea)(this.FindControl("codearea"));
    string txtbod = bodytextarea.Value;

When i debug it i get a null reference exception saying that bodytextarea is null. I have to mention that my textarea is not runat="server" and i do not want to make it on server side. Any help?

You should add runat="server" to your <TextArea id="myTextArea" runat="server" />

like this you can directly get the value in code behind just by using the ID of the textarea


And if you dont wanna use server side then you have to use Jquery to get the value and create a [webmethod] method in your code behind so Jquery can call that method passing the value


or simply string data = request["codearea"];

If it's not a runat="server" control, then you have to get the value from the http context. The value from the textarea will be treated as a (most likely) POST or GET variable.

see also: Get POST data in C#/ASP.NET

and Getting a POST variable

official MSDN: http://msdn.microsoft.com/en-us/library/system.web.httpcontext.aspx

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