Collect data from JavaScript using the ASP.Net TreeView

I have a custom user control (ascx) with an ASP.NET treeview inside. I am attempting to retrieve the value from another ascx control on the same page using the following method.

       var treeViewData = window["<%=TreeView1.ClientID%>" + "_Data"];
        if (treeViewData.selectedNodeID.value != "") {
            var selectedNode = document.getElementById(treeViewData.selectedNodeID.value);
            var value = selectedNode.href.substring(selectedNode.href.indexOf(",") + 3, selectedNode.href.length - 2);
            var text = selectedNode.innerHTML;
            alert("Text: " + text + "\r\n" + "Value: " + value);
        } else {
            alert("No node selected.")
        }

However, it is unable to locate TreeView1 because it resides in a different ascx control. Any suggestions on how to accomplish this?

Answer №1

To establish a JavaScript variable within your initial user control, insert the following code:

var uniqueID = "<%=SomeElement.ClientID%>";

Then, utilize this JavaScript variable in your subsequent user control to access the element (JavaScript variables have global scope).

var element = window[uniqueID + "_Data"] ;

IMPORTANT: In order for this method to function properly, the first control must be rendered before the second one.

Answer №2

One way to share data between two user controls in an ASPX page is by setting a global JavaScript variable from the first control (ascx) when a node is selected in a tree view, and then accessing this variable from the second control. While both controls may work together in terms of JavaScript code, their server-side ASPX and CS (C#) code remains separate.

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

Unlocking the Power of RxJS with Observable Sharing

Let's take a look at a function that contains the code below: private foo() { let obs: Observable<any> = this._http.get<number>('/foo') obs.subscribe(x=> { console.log("foo : " + x) }); this.blah(ob ...

Does JSFiddle work well with JQuery Mobile version 1.1.1?

As a newcomer to the world of coding, I’ve spent the past two days diving deep into code-related research. One thing that has sparked my curiosity is exploring the impact of using the jsfiddle website with JQuery Mobile. Currently, I’m knee-deep in a ...

Experiencing discrepancies in pixel height while conducting tests using Nightwatch

Let me start by sharing some details about my machine setup: Operating System: CentOS 7 Graphics Drivers: kmod-nvidia (dedicated GPU) I am currently testing a webpage using Nightwatch, and one of the requirements is to ensure that a background image&apo ...

Tips for obtaining the correct Javascript output for the number pyramid

I'm currently in the process of creating a half pyramid of numbers, but I'm facing an issue with getting the output to show the total for each line while maintaining everything except the * sign between the numbers. If anyone is able to offer som ...

Tips for Guaranteeing a Distinct Email and Username are Stored in MongoDB with Mongoose

db.UserSchema = new db.Schema({ user: {type:String, unique:true, required:true,index:true}, email: {type:String, unique:true, required:true,index:true}, password: {type:String, required:true}, phon ...

Integrating Material-UI Dialog with Material-table in ReactJS: A Step-by-Step Guide

I have implemented the use of actions in my rows using Material-Table, but I am seeking a way for the action to open a dialog when clicked (Material-UI Dialogs). Is there a way to accomplish this within Material-Table? It seems like Material-UI just appen ...

An error occurred with the datepicker: Unable to connect to 'bsValue' as it is not recognized as a property of 'input'

Despite importing DatepickerModule.forRoot() in my Angular unit test, I am encountering the following error: Error: Template parse errors: Can't bind to 'bsConfig' since it isn't a known property of 'input'. (" ...

Tips for activating Vue.js production mode with webpack 2.7

I have encountered performance issues with Vue.js in my existing code base. Additionally, I noticed a notice in the browser console: https://i.sstatic.net/KY1B3.png So, I believe that one simple solution could be to switch Vue into production mode. Foll ...

What is the best way to retrieve the most recent CMS posts information within a Gatsby-constructed project?

I created a static website using Gatsby and everything was working well. However, I encountered an issue when updating the titles and content of posts in Contentful CMS - the changes were not reflected when I refreshed the website. How can I ensure that ...

The process of incorporating the dymo.framework into an Angular project

My Angular project is currently in need of importing dymo.connect.framework. However, I am facing some challenges as the SDK support provided by dymo only explains this process for JavaScript. I have also referred to an explanation found here. Unfortunate ...

What is the best way to achieve consistent alignment in Javascript?

I have been attempting to incorporate HTML code within JavaScript for alignment and styling. Despite searching on Google for 3 to 4 days, I have not found a suitable answer yet. What exactly am I looking for? Here is a snippet of my JavaScript code: funct ...

Is there a utility that can track database performance in terms of hits and duration on each page

Profiling SQL Server with SQL Server profiler is effective for analyzing the performance of web applications. However, during my web app testing process, I am interested in a summary of database interactions and duration specific to each page. Is there an ...

Generating elements added at various depths within an HTML document with the help of JavaScript

create_new.append("div") .append("form").merge(update_5) .attr("action", d => d.market) .attr("target","_blank") .style("width","100%") .style("height","282") .append("input").merge(update_5) .attr("type","submit") ...

The error message "A form control with an invalid name attribute is not focusable" is displayed when attempting to use an input type file in Material

I've created a form using Material UI as a functional component, with the following structure: <form className={classes.container} onSubmit={show}> <Grid container item xs={12} alignItems="center"> <input accept=".xlsx,.xls" cl ...

Trouble with event.preventDefault functionality

This snippet of code I'm working on is pretty basic, nothing too intricate. $("input#send").submit(function(event){ event.preventDefault(); $.ajax({ type: 'POST', url: add.php, data: data, succe ...

Tips on how to prevent altering the formula selection following the onchange event

Hey there, I'm facing an issue with JavaScript. Please check out my website . What I want is that when I select "Option One", even after reloading the website, it should stay selected as "Option One" and not revert back to "Select...". Any ideas on ...

What steps can I take to combine the values from an array of dictionaries?

Here is my current data: a = [{"country":"Colombia","date":"1995"}, {"country":"China","date":"1995"},{"country":"USA","date":"1992"}] ...

Exporting modules in node.js is driving me crazy. Just four lines of code and I can't figure out what's wrong

Currently, I am delving into the realm of node.js through an online course on Udemy. However, I've run into a perplexing issue that seems to defy logic and reason. The lesson revolves around accessing external files in node.js using module.exports. I ...

Selenium is encountering an issue where it is unable to automatically download files, as the download confirmation

While reviewing someone else's code, I encountered an issue with automatically downloading PDF files from a web page using selenium. Despite setting the browser.helperApps.neverAsk.saveToDisk property to the PDF mime type, I am still getting the fire ...

HTML5 Video Frozen in Place on Screen's Edge

I am encountering a problem specifically on Webkit, particularly in web view on iOS. When I tested it on desktop Chrome, the issue did not appear. Here is the Portrait image and Here is the Landscape image The video seems to be fixed in one position rega ...