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.

9
  • you have to provide an property to WebMethod like this [System.Web.Services.WebMethod(BufferResponse=false)] Commented Jul 14, 2012 at 8:23
  • @WaqarJanjua its not solving my problem i am still getting that error. Commented Jul 14, 2012 at 8: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 Commented Jul 14, 2012 at 8:29
  • simple store the text in a string, why are you assigning it to a page object ? Commented Jul 14, 2012 at 8: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". Commented Jul 14, 2012 at 8:35

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

4 Comments

are you sure, is there no way to do this? Please tell me any other way of doing this.
can you tell me why i am getting this error:"JavaScript runtime error: '$' is undefined" still i included jquery in my project <script src="~/Scripts/jquery-1.4.1.js" type="text/javascript"></script>
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.
@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.
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.

1 Comment

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.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.