从aspx中的代码调用JavaScript

时间:2013-12-09 19:19:20

标签: javascript asp.net vb.net

我有这个javascript:

<script language="javascript" type="text/javascript">
    function NewWindow(mypage, myname, w, h, scroll, pos) {
        var win = null;
        if (pos == "random") {
            LeftPosition = (screen.width) ? Math.floor(Math.random() * (screen.width - w)) : 100;
            TopPosition = (screen.height) ? Math.floor(Math.random() * ((screen.height - h) - 75)) : 100;
        }
        if (pos == "center") {
            LeftPosition = (screen.width) ? (screen.width - w) / 2 : 100;
            TopPosition = (screen.height) ? (screen.height - h) / 2 : 100;
        }
        else if ((pos != "center" && pos != "random") || pos == null) {
            LeftPosition = 0; TopPosition = 20
        }
        settings = 'width=' + w + ',height=' + h + ',top=' + TopPosition + ',left=' + LeftPosition + ',scrollbars=' + scroll + ',location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=no';
        win = window.open(mypage, myname, settings);
    }
</script>

我正试图从我的代码中调用此函数,如下所示:

Dim Message As String = "This is My new Message"
Dim url As String = HttpContext.Current.Request.Url.AbsoluteUri
Dim script As String = "NewWindow(" & url & "," & Message & ",200,50,Yes,random)"
        If Not Page.ClientScript.IsStartupScriptRegistered(Me.GetType(), "alertscript") Then
            Page.ClientScript.RegisterStartupScript(Me.GetType(), "alertscript", script, True)
        End If

所有想法都是在我的网站上设置弹出式窗口或错误信息 vb代码采用了我想要的所有属性 但是我的aspx.page中的函数没有被调用。

2 个答案:

答案 0 :(得分:1)

请试试这个:

Default.aspx的

<%@ Page Title="Home Page" Language="vb" MasterPageFile="~/Site.Master" AutoEventWireup="false"
    CodeBehind="Default.aspx.vb" Inherits="TesteVB._Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <h2>
        Welcome to ASP.NET!
    </h2>
    <script language="javascript" type="text/javascript">
        function Test() {
            alert('Banzai');
        }

        function NewWindow(mypage, myname, w, h, scroll, pos) {
            var win = null;
            if (pos == "random") {
                LeftPosition = (screen.width) ? Math.floor(Math.random() * (screen.width - w)) : 100;
                TopPosition = (screen.height) ? Math.floor(Math.random() * ((screen.height - h) - 75)) : 100;
            }

            if (pos == "center") {
                LeftPosition = (screen.width) ? (screen.width - w) / 2 : 100;
                TopPosition = (screen.height) ? (screen.height - h) / 2 : 100;
            }
            else if ((pos != "center" && pos != "random") || pos == null) {
                LeftPosition = 0; TopPosition = 20
            }

            settings = 'width=' + w + ',height=' + h + ',top=' + TopPosition + ',left=' + LeftPosition + ',scrollbars=' + scroll + ',location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=no';

            win = window.open(mypage, myname, settings);
        }
    </script>
</asp:Content>

Default.aspx.vb

Public Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim csname1 As String = "PopupScript"
        Dim csname2 As String = "AutoPopup"
        Dim cstype As Type = Me.GetType()

        ' Get a ClientScriptManager reference from the Page class.
        Dim cs As ClientScriptManager = Page.ClientScript

        ' Check to see if the startup script is already registered.
        If (Not cs.IsStartupScriptRegistered(cstype, csname1)) Then

            'Dim cstext1 As String = "Test();"
            'cs.RegisterStartupScript(cstype, csname1, cstext1, True)

            Dim Message As String = "This is My new Message"
            Dim url As String = HttpContext.Current.Request.Url.AbsoluteUri
            Dim script As String = "NewWindow('" & url & "','" & Message & "',200,50,'Yes','random');"

            cs.RegisterStartupScript(cstype, csname1, script, True)

        End If
    End Sub

End Class

答案 1 :(得分:0)

我想你想把“NewWindow”放在你的脚本所在的地方..就像这样......

Page.ClientScript.RegisterStartupScript(this.GetType(), "Call my function", "NewWindow();", true);

这就是我所说的。

编辑:试试这个。它没有经过测试。

Page.ClientScript.RegisterStartupScript(Me.[GetType](), "Call my function", "NewWindow();", True)