Invoking a .js file within an UpdatePanel from the CodeBehind

I have been dedicating my time to self-teach ASP.NET and JavaScript for a project, and I've hit a roadblock that has consumed dozens of hours.

After discovering a fantastic drag-and-drop JavaScript list online, I copied the provided source code and organized the CSS into a .css file, the JavaScript into a .js file, and integrated the HTML and reference into my ASP.NET page. It worked flawlessly. Fantastic!

Subsequently, I replaced the JavaScript list with a static HTML list containing the same data, enclosed it in an UpdatePanel, and configured an "edit order" button to switch the static list's HTML with the JavaScript list's HTML upon button press.

No luck!

Initially, runtime errors occurred indicating that certain objects could not be located. For instance:

Microsoft JScript runtime error: Unable to get value of the property 'getElementsByTagName': object is null or undefined

I grasped this issue as the elements weren't present yet. Therefore, I removed the .js reference from the main header and attempted to register the .js file when the update panel changed instead.

This predicament perplexes me.

The majority of online explanations focus on RegisterClientScriptBlock, RegisterStartupScript, RegisterClientScriptInclude, or myLiteral, but none seem to solve my dilemma. Additionally, many resources detail how to execute a single JavaScript function, whereas the script I'm attempting to utilize consists of 700 lines! Must I individually reference each one?

Pardon me for posing what may seem like a novice question. I waited until I'd vehemently shouted at the screen enough times before seeking assistance!

Warm regards.

EDIT: CODE

As requested, here is the code:

VB.net (contained within a sub invoked by the button press. This is where the script needs to be registered)

    Dim script As String = ""

    Dim Labelb As Label = CType(FindControl("Labelb"), Label)


    Dim con As SqlConnection
    Dim cmd As SqlCommand
    con = New SqlConnection("[connection string here]")
    con.Open()
    Dim lrd As SqlDataReader

    cmd = New SqlCommand("[command string here]", con)
    lrd = cmd.ExecuteReader

    Dim item = ""



    While lrd.Read()
        item = item & "<li style=""position: relative;"">" & lrd(1) & "</li>"
    End While
    lrd.Close()

    item = "<table id=""phonetics""><tbody><tr><td><ul id=""phonetic3"" class=""boxy"">" & item & "</ul></td></tr></tbody></table><br/>"


    Labelb.Text = item

This represents the HTML update panel in the ASP.NET master page:

 <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true"/>
                            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                            <ContentTemplate>
                            <asp:Label ID="Labelb" runat="server" Text="" />

                            </ContentTemplate>
                            </asp:UpdatePanel>

Finally, this is the .js file requiring registration:

var ToolMan = {
// The rest of the JavaScript code goes here beyond this point...
}

Window.onload = function() {

junkdrawer.restoreListOrder("phonetic3")

//junkdrawer.restoreListOrder("twolists1")
//junkdrawer.restoreListOrder("twolists2")

dragsort.makeListSortable(document.getElementById("phonetic3"),
    verticalOnly, saveOrder)

/*
dragsort.makeListSortable(document.getElementById("twolists1"),
    saveOrder)
dragsort.makeListSortable(document.getElementById("twolists2"),
    saveOrder)
*/
}

function verticalOnly(item) {
item.toolManDragGroup.verticalOnly()
}

function speak(id, what) {
var element = document.getElementById(id);
element.innerHTML = 'Clicked ' + what;
}

function saveOrder(item) {
var group = item.toolManDragGroup
var list = group.element.parentNode
var id = list.getAttribute("id")
if (id == null) return
group.register('dragend', function() {
  ToolMan.cookies().set("list-" + id, 
      junkdrawer.serializeList(list), 365)
})
}
//-->

Your support is greatly appreciated!

Best wishes,

Answer №1

If you encounter the error message

"value of the property 'getElementsByTagName' object is null or undefined"
, it could be due to one of two reasons:

1) The script file may not have loaded properly.
2) 'getElementsByTagName' may be called incorrectly.

Without any sample code provided, it's challenging for anyone to give a definite solution to your issue.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Jquery Timer that can be toggled on and off with a single button

I'm really struggling to find a way to make this work smoothly without any bugs. The button in the code below is supposed to perform three actions: Initiate a countdown when clicked (working) Stop the countdown automatically and reset itself when it ...

PHP enables users to look at manual values in columns and MySQL values row by row

I have created a PHP program to organize seating arrangements for an exam hall. The user manually inputs the names of the halls, which should be displayed in columns in a table. The register numbers are fetched from a MySQL database and should be displayed ...

Innovative application transforms rigid input with dynamic hyphens

I am currently working on an input field specifically designed for Social Security Numbers (SSNs). My aim is to have an input with pre-placed hyphens, allowing the user to simply enter their 9-digit SSN while the numbers automatically space themselves arou ...

Configuring Tailwind CSS with PostCSS in a Nuxt project

Error Message "In order to avoid issues with features like alias resolving inside your CSS, please make sure to use build.postcss in your nuxt.config.js file instead of an external configuration file. Support for external config files will be depreca ...

Tips for accessing .js variables in .ejs files

I am facing an issue with my index.js and list.ejs files in Express. Here is the relevant code: index.js: const express = require("express"); const app = express(); app.set("view engine", "ejs"); app.set("views", __dirname + "\\views"); let ...

Interactive table with Draggable feature supported by Bootstrap Vue

After tirelessly searching for a solution to drag and drop rows on a Bootstrap Vue table, I finally stumbled upon a functional version here: Codepen I attempted to integrate this code into my own table: Template: <b-table v-sortable="sortableOptions ...

When working on a Vue project, the Navbar @click functionality seems to

Having trouble with my navbar search form <form action="" method="post" class="search"> <input type="text" name="" placeholder="поиск" class="input" v-model="alls ...

The ng-change event in AngularJS is not being activated by IE 11

Hello everyone, I am currently working with the angularjs framework and implementing a datepicker functionality. Unfortunately, the input type date is not functioning correctly on Internet Explorer. As a workaround, I have utilized jquery and css to create ...

Can dark mode be activated and maintained across multiple pages using just one JavaScript file?

I'm facing an issue with maintaining a dark mode across multiple web pages. I tried adding a JavaScript script to all my HTML files, but it didn't work as expected. Then, I experimented by adding the script to just one HTML file, and while it suc ...

React App folders are not being properly installed through NPX

Encountering an error message while attempting to use npx create-react-app Husna@LAPTOP-LPCC954R MINGW64 ~/Desktop/React GitHib Project (master) $ npx create-react-app github2020 Creating a new React app in C:\Users\Husna\Desktop\Reac ...

"Explore the Hong browser designed specifically for enhanced Ajax functionality

I made the decision to revamp my new job by incorporating Ajax into the mix. Here is the code snippet I used to load pages: html <html> <head> <meta charset="utf-8"> <title>Untitled Document</title> <link rel="stylesheet" ...

The functionality of `config.assets.debug = true` fails to

I've encountered an issue with the configuration on development where config.assets.debug = true doesn't seem to work correctly. Instead of having multiple separate JavaScript and CSS file inclusions, I'm getting a consolidated one: <li ...

Having issues with the link page in react-router-dom?

Currently, I am utilizing the react-router-dom library to manage routers. However, I have encountered an issue where linking to another page does not change the page, but instead adds the new page right after the previous one as shown in the image below. W ...

Creating a unique kendo ui widget from scratch

Is there a way to create a custom widget that functions similarly to the example shown in this sample: http://jsfiddle.net/anilca/u2HF7/ I found some helpful information here, but I'm struggling with defining dropdownlist templates and linking them ...

Angular refuses to showcase the object

Currently, I am working on a web application and facing an issue in parsing an object. I attempted to retrieve the IDs of all results but unfortunately, it is returning undefined in my console. Here's what I tried: var app = angular.module("DB", ...

How to implement setState within a Promise function using useEffect in React functional components with hooks?

I am struggling to set the value of a hook within a Promise function inside a useEffect(), and then store the returned promise value in the fruit hook so that it can be accessed in the return function of MyComponent() This is what I have attempted so far: ...

Creating MySQL query results in Node.js manufacturing process

I am looking to format the MySQL result in Node.js in a specific way. The desired result format should look like this: [ { "m_idx" :1 , "contents" : { "m_name" : "a", ...

What is the best way to delete HTML classes that were generated by a function?

Currently, I'm immersed in the Etch A Sketch project as part of my journey through The Odin Project. Using DOM manipulation, I successfully created a grid and displayed it on the screen. Now, my aim is to allow users to resize the grid by removing the ...

Perform DOM manipulation prior to triggering the AJAX event to prevent CSRF Error

Currently, I am faced with a challenge while working on Django. My goal is to implement a chained AJAX call - meaning that once one call returns, it triggers additional AJAX calls. Despite thorough research and referencing the suggested methods in the Djan ...

Merge two lists together based on their text properties and include an additional value

I am looking to transfer the values in brackets from the second list to the first one. My attempt at achieving this is shown below: $('#views-exposed-form-cat-watch-block .form-type-radio label.option').each(function(){ var a = this; $(& ...