locked
Calling vb.net function from Javascript or HTML RRS feed

  • Question

  • User1239610449 posted

    I want to call a vb.net function from javascript. the scenario is I am executing a for next loop in which I am creating hyperlink using HTMLAnchor control and by clicking on this control i want to pass some value to vb.net function.

    Please help, i am stuck on it.

    Tuesday, June 18, 2019 12:27 PM

All replies

  • User475983607 posted

    JavaScript cannot directly invoke a vb.net function but JavaScript can make an HTTP request.  There are a lot of ways to solve this problem.  In Web Forms it is common to create a static [WebMethod] in an aspx. page that the JavaScript code invokes via an HTTP POST.

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AjaxDemo.aspx.cs" Inherits="WebFormsDemo.AjaxDemo" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
       
    </head>
    <body>
        <form id="form1" runat="server">
            <asp:LinkButton ID="LinkButton1" runat="server">LinkButton</asp:LinkButton>
        </form>
         <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
        <script>
    
            $("#<%= LinkButton1.ClientID%>").click(function (e) {
                e.preventDefault();
                var data = {
                    inputParam: "Hello World"
                }
                    $.ajax({
                        type: "POST",
                        url: '/AjaxDemo.aspx/DoSomething',
                        data: JSON.stringify(data),
                        dataType: 'json',
                        contentType: "application/json; charset=utf-8",
                    }).done(function (data) {
                        console.log(data);
                    });
            });
        </script>
    </body>
    </html>
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading;
    using System.Web;
    using System.Web.Script.Services;
    using System.Web.Services;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace WebFormsDemo
    {
        public partial class AjaxDemo : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
    
            }
    
            [WebMethod]
            public static string DoSomething(string inputParam)
            {
                return $"You sent {inputParam}.";
            }
    
        }
    }

    Tuesday, June 18, 2019 1:47 PM
  • User-719153870 posted

    Hi knowledgist,

    As mgebhard said, we usually use jquery. Ajax to call webmethod to achieve what you said.

    However, we also recommend another way that we can use ScriptManager to call PageMethods.

    Please refer to codes below for further information:

    ASPX:

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <title></title>
    <script type="text/javascript">
    function ShowText() {
    PageMethods.GetText(document.getElementById("<%=TextBox1.ClientID%>").value, OnSuccess);
    }
    function OnSuccess(response) {
    alert(response);
    }
    </script>
    </head>
    <body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
    </asp:ScriptManager>
    <div>
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:Button ID="Button1" runat="server" Text="show" OnClientClick="ShowText()" />
    </div>
    </form>
    </body>
    </html>

    CS

    protected void Page_Load(object sender, EventArgs e)
    {
    
    }
    
    [System.Web.Services.WebMethod]
    public static string GetText(string Text)
    {
    string MyText = "My text is :" + Text;
    return MyText;
    }

    Hereis result of my demo:

    Best Regard,

    Yang Shen

    Wednesday, June 19, 2019 6:35 AM
  • User753101303 posted

    Hi,

    Is this even what you need? If your server side code is called when clicking the link you don't need to use Ajax. If you want to take full advantage of webforms I would suggest to have a look at https://docs.microsoft.com/en-us/aspnet/web-forms/overview/presenting-and-managing-data/model-binding/retrieving-data

    You can use a "data bound" control such as a repeater which will includes control taken some of their properties from the data source and then all this is rendered for you witthout any further work.

    Wednesday, June 19, 2019 8:10 AM