typescriptvb.net

Calling VB function to Typescript


i have this function on a vb class

Public Shared Function UserTeDrejta(rule As String, userid As Integer) As String
    Dim aa As New DefaultDataSetTableAdapters.QueriesTableAdapter
    Dim perdorues As String = ""
    Try
        Dim result As Integer = aa.CheckakaTeDrejte(rule , userid )
        If result > 0 Then
            perdorues += "row.dataset.userId = user.UserID;
                        const idCell = document.createElement(""td"");
                        idCell.textContent = user.UserID;
                        row.appendChild(idCell);
                        const usernameCell = document.createElement(""td"");
                        usernameCell.textContent = user.Username;
                        row.appendChild(usernameCell);"
        End If
    Catch ex As Exception
        perdorues = ""
    End Try
    Return New JavaScriptSerializer().Serialize(perdorues)
End Function

I want to call it here were i will add the UserID that has loged in and a rule "Modifiko_Perdorues"
i am not allowed to use WebMethod

hfUserID.Value = Session("UserID").ToString()
<asp:HiddenField ID="hfUserID" runat="server" />

function populateUsersTable(users: any[]) {
    const tableBody = document.querySelector("#perdorues tbody");
    if (!tableBody) return;

    tableBody.innerHTML = ''; // Clear existing rows

    users.forEach(user => {
        const row = document.createElement("tr");
        //row.dataset.userId = user.UserID; // Store user ID in the row

        //const idCell = document.createElement("td");
        //idCell.textContent = user.UserID;
        //row.appendChild(idCell);

        //// Username column
        //const usernameCell = document.createElement("td");
        //usernameCell.textContent = user.Username;
        //row.appendChild(usernameCell);

Solution

  • first method api with cookie not session, add Contoller on your name class

    Imports System.Web.Http
    Imports System.Web.Script.Serialization
    Imports BariPOS.DefaultDataSetTableAdapters
    Imports System.Web.Services
    
    Public Class CheckTeDrejtaController
        Inherits ApiController
    
        <HttpGet>
        <Route("api/CheckTeDrejta/CheckBox")>
        Public Function CheckBoxTeDrejta(edrejta As String) As IHttpActionResult
            Dim useriCookie = HttpContext.Current.Request.Cookies("UserAuth")
            If useriCookie Is Nothing OrElse String.IsNullOrEmpty(useriCookie.Value) Then
                Return Unauthorized()
            End If
            Dim useri As Integer = Convert.ToInt32(useriCookie.Value)
    
            Dim aa As New DefaultDataSetTableAdapters.QueriesTableAdapter
            Dim butonat As New StringBuilder("")
            Try
                Dim result As Integer = aa.CheckakaTeDrejte(edrejta, useri)
                If result > 0 Then
                    butonat.Append("const checkboxCell = document.createElement(""td"");
                                 const checkbox = document.createElement(""input"");
                                 checkbox.type = ""checkbox"";
                                 checkbox.name = ""rightCheckbox"";
                                 checkbox.value = right.TeDrejtatID;
                                 checkbox.dataset.rightName = right.PermissionName;
                                 checkbox.checked = false;
                                 checkboxCell.appendChild(checkbox);
                                 row.appendChild(checkboxCell);")
                End If
            Catch ex As Exception
                butonat = New StringBuilder("")
            End Try
            Return Ok(butonat.ToString())
    
        End Function
    enter code here
    

    second method with inject: needs to be placed on Page_Load

    If Not IsPostBack Then
                Dim csrfToken As String = Guid.NewGuid().ToString()
                hfCSRFToken.Value = csrfToken
                Session("CSRFToken") = csrfToken
    
                Dim userID As Integer = Convert.ToInt32(Session("UserID"))
                Dim tsCode1 As String = CheckTeDrejtaController.UserTeDrejta("Modifiko_Perdorues", userID)
    
                If Not String.IsNullOrEmpty(tsCode1) Then
                    ClientScript.RegisterStartupScript(Me.GetType(), "permUser",
                        $"window.injectUserTeDrejta = {tsCode1};", True)
                End If
            End If
    

    the third is webmethod same as api but old not recommended