2010-04-05 71 views

回答

2

你不能直接调用它的函数调用。因为Javascript是针对Web浏览器的脚本语言。

您可以使用AJAX或完整页面发布参数来允许您执行子例程。

阅读更多关于Ajax的信息,这是更好的方法。

1

执行此操作的一种方法是使用接口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'; 
     } 
    }