Ali
Ali

Reputation: 1759

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

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 class="rowMenu" 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

Upvotes: 0

Views: 242

Answers (1)

Fawlty
Fawlty

Reputation: 497

The issue is with this line:

<div class="rowMenu" 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 class="rowMenu" 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>

Upvotes: 3

Related Questions