简体   繁体   English

ASP.net:从ajax javascript调用webmethod。 [vb.net]

[英]ASP.net: Calling a webmethod from ajax javascript. [vb.net]

I'm creating dynamically a Popup-Extender with some controls. 我正在动态创建带有某些控件的Popup-Extender。 One control is a Button which has the "onclick" attribute and has to call the JavaScript method submit. 一个控件是Button,它具有“ onclick”属性,并且必须调用JavaScript方法Submit。 I'm creating the Popup-Extender and it's controls in "SiteMaster.vb". 我正在创建Popup-Extender,它是“ SiteMaster.vb”中的控件。 the attribute "onclick" of the button is set in the "click-Event" of the Button which calls the "modalpopup.show" Method. 按钮的属性“ onclick”在按钮的“ click-Event”中设置,该按钮调用“ modalpopup.show”方法。 The created html-Code which i can see with the developer-tool from my Browser is: 我可以通过浏览器的开发人员工具看到的创建的html代码为:

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

In my opinion everything looks fine until now. 在我看来,到目前为止一切都还不错。 I have written the JavaScript Method "submit" in the Source of Site.Master: 我已经在Site.Master的源代码中编写了JavaScript方法“提交”:

<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>

At Default.aspx.vb i have written the webmethod: 在Default.aspx.vb,我编写了web方法:

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

But I have the feeling that JavaScript method is never called because I cannot see that in the logs in browser console as I have put console.log() Method in JavaScript function which is being called. 但是我感觉从来没有调用JavaScript方法,因为我没有在浏览器控制台的日志中看到它,因为我将console.log()方法放在了要调用的JavaScript函数中。

The input type you have used is submit , so once you click this button it won't call javascript function, and will directly submit the form. 您使用的输入类型是submit ,因此,一旦单击此按钮,它将不会调用javascript函数,而将直接提交表单。 So instead of this please change use type="button" to call JavaScript function 因此,请代替使用type="button"来调用JavaScript函数

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

This is the button event where i call modalpopup.show. 这是我称为modalpopup.show的按钮事件。 in the method load Container_LoadControls the button is generating. 在方法load Container_LoadControls中,按钮正在生成。 later i add the attribute onklick 稍后我添加属性onklick

I just saw that the Javascript-Method "submit" can be called from the buttons which are not created in the ButtonClick()-Event. 我只是看到可以从不在ButtonClick()-Event中创建的按钮调用Javascript-Method“ submit”。 Someone has a solution? 有人有解决方案吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM