0

I would prefer not to use an update panel and using the common WebMethod approach leads me to this error with this code

     private string currentHtml() {
         StringWriter str_wrt = new StringWriter();
         HtmlTextWriter html_wrt = new HtmlTextWriter(str_wrt);
         Page.RenderControl(html_wrt);
         return str_wrt.ToString();
     }

     [WebMethod]
     public static void EmailPtoRequest() {
         string test = currentHtml();
     }

Error 8 An object reference is required for the non-static field, method, or property 'PtoRequest.cs.WebForm1.currentHtml()

Clearly the method being static is causing a bunch of headaches.

Is there a standard that I can use for this type of functionality? The goal is to allow the user to send their data to the server without causing a post and refreshing the page.

Note: I DO NOT want to use a webmethod as it is causing an error which does not let me compile.

     public partial class WebForm1 : System.Web.UI.Page {
     protected void Page_Load(object sender, EventArgs e) {

     }

     private string currentHtml() {
         StringWriter str_wrt = new StringWriter();
         HtmlTextWriter html_wrt = new HtmlTextWriter(str_wrt);
         Page.RenderControl(html_wrt);
         return str_wrt.ToString();
     }

     [WebMethod]
     public static void EmailPtoRequest() {
         WebForm1 WebForm1 = new WebForm1();
         string test = WebForm1.currentHtml();
     }
 }

Results in 'test' being an empty string instead of the html of the page.

   private static string currentHtml() {
         StringWriter str_wrt = new StringWriter();
         HtmlTextWriter html_wrt = new HtmlTextWriter(str_wrt);
         Page.RenderControl(html_wrt);
         return str_wrt.ToString();
     }

     [WebMethod]
     public static void EmailPtoRequest() {
         string test = currentHtml();
     }
 }

Results in the first error again, but in the currentHtml method instead.

Please remember the question is not about the error, but an alternative to webmethod or update panels. Thank you.

joordan831
  • 720
  • 5
  • 6
codemonkeyliketab
  • 320
  • 1
  • 2
  • 17
  • You need an object reference to call non-static currentHtml() method from the static method, EmailPtoRequest(). – joordan831 Jan 11 '16 at 22:35
  • your currentHtml() method needs to be static to be able to call it from another static method – Juan Jan 11 '16 at 22:35
  • You can either call the method like this, new ClassName.currentHtml(); or you can make currentHtml() static. – joordan831 Jan 11 '16 at 22:37
  • Try this instead - http://stackoverflow.com/questions/599275/how-can-i-download-html-source-in-c-sharp – joordan831 Jan 11 '16 at 23:10

1 Answers1

1

3 options:

  1. Make the currentHtml method static,

  2. Instantiate the class that contains currentHtml like this:

    new MyClass().currentHtml();

  3. Use an ajax enabled wcf service.

Jeremy
  • 44,950
  • 68
  • 206
  • 332