【问题标题】:call a vb.net subrouting from a javascript function?从 javascript 函数调用 vb.net 子路由?
【发布时间】:2010-04-05 15:59:43
【问题描述】:

大家好,我在后面的代码中有一个名为 CheckDate() 的子例程。

如何从 javascript 函数中调用该子例程?

干杯,

-琼斯

【问题讨论】:

    标签: asp.net javascript vb.net


    【解决方案1】:

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

    您可以使用 AJAX 或整页帖子发送参数以允许您执行子程序。

    阅读有关 Ajax 的更多信息,这是更好的选择。

    【讨论】:

      【解决方案2】:

      为了扩展克罗纳斯所说的内容,我发现这篇文章在过去对做你想做的事很有用http://encosia.com/2008/05/29/using-jquery-to-directly-call-aspnet-ajax-page-methods/。如果您进行一些搜索,Encosia 也有大量关于此的其他博客情节

      您要使用的通常称为 WebMethod、ScriptMethod 或 Page Method,具体取决于您使用的框架

      【讨论】:

        【解决方案3】:

        一种方法是使用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';
                }
            }
        

        【讨论】: