[英]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:
如果您的项目正在使用:
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
. 它必须是
protected
或public
,不能是private
。
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. 这些是几种方法,希望可以为您指明正确的方向。
You need to use webmethods: 您需要使用网络方法:
Have a look at: http://encosia.com/using-jquery-to-directly-call-aspnet-ajax-page-methods/ 看看: http : //encosia.com/using-jquery-to-direct-call-aspnet-ajax-page-methods/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.