asp.net 前后台交互 ICallbackEventHandler

<script type="text/javascript">
            function Success(args, context) {  
            if (args != 'error') {
                alert('执行的是' + args + '方法');
                if (args == 'Confirm') {
                    document.getElementById('txt').value = 'aaa';
                    document.getElementById('txt1').value = 'name name';
                    document.getElementById('txt2').value = 'aaa';
                    //document.getElementById('lblName').innerHtml = 'abababab';
                }
                else {
                    document.getElementById('txt').value = '';
                    document.getElementById('txt1').value = '';
                    document.getElementById('txt2').value = '';
                    //document.getElementById('lblName').innerHtml = '';
                }
               
            }  
        }  
        function Error(args, context) {
            alert('error');
            docume.t.getElementById('lblName').value = '';
       } 

     </script> 


Public Class WebForm4
    Inherits System.Web.UI.Page
    Implements System.Web.UI.ICallbackEventHandler

    Public dic As Dictionary(Of String, String)
    Private returnValue As String

    Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim cbReference As String
        cbReference = Page.ClientScript.GetCallbackEventReference(Me, _
            "args", "Success", "", "Error", False)
        Dim callbackScript As String = ""
        callbackScript &= "function CallServer(args, context) { " & _
            cbReference & "} ;"

        Page.ClientScript.RegisterClientScriptBlock(Me.GetType(), _
            "CallServer", callbackScript, True)

    End Sub

    Protected Sub txt_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles txt.TextChanged

        'Page.ClientScript.RegisterStartupScript(Me.GetType(), "after", "alert('aaaaa');", True)
        'Page.ClientScript.RegisterStartupScript(Me.GetType(), "", "confirm('确认吗?');", True)


        'Page.ClientScript.RegisterStartupScript(Me.GetType(), "confirm", "CallServer(confirm(确认吗?),null);", True)
        If Me.txt.Text = "" Then
            Return
        ElseIf Me.txt2.Text <> "" AndAlso Me.txt.Text = Me.txt2.Text Then
            Me.txt2.Text = ""
            Return
        End If

        Me.txt.Text = ""
        Me.txt1.Text = ""
        Me.txt2.Text = ""
        Page.ClientScript.RegisterStartupScript(Me.GetType(), "confirm", "CallServer(confirm('确认吗?'),null);", True)
    End Sub





    Public Sub RaiseCallbackEvent(ByVal eventArgument As String) Implements System.Web.UI.ICallbackEventHandler.RaiseCallbackEvent
        dic = New Dictionary(Of String, String)
        dic.Add("true", Confirm())
        dic.Add("false", Cancel())

        'If eventArgument = "false" Then
        '    Me.txt.Text = ""
        '    Me.lblName.Text = ""
        'Else
        '    Me.txt1.Text = "aaaaaabbbbbb"
        '    Me.lblName.Text = "afasdgasdg" & eventArgument
        'End If

        returnValue = dic(eventArgument)
    End Sub

    Public Function GetCallbackResult() As String Implements System.Web.UI.ICallbackEventHandler.GetCallbackResult

        Return returnValue

    End Function


    Public Function Confirm() As String
        Return "Confirm"
    End Function

    Public Function Cancel() As String
        Return "Cancel"
    End Function

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
        'Page.ClientScript.RegisterStartupScript(Me.GetType(), "", "confirm(确认吗?);", True)
        Dim m_txt As String = Me.txt.Text
        Dim m_name As String = Me.lblName.Text
    End Sub
End Class

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值