2017年10月20日 星期五

[ASP.NET] vb.net call javascript function

vb.net call javascript function

一、
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="javascript:openUserAppSetting();return false;" />

二、
<asp:Button ID="but_save" runat="server" Text="提交變更" class="btn btn-primary"  OnClientClick="return openUserAppSetting();"/>

<asp:Button id="Button2" runat="server" Text="XD" OnClientClick="closeUserAppSetting();" OnClick="Button2_Click"  UseSubmitBehavior="false" />
OnClientClick會在onclick之前執行

三、
ScriptManager.RegisterClientScriptBlock(Me, Me.GetType(), "", "showDisplay()", True)
ScriptManager.RegisterStartupScript(Me, Page.GetType, "Script", "showDisplay();", True)

<script type="text/javascript">
        function openUserAppSetting() {
            $('#userAppSetting').skygqbox();
        }

        function closeUserAppSetting() {
            $('#userAppSetting').hide();
        }
</script>



HTML/javascript call vb function

[ HTML ]
<td onclick="callServersideFunction()">日期</td>

[ javascript ]
//call vb function
 function callServersideFunction() {
         var someValueToPass = name;
          __doPostBack('CustomPostBack', someValueToPass);

}

[ vb ]
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    ' Insure that the __doPostBack() JavaScript method is created...
    Me.ClientScript.GetPostBackEventReference(Me, String.Empty)

    'If IsPostBack = False Then
    Dim eventTarget As String = ""
    Dim eventArgument As String = ""

    If Request("__EVENTTARGET") Is Nothing Then
        eventTarget = String.Empty
    Else
        eventTarget = Request("__EVENTTARGET")
    End If

    If Request("__EVENTARGUMENT") Is Nothing Then
        eventArgument = String.Empty
    Else
        eventArgument = Request("__EVENTARGUMENT")
    End If

    If eventTarget = "CustomPostBack" Then
        Dim valuePassed As String = eventArgument
        ' there call function....
    End If
    ' End If

End Sub







沒有留言:

張貼留言