Get textarea in codebehind

梦想与她 提交于 2019-12-24 02:14:12

问题


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?


回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/11609330/get-textarea-in-codebehind

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!