如何使用c#从web方法调用函数 [英] How to call a function from web method using c#

查看:72
本文介绍了如何使用c#从web方法调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下功能。



I have following function.

protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
       {
          //Some code here...

       }





其他是网络方式。





And other is web method.

[WebMethod]
        static public void ImageButtonClick()
        {
             //what code I have to write here..?
        }





我想从webMethod调用ImageButton1_Click。

怎么做?



I want to call ImageButton1_Click from webMethod.
How it will done?

推荐答案

您正在尝试从中访问Windows窗体方法网络服务。这是可能的,但不推荐,因为在这种情况下您可能会丢失一些值;要访问这些值,您必须在Windows窗体应用程序中进行大量更改。如果您选择不这样做,这是您的决定,但我会告诉您如何以最简单的方式做到这一点。



如果两者都是在相同的解决方案中你可以访问ImageButton1_Click,那么这是可能的,但我认为方式不是直接的。



一种方法是更改​​签名ImageButton1_Click方法。将它从protected更改为public,如果该类默认不公开,也将其类公开。之后,您可以在Web方法中创建该类的对象,并可以调用其公共方法。要调用上面提到的方法,你可以编写如下代码:



Class1 obj = new Class1();

obj。 ImageButton1_Click(null,null);

obj.ImageButton1_Click(object sender,ImageClickEventArgs e)



基本上ImageButton1_Click是点击事件,所以你可以传递'null'作为参数,以防你没有使用'sender'和'e'的值,但是如果你使用的值反对'sender'和'e'那么你应该决定你应该传递哪些值case。
You are trying to access a Windows Form method from a web-service. This is possible but not recommended as you may be losing some values in that case; and to access those values you will have to make lots of changes in your Windows Form application. It's your decision if you opt to do it or not, but I am going to tell you how you can do that in the simplest possible way.

If they both are in the same solution and you have access to ImageButton1_Click, then this is possible but I think the way is not the direct one.

One way to do is to change signatures of ImageButton1_Click method. Change it from protected to public, also make its class public if that class is not public by-default. After that you can create object of that class in the web-method and can call its public methods. To call the method which you mentioned above, you can write code like this:

Class1 obj = new Class1();
obj.ImageButton1_Click(null, null);
obj.ImageButton1_Click(object sender, ImageClickEventArgs e)

Basically ImageButton1_Click is the click event, so you can pass 'null' as its parameters in case you are not using values against 'sender' and 'e', but in case you are using values against 'sender' and 'e' then you should decide which values you should pass in that case.


您指的是一个运行时错误,通常在未初始化对象时发生。因此,简单的调试将帮助您了解它出了什么问题。我觉得错误是在WebPage_Name obj = new WebPage_Name(); line只是因为初始化静态方法时,并不是所有内容都可用于初始化此对象。在该行上放置一个调试点,然后在调试环境中运行您的代码,一旦到达该行,请进入该方法并查看其中的每一行。我相信你会发现你的问题。
You are referring to a run-time error which usually occurs when a object is not initialized. So, a simple debug will help you get to know what is going wrong with it. I feel the error is at WebPage_Name obj = new WebPage_Name(); line just because when static method is initialized, not everything is available for this object to get initialized. Put a debug point on that line and then run your code in debug environment and once you are reached at that line, go within that method and look in to each line of it. I am sure you will catch your issue.


这篇关于如何使用c#从web方法调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆