ASP.net:从ajax javascript调用web方法。 [vb.net]

时间:2017-02-02 11:13:42

标签: javascript asp.net vb.net

我使用一些控件动态创建一个Popup-Extender。一个控件是一个按钮,它具有" onclick"属性并且必须调用JavaScript方法提交。我在" SiteMaster.vb"中创建了Popup-Extender及其控件。属性" onclick"按钮的设置在" click-Event" Button的调用" modalpopup.show"方法。我用浏览器中的开发人员工具看到的创建的html-Code是:

<input type="submit" name="ctl00$button_buttontestfooter_50_0_0" value="test" onclick="submit();" id="button_buttontestfooter_50_0_0" runat="server">

在我看来,一切看起来都很好。我编写了JavaScript方法&#34;提交&#34;在Site.Master来源:

<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<link href="~/Styles/Site.css" rel="stylesheet" type="text/css" />
<asp:ContentPlaceHolder ID="HeadContent" runat="server">
</asp:ContentPlaceHolder>
<script type="text/javascript">
    function submit() {
        alert("blaaaaa");
        console.log("jijijij");
        window.open("http://www.google.de");
        var dataValue = "{'name': 'Niklas'}";
        $.ajax({
            type: 'POST',
            url: "http://localhost:53470/GetCurrentTime",
            data: dataValue,
            contentType: 'application/json',
            dataType:'json',
            error: function (a, b, c) {
                console.log("jijijij")
                alert(a + ":" + b + "::::" + c);
            },
            success: function (data) {
                console.log("hallo");
                alert("" + data);
            }
        });
    }
</script>
</head>

在Default.aspx.vb,我写了webmethod:

Public Class _Default
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

End Sub
<System.Web.Services.WebMethod()>
Public Shared Function GetCurrentTime(ByVal name As String) As String
    Return name
End Function

但是我觉得JavaScript方法永远不会被调用,因为我在浏览器控制台的日志中看不到它,因为我在调用的JavaScript函数中放了console.log()方法。

3 个答案:

答案 0 :(得分:0)

您使用的输入类型为submit,因此一旦您点击此按钮,它就不会调用javascript函数,并会直接提交表单。因此,请更改使用type="button"来调用JavaScript函数

答案 1 :(得分:0)

Private Sub ButtonClick(sender As Object, e As EventArgs)
    Dim LayoutDef As ASPGlobalHandling.LayoutDefinition = p.Layout_Load("sessionitem_layouts", 0, "footer", "", False)
    ctrl.Container_LoadControls(updPnlContent.ContentTemplateContainer, 0, LayoutDef.Layout, "", 0)
    Dim c3po As Control = p.Container_FindControl(updPnlContent, "button_buttontestfooter_50_0_0")
    If TypeOf (c3po) Is Button Then
        Beep()
        Dim c3po1 As Button = CType(c3po, Button)
        c3po1.Text = "test"
        c3po1.Attributes.Add("OnClientClick", "submit();")
    End If
    modalPopUp.Show()
End Sub

这是我调用modalpopup.show的按钮事件。在方法中加载按钮生成的Container_LoadControls。后来我添加了onklick属性

答案 2 :(得分:0)

我刚刚看到Javascript-Method“submit”可以从ButtonClick() - Event中未创建的按钮调用。有人有解决方案吗?