简体   繁体   English

在ASP.NET用户控件中,我想向客户端公开方法

[英]In ASP.NET user control, i want to expose a method to client side

I have an User Control Which is having a lot of lot methods. 我有一个用户控件,其中有很多方法。 I want to expose a method to client. 我想向客户公开一种方法。 So that i can call the method of the control in javascript and get the result. 这样我就可以在javascript中调用控件的方法并获取结果。

For Example: A method called, GetData(), which will get the data from the server. 例如:一个名为GetData()的方法,该方法将从服务器获取数据。 Currently i am calling this method only on code behind. 目前,我仅在后面的代码中调用此方法。 It will return the data. 它将返回数据。 Now i want to call the same method in client side. 现在我想在客户端调用相同的方法。

Is it possible? 可能吗?

You have a couple of approaches, which will vary on your technology. 您有两种方法,具体取决于您的技术。 If your project is using: 如果您的项目正在使用:

  • Web-Forms: 网络表格:

Example: 例:

<div>
     <span><%= GetData(Eval("Data Column").ToString()) %></span>
</div>

That would be your front end, you notice how your exposing the method. 那将是您的前端,您会注意到如何公开该方法。

protected string GetData(string content)
{
     // Do Something.
}

That is the code behind, which is your method. 那就是背后的代码,这就是你的方法。 It must be protected or public it can not be private . 它必须是protectedpublic ,不能是private

  • Web-Method: This is viable, but as mentioned above it isn't supported anymore. 网络方法:这是可行的,但是如上所述,它不再受支持。 So I won't provide an example, though they exist. 因此,尽管它们存在,但我不会提供示例。
  • Model View Controller: 模型视图控制器:

Example: 例:

<div>
     <span>@{ ClassName.GetData() }</span>
</div>

That is the front end, as you notice you use Razor to expose the method from behind. 那是前端,正如您注意到的那样,您使用Razor从后面公开该方法。 That will help you call a method from the Controller . 这将帮助您从Controller调用方法。

Those are a couple of approaches, hopefully that points you in the right direction. 这些是几种方法,希望可以为您指明正确的方向。

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

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