Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6817469
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:03:34+00:00 2026-05-26T21:03:34+00:00

A similar question was asked elsewhere, but the answer doesn’t seem to work in

  • 0

A similar question was asked elsewhere, but the answer doesn’t seem to work in my particular situation.
I have a hidden field on an aspx page:

<asp:HiddenField ID="dataReceiver" runat="server" Value="" />

I’m trying to access this field from the codebehind. It seems like I have to reference it from within the default class that’s automatically generated by VS2010. Since I can’t create a new class I tried the following.

1public partial class _Default : System.Web.UI.Page
2{
3   protected void Page_Load(object sender, EventArgs e)
4   {
5       Data2();
6       MessageBox.Show(dataReceiver.Value);
7   }

8   public void Data1()
9   {
10      dataReceiver.Value = "123456";
11  }

12  public static void Data2()
13  {
14      _Default def = new _Default();
15      def.Data1();
16  }
17}

This generates an error at Line 10: “Object reference not set to an instance of an object.”

I’ve also tried typing Line 14 as “_Default def = new _Default().Data1();” but this is rejected by the compiler with an error: “Cannot implicitly convert type ‘void’ to ‘WebApplication6._Default'”

Is there a way to make this work, or do I need a completely different approach?

[EDIT]
Darin’s response below resolved this for me, but it did take me a little while to figure out how to apply the information. I thought I should clarify the details of the solution in case anyone reads this later with a similar problem.
Although the WebMethod can’t call an instance method, and it doesn’t seem to be able to access elements of the page regardless of their “runat” attribute, it can return a value to the JavaScript method calling it. The value will be accessible in the JavaScript as a local variable called “result” which is passed to the “success” or “failure” functions.

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<asp:ScriptManager id="scriptManager1" runat="server" EnablePageMethods= "true" />
<asp:HiddenField ID="dataReceiver" runat="server" Value="789" />
</asp:Content>
//Javascript
function callServer() {
    PageMethods.Data2($("#MainContent_dataReceiver").attr("value").toString(), success, failure);

    function success(result, userContext, methodName) { alert(result + " . " + userContext + " . " + methodName); }
    function failure(result, userContext, methodName) { alert(result + " . " + userContext + " . " + methodName); }
}
//C# Code Behind
[WebMethod]
public static string Data2(string value)
{
    value = "101112";
    return value;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T21:03:35+00:00Added an answer on May 26, 2026 at 9:03 pm

    You cannot access instance fields from a PageMethod in ASP.NET which I suspect is what you are trying to achieve here. That’s how it is and there’s not much you could do about it. So what I would recommend you is to simply have your page method take the value of the hidden field as argument:

    public static void Data2(string value)
    {
        ... do something with the value of the hidden field
    }
    

    Now let the caller of the page method supply the required value since it is in the context of the page.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have asked a similar question before, but I didn't have a firm grasp
Despite similar question was asked , I have differnet situation: My app consists mostly
A similar question was asked here , but the answers generally all seem to
Sorry I know similar question have been asked many times before but like most
Question 828421 asked similar question, but received only one real answer (update rubygems) and
A similar question has been asked: MSDN subscriptions on the cheap? , but I
I have asked similar question for Linux RPM ( Adding License Agreement in RPM
I already asked a similar question, PE Header requirements , but I'm not really
I asked a similar question about this previously, but I did not specify that
I asked a similar question yesterday, but recognize that i need to rephase it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.