views:

33

answers:

2

Here's what I've got:

<textarea id="TextArea1" rows="6" cols="20" runat="server"></textarea>

and in the code-behind:

partial class _Default : System.Web.UI.Page
{
    [Webmethod()]
    public static void Data(int TestNum)
    {
        if (TestNum > 0) TextArea1.InnerText = "hello world";
    }
}

And I'm getting the following error:

Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class.

As I understand it, I need to declare an instance of the class within my shared function like so:

_Default NewInstance = New _Default();
NewInstance.TextArea1.InnerText = "hello world";

My question then is, is there any way I can avoid doing this? Is this bad practice and what kind of memory or performance penalty will I incur for doing this?

Thank you.

Edit: I should mention that the Static declaration is necessary for WebMethods

+1  A: 

You need to remove the static modifier from your Data method.

Heinzi
That is indeed the simple solution but I can't do that as from what I know it's necessary to declare webmethods as static.
Radu
@Radu: You were informed wrong. WebMethods don't have to (can't) be static.
Henk Holterman
No, you don't need to declare them as static. BTW, web methods are usually not inside a web page (i.e. a class derived from `System.Web.UI.Page`) but inside a web service (i.e. a class derived from `System.Web.Services.WebService`). You might want to have a look at these pages: http://msdn.microsoft.com/en-us/library/3x5e7527.aspx
Heinzi
Just had a chance to test this and it seems that while I don't get any errors by removing the Static keyword, my webmethod no longer works.
Radu
+1  A: 

If the intent is to change the textarea on the page via an asynchronous callback, your best bet is to wrap it in an UpdatePanel or use something like JQuery to do the ajax call by hand. In the latter case you would only reference the textarea from the javascript and fetch the content asynchronously.

flatline
Yep, that's what I'm going for in the broad sense and I'm using jQuery but I have a few other attributes that I thought would be simpler to handle directly on the server-side.
Radu
I believe that since this code is responding to an ajax call, the page won't go through the regular render cycle, so it is somewhat meaningless to attempt to set a property on a control within the webmethod - you can however return a value that will get handled client-side.
flatline
@flatline, good point.
Radu