回答
你不能直接调用它的函数调用。因为Javascript是针对Web浏览器的脚本语言。
您可以使用AJAX或完整页面发布参数来允许您执行子例程。
阅读更多关于Ajax的信息,这是更好的方法。
要什么Kronass说,我发现这篇文章是做你想要什么http://encosia.com/2008/05/29/using-jquery-to-directly-call-aspnet-ajax-page-methods/在过去有用的扩展。 Encosia也有其他博客地块在此堆,如果你做一些搜索
的你正在寻找使用通常被称为一个WebMethod什么,ScriptMethod或页面方法取决于哪个框架您正在使用
执行此操作的一种方法是使用接口ICallbackEventHandler。我发现你有一个关于AjaxControToolkit CalendarExtender的问题,所以我猜这个问题与此有关,以及你如何在服务器端方法中进行一些验证。 ICallbackEventHandler是AJAX,但您可以将验证编写为常规方法,而不是PageMethod/WebMethod。它在JavaScript方面稍微有点繁琐,但不是太多。
让我们开始我们的基本的文本框,日历扩展:
<form id="form1" runat="server">
<asp:ScriptManager runat="server" ID="ScriptManager" />
<div>
<asp:TextBox runat="server" ID="DateTextBox" />
<ajaxtoolkit:CalendarExtender runat="server" ID="CalendarExtender" TargetControlID="DateTextBox"
PopupButtonID="SelectorButton" OnClientDateSelectionChanged="checkDate" Format="dd MMM yyyy" />
<asp:ImageButton runat="server" ID="SelectorButton" ImageUrl="Path to a pretty graphic" />
<br />
<asp:Label runat="server" ID="ValidDateLabel" />
</div>
</form>
我已经添加了扩展,因为这将揭开序幕调用服务器端方法的过程OnDateSelectionChanged属性;我们很快就会回到那里。
在你的类声明代码隐藏,你需要说你实现接口:
Partial Public Class _Default
Inherits System.Web.UI.Page
Implements ICallbackEventHandler
实现接口,我们则需要增加两个方法来处理的两种方法接口,RaiseCallbackEvent和GetCallbackResult。我们还需要一个属性来暂时存储我们试图验证的日期。
Private mCallbackDate As Date
Private Property CallbackDate() As Date
Get
Return mCallbackDate
End Get
Set(ByVal value As Date)
mCallbackDate = value
End Set
End Property
Public Sub RaiseCallbackEvent(ByVal eventArgument As String) Implements ICallbackEventHandler.RaiseCallbackEvent
'eventArgument will contain the date the user selected from the extender
Dim testDate As Date
If eventArgument = String.Empty Then
Else
If Date.TryParse(eventArgument, testDate) Then
'If we have a legal date selected then store it
Me.CallbackDate = testDate
End If
End If
End Sub
Public Function GetCallbackResult() As String Implements ICallbackEventHandler.GetCallbackResult
Dim result As String = String.Empty
'Get the date that we stored in memory and pass it to our CheckDate function
'We'll pass back to the Javascript in the page the string 'true' if the date is
'valid under our business rules and 'false' if it isn't
If checkDate(Me.CallbackDate) Then
Return "true"
Else
Return "false"
End If
End Function
Public Function checkDate(ByVal dateToCheck As Date) As Boolean
'If the date is in the future then return True, otherwise False
If dateToCheck > Date.Today Then
Return True
Else
Return False
End If
End Function
有服务器端,我们需要添加,在Page_Load中,它执行JavaScript和服务器端代码的挂接之一多一点。该ClientScriptManager的GetCallbackEventReference功能将注入一点脚本到我们的页面需要浏览器和服务器之间的通信服务。然后,我们只需要注册一个脚本块调用脚本注入 - 我们将调用这个函数checkDateOnServer。
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim callbackScript As String
callbackScript = "function checkDateOnServer(arg){" & _
Page.ClientScript.GetCallbackEventReference(Me, "arg", "receiveDateValidation", "") & _
"}"
ClientScript.RegisterClientScriptBlock(Me.GetType, "callback", callbackScript, True)
End Sub
回到客户端位。我们需要编写一个Javascript checkDate函数来将用户选择的日期传递给回调函数。
function checkDate()
{
// Get the date the user selected
var selectedDate = document.getElementById('DateTextBox').value;
// This will start the callback sequence
checkDateOnServer(selectedDate);
}
我们需要做的最后一位是接收来自服务器,这是我们在Page_Load中说,会被称为receiveDateValidation回来的价值。
function receiveDateValidation(arg, context)
{
var ValidDateLabel = document.getElementById('SelectedDateLabel');
// We get a string value back from the server which is 'true' or 'false'
if (arg == 'true')
{
ValidDateLabel.innerText = 'Your date IS valid';
}
else
{
ValidDateLabel.innerText = 'Your date IS NOT valid';
}
}
- 1. 从javascript调用VB.net函数
- 2. 从VB.Net类调用函数
- 3. 使用express.js路由的Javascript函数
- 4. C++函数调用路由解析器
- 5. 从vb.net调用javascript代码
- 6. 从VBScript中调用VB.net函数
- 7. 需要从VB.NET调用com函数
- 8. 从vb.net的模态窗口中调用javascript函数
- 9. 从CSS调用JavaScript函数
- 10. 从servlet调用JavaScript函数
- 11. 从xcode调用javascript函数
- 12. 从WebMethods调用Javascript函数
- 13. 从UIWebView调用javascript函数
- 14. 从Perl调用Javascript函数
- 15. 从vbscript调用javascript函数
- 16. 从Silverlight调用Javascript函数
- 17. 从HTML调用JavaScript函数
- 18. 从Javascript调用ActionScript函数
- 19. 从JavaScript调用Java函数
- 20. 从Javascript调用Azure函数
- 21. 从javascript调用ASP.Net函数
- 22. 从javascript调用c#函数
- 23. 从objectiveC调用javascript函数
- 24. 从Javascript函数调用BHO
- 25. 从javascript调用ajax函数
- 26. 从activex调用javascript函数
- 27. 从javascript调用VB函数
- 28. 从JavaScript调用php函数
- 29. 从函数的函数调用javascript中
- 30. 由javascript调用的javascript函数由php回声不响应