如何在java脚本中调用类文件函数 [英] How to call a class file function in java script
问题描述
你好
如何在java脚本中调用类文件函数..我有一个类文件让class1.cs在其中定义一个函数let log_record()....我想调用这个函数在我的java脚本函数中我怎么能这样做请帮助.....
我的完整类文件class1.cs是.....
Hello
How to call a class file function in java script..I have a class file let class1.cs in which i define a function let log_record()....i wanna call this function in my java script function how can i do this please help .....
My complete Class file class1.cs is.....
public class age_calculator
{
public age_calculator()
{
//
// TODO: Add constructor logic here
//
}
public void log_info(out string browser, out string pagepath, out string pagename, out string ip, out string dt)
{
string pgepath = System.Web.HttpContext.Current.Request.Url.AbsolutePath;
pagepath = System.Web.HttpContext.Current.Request.Url.ToString();
System.IO.FileInfo oinfo = new System.IO.FileInfo(pgepath);
pagename = oinfo.Name;
string visitorIP = HttpContext.Current.Request.UserHostAddress.ToString();
ip = GetIP();
dt = Convert.ToString(DateTime.Now);
//string browser;
browser = HttpContext.Current.Request.Browser.Browser.ToString();
browser = browser + "-" + HttpContext.Current.Request.Browser.Version.ToString();
}
}
实际上我想打电话这个函数在我的经典asp页面上,但它不可能直接所以我想在java脚本中调用它然后在我的代码中调用javascript函数...如果你有任何其他方式那么请建议我..
谢谢&问候
Srishti
推荐答案
在aspx页面方法中调用类函数,然后通过javascript调用此方法。
见这里 ASP.NET - 如何从客户端JavaScript调用服务器端方法 [ ^ ]
Call your class function in an aspx page method then call this method via javascript.
See here ASP.NET – How to call a server-side method from client-side JavaScript[^]
请参阅:如何使用JavaScript排除ac#方法(代码隐藏) [ ^ ]
问候..
See this: How to excecute a c# Method (Code-Behind) with JavaScript[^]
Regards..
从服务器端调用函数用一个属性作为f来装饰你的函数ollow。
For calling a function from server side decorate your function with a Attribute which is as follow.
[WebMethod]
public static bool IsUserAvailable(string val)
{
if (val == "daljeet")
{
return true;
}
else
{
return false;
}
}
现在注册你的函数添加一个脚本管理器控件到你的页面使用EnablePageMethods属性集为真。
Now for registering your function add a script manager control to your page with EnablePageMethods property set to true.
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
</asp:ScriptManager>
<asp:Button ID="btnForPass" runat="server" Text="Click Here" OnClientClick="CallJavascript();"/>
</form>
现在按钮点击我正在调用一个调用函数的javascript函数服务器端。
now on the button click i am calling a javascript function which is calling the function from server side.
<script language="javascript" type="text/javascript">
function CallJavascript() {
PageMethods.IsUserAvailable(username, OnSucceeded);
}
function OnSucceeded(result, userContext, methodName) {
if (methodName == "IsUserAvailable") {
if (result == true) {
alert("Success");
}
else {
alert("Failed");
}
}
}
</script>
这篇关于如何在java脚本中调用类文件函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!