如何从 aspx.cs 代码隐藏中的静态方法调用非静态方法
其他地方也提出了类似的问题,但答案似乎不适用于我的特定情况。
我在 aspx 页面上有一个隐藏字段:
<asp:HiddenField ID="dataReceiver" runat="server" Value="" />
我正在尝试从代码隐藏访问此字段。看来我必须从 VS2010 自动生成的默认类中引用它。由于我无法创建新类,因此我尝试了以下操作。
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}
这会在第 10 行生成错误:“对象引用未设置为对象的实例。”
我还尝试将第 14 行输入为“_Default def = new _Default().Data1();”但这被编译器拒绝,并出现错误:“无法将类型‘void’隐式转换为‘WebApplication6._Default’”
有没有办法使这项工作有效,或者我是否需要完全不同的方法?
[编辑] 达林的以下回应为我解决了这个问题,但我确实花了一些时间才弄清楚如何应用这些信息。我认为我应该澄清解决方案的细节,以防有人稍后遇到类似问题。
尽管 WebMethod 无法调用实例方法,并且它似乎无法访问页面元素(无论其“runat”属性如何),但它可以向调用它的 JavaScript 方法返回一个值。该值将在 JavaScript 中作为名为“结果”的局部变量进行访问,该变量被传递给“成功”或“失败”函数。
<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;
}
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您无法从 PageMethod 访问实例字段在 ASP.NET 中,我怀疑这就是您想要在这里实现的目标。事情就是这样,你对此无能为力。因此,我建议您简单地让页面方法将隐藏字段的值作为参数:
现在让页面方法的调用者提供所需的值,因为它位于页面的上下文中。
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:
Now let the caller of the page method supply the required value since it is in the context of the page.
目前还不清楚这里发生了什么 - 你想要实现什么目标。您收到错误的原因可能是
_Default
的无参数构造函数未将dataReceiver
设置为有用的值。Data2()
需要 是否有任何理由成为静态方法? 逻辑上应该调用Data1()
什么?您不应该因为需要在某些实例上调用Data1()
而决定只创建_Default
的新实例即可> 并以此为基础。弄清楚调用的逻辑目的,这应该表明您需要在哪个实例上调用Data1()
- 或者这实际上是否应该是一个实例方法。It's unclear what's going on here - what you're trying to achieve. You're receiving the error because presumably the parameterless constructor for
_Default
doesn't setdataReceiver
to a useful value.Is there any reason why
Data2()
needs to be a static method? What should it logically be callingData1()
on? You shouldn't just decide that because you need to callData1()
on some instance, that it's okay to just create a new instance of_Default
and call it on that. Work out the logical purpose of the call, and that should indicate which instance you need to callData1()
on - or whether this should actually be an instance method to start with.为什么要创建一个新实例:
_Default def = new _Default();
,要使用dataReceiver.Value
访问它。如果控件指定runat=server
,则意味着您可以从代码隐藏中访问该控件。为什么
Data1
是一个实例方法?我们需要更多信息。Why are you creating a new instance:
_Default def = new _Default();
, to access it usedataReceiver.Value
. If a control specifiesrunat=server
it means you can access the control from within code behind.Why is
Data1
an instance method? We need more information.