0

I am using an ajax htmleditor in asp.net web application so i am trying to get the text the user has entered in the editor then i will send that text back to the client javascript function that will show the text in a div. But I am getting this error "Object reference not set to an instance of an object."

Firstly i tried to access the text of textbox linked with htmleditorextender through javascript but it was not working for me so i moved to ajax webmethod but this time also i am facing a problem. Please Help me.

    [System.Web.Services.WebMethod]
    public static string seteditor()
    {
        String x="";
        try
        {
            Content c = new Content();
            x = c.txteditor.Text;
        }
        catch (Exception ex) { x=ex.Message; }
        return x;
    }

Here, txteditor is the ID of asp:textbox which is linked with ajaxcontroltoolkit htmleditorextender.

  • you have to provide an property to WebMethod like this [System.Web.Services.WebMethod(BufferResponse=false)] – Waqar Janjua Jul 14 '12 at 08:23
  • @WaqarJanjua its not solving my problem i am still getting that error. –  Jul 14 '12 at 08:26
  • @nbrooks 'Content' is my asp.net code behind page's class name that's why i am creating an instance of that class and trying to access textbox that is present on my aspx page –  Jul 14 '12 at 08:29
  • simple store the text in a string, why are you assigning it to a page object ? – Waqar Janjua Jul 14 '12 at 08:30
  • @WaqarJanjua you are not getting my problem, i have textbox on my aspx page and i want to that textbox's text from a webmethod but webmethods are always static and i can not directly access element of the page through static method, that creates an error like "an object reference is required for non-static field". –  Jul 14 '12 at 08:35
  • You want to fill your text from a Web Method returned by a web service ? Then first add a reference to your web service and then create the object of that web service why you are creating object of the Page ? – Waqar Janjua Jul 14 '12 at 09:51
  • @WaqarJanjua that web method is not placed in web service its placed in my code behind page that is named Content. –  Jul 14 '12 at 10:06
  • I want to get the text of txteditor textbox in my webmethod, that textbox is placed on my Content.aspx page and my webmethod seteditor is placed in Content.aspx.cs page –  Jul 14 '12 at 10:10
  • are you getting my question? i explained my question in very detail in comments, my other codes are working fine. The returned string x of my code is shown by a javascript page method in div tag and this time this is showing the exception message returned from WebMethod. –  Jul 14 '12 at 10:30

3 Answers3

1

You cannot get your aspx controls inside a static method. If you are Calling a static method from jquery means the Page and its Controls don't even exist. You need to look another workaround for your problem.

EDIT:

I always pass my control values to page methods like this:

Assume I have two text controls: txtGroupName and txtGroupLevel

...My JS with Jquery will be :

var grpName = $("#<%=txtGroupName.ClientID%>").val();
var grpLevel = $("#<%= txtGroupLevel.ClientID %>").val();

data: "{'groupName':'" + grpName + "','groupLevel':'" +   grpLevel + "'}",

Where groupName and groupRights are my webmethod parameters.

EDIT2:

Include your script like this:

<script type="text/javascript" src="<%= ResolveUrl("~/Scripts/jquery-1.4.1.js") %>"></script>  

I suggest you to use the latest jquery version.

Suave Nti
  • 3,721
  • 11
  • 54
  • 78
  • are you sure, is there no way to do this? Please tell me any other way of doing this. –  Jul 14 '12 at 10:12
  • can you tell me why i am getting this error:"JavaScript runtime error: '$' is undefined" still i included jquery in my project `` –  Jul 14 '12 at 10:47
  • it again fired another error, but don't worry my problem is resolved i am going to add my answer. THANKS A LOT for helping me. –  Jul 14 '12 at 11:33
  • 1
    @user1306589 : I can see you went with javascript its ok but just as a suggestion spend some time practicing jquery,It is even simpler than javascript and very much useful. – Suave Nti Jul 14 '12 at 12:07
0

Web methods do not interact with the page object or the control hierarchy like this. That's why they're static in the first place. You need to pass the text from the client as a parameter to the web method, not read it from the textbox.

Jonas Høgh
  • 10,358
  • 1
  • 26
  • 46
  • yes, this is helpful, please tell me how i can get text from an asp:textbox (i.e. multiline) using javascript. Usually I use this code to get text from simple html textbox `var z = document.getElementById('txtarea').value;` but its not working in case of asp:textbox and ajax htmleditorextender can only be linked with asp:textbox. Please help. –  Jul 14 '12 at 10:17
0

This issue was torturing me from last 18 hours continuously
First I tried javascript than webmethod and than on user1042031's suggestion I tried jquery and than again I tried javascript and look how much easily it can be done with a single line of code.

var a = document.getElementById('<%= txteditor.ClientID %>').value;

read this stackoverflow article Getting Textbox value in Javascript

I apologize to everyone who responded me in this question but i have not found that article in my initial search.

Community
  • 1
  • 1