2

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?

user1292656
  • 2,502
  • 20
  • 48
  • 68

2 Answers2

6

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"];

JohnnBlade
  • 4,261
  • 1
  • 21
  • 22
  • I have a TextArea with `runat="server"` in my Content Page but I want to access it from my Master Page. I have a few Labels in my Content Page which I am calling it like this in my Master Page: `lblUser = (System.Web.UI.WebControls.Label)ContentMain.FindControl("lblUser");`. How do I do the same with the TextArea? – SearchForKnowledge Sep 04 '14 at 14:52
1

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

Community
  • 1
  • 1
Yoeri
  • 2,249
  • 18
  • 33
  • I updated my answer with a reference. You should check the last answer there. (using HttpContext.Current.Request[] for example) – Yoeri Jul 23 '12 at 09:04