Home > Software design >  How to get the value of an HTML element attribute that added from code behind?
How to get the value of an HTML element attribute that added from code behind?

Time:06-18

I have added an attribute isAdmin to an HTML tag as below:

Partial Class Controls_View_ViewOrder
Inherits System.Web.UI.UserControl
Protected Overrides Sub OnInit(e As EventArgs)

    Dim isAdmin = Page.RouteData.Values("UserAdmn")
    rowMenu.Attributes.Add("isadmin", isAdmin)

End Sub
End Class

Trying to get the value of isAdmin by JavaScript as below:

<div  id="rowMenu" runat="server"> </div>
.
.
.
let isAdmin = document.getElementById("rowMenu").getAttribute("isadmin");

but I got the below error: enter image description here

When I try to get the value of isAdmin I got error

CodePudding user response:

The issue is with this line:

<div id="rowMenu" runat="server"> </div>

Please write the code in this way:

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="Scrap._Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
             <div  id="rowMenu" isadmin="<%= Page.RouteData.Values("UserAdmn") %>" > </div>
             <script type="text/javascript">
                let isAdmin = document.getElementById("rowMenu").getAttribute("isadmin");
                alert(isAdmin);
            </script>
        </div>
    </form>
</body>
</html>
  • Related