简体   繁体   English

如何用静态方法调用非静态方法(使用WebControl)?

[英]How do I call a non-static method (that uses a WebControl) with a static method?

I have the following snippet of code. 我有以下代码片段。 I can only access the PbcTreeView in a non-static method, so I call it during Page_Load. 我只能以非静态方法访问PbcTreeView ,因此在Page_Load期间将其调用。 However, the setTreeView() method can't access the object b not can it call the buildTreeView() method. 但是,setTreeView()方法无法访问对象b不能调用buildTreeView()方法。 (setTreeView() has to be static according to the WebMethod rules and buildTreeView can't be static because it uses the TreeView). (根据WebMethod规则,setTreeView()必须为静态,而buildTreeView不能为静态,因为它使用TreeView)。 How could I get it to do something similar to b.buildTreeView() that I have in the method? 如何获得与方法中具有的b.buildTreeView()类似的功能?

Thanks 谢谢

protected void Page_Load(object sender, EventArgs e)
    {
        BuildPbcTree b = new BuildPbcTree(PbcTreeView);
    }

    [WebMethod]
    public static void setTreeView()
    {
        b.buildTreeView(); //how do I do this??
    }

    public class BuildTree
    {
        TreeView Tree;
        public BuildTree(TreeView t)
        {
            Tree = t;
        }
        public void buildTreeView() {...//implementation}
    }

You can pass the instance of the class to the Static method. 您可以将类的实例传递给Static方法。 For example, in your Page_Load: 例如,在您的Page_Load中:

SetTreeView(b);

This assumes you change your web method to accept a BuildTree class. 假定您将Web方法更改为接受BuildTree类。

I assume that PbcTreeView is some control on your page. 我认为PbcTreeView是您页面上的某些控件。

In that case, you are out of luck. 在这种情况下,您很不走运。 You cannot modify server-side controls during AJAX calls (web methods). 您不能在AJAX调用(Web方法)期间修改服务器端控件。 AJAX calls are designed to be used as follows: AJAX调用的设计用途如下:

  1. You call your web method in JavaScript code. 您可以使用JavaScript代码调用网络方法。
  2. The web method performs some database lookup or computation and returns the result. Web方法执行一些数据库查找或计算并返回结果。
  3. Your JavaScript code updates the user interface. 您的JavaScript代码将更新用户界面。

You are trying to update the user interface inside a web method. 您正在尝试更新Web方法的用户界面。 That won't work. 那行不通。 If you need to do that, use a postback or an asynchronous postback with an UpdatePanel. 如果需要执行此操作,请使用带有UpdatePanel的回发或异步回发。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM