Iterate over the controls within an UpdatePanel, and interact with JavaScript functions

I'm attempting to change all TextBoxes into labels on my page using the following code:

foreach (Control ctrl in masterform.Controls)
{
    if (ctrl.GetType() == typeof(TextBox))
    {
        TextBox t = ctrl as TextBox;
        t.ReadOnly = true;
        t.BackColor = transparent;
        t.BorderWidth = 0;
    }
}

However, I wrapped all the text boxes in an update panel which prevents me from accessing them. So I tried this:

foreach (Control ctrl in masterform.Controls)
{
    if (ctrl is UpdatePanel)
    {
        UpdatePanel s = ctrl as UpdatePanel;
        if (s == PartPanel)
        {
            foreach (Control ctrl2 in s.Controls)
            {
                if (ctrl2 is TextBox)
                {
                    TextBox t = ctrl2 as TextBox;
                    t.ReadOnly = true;
                    t.BackColor = transparent;
                    t.BorderWidth = 0;
                }
            }
        }
    }
}

This only shows the panels control count as 1, even though there are multiple textbox controls inside. Any assistance on this issue would be greatly appreciated.

Additionally, I have checkboxes that are mutually exclusive: If #1 is checked, #2 or #3 cannot be checked, and vice versa. I've written a function to handle this, but it only works if the update panel does not update:

var objChkd;
$(document).ready(function() 
{
    $('.mutuallyexclusive1').click(function () 
    {
        checkedState = $(this).attr('checked');
        $('.mutuallyexclusive2:checked').each(function () 
        {
            $(this).attr('checked', false);
        });
        $(this).attr('checked', checkedState);
    });
    $('.mutuallyexclusive2').click(function () 
    {
        checkedState = $(this).attr('checked');
        $('.mutuallyexclusive1:checked').each(function () 
        {
            $(this).attr('checked', false);
        });
        $(this).attr('checked', checkedState);
    });
});  

<input id="Chk1" type="checkbox" runat="server" class="mutuallyexclusive1" /><br />
<input id="Chk2" type="checkbox"  runat="server" class="mutuallyexclusive2" /><br />
<input id="Chk3" type="checkbox" runat="server" class="mutuallyexclusive2" /><br />

The issue arises when an update panel's update() function is triggered, causing the checkboxes to lose connection with the JavaScript function they are meant to call. Any assistance on resolving this problem would be highly appreciated. Thank you.

Answer №1

To update the textboxes within your UpdatePanel, you can use the following code snippet to recursively locate and update them:

void DisableTextBoxes(Control parent)
{
    foreach (Control ctrl in parent.Controls)
    {
        TextBox t = ctrl as TextBox;
        if (t != null)
        {
            t.ReadOnly = true;
            t.BackColor = transparent;
            t.BorderWidth = 0;
        }
        else
        {
            DisableTextBoxes(ctrl);
        }
    }
}

DisableTextBoxes(masterform);

As for the checkboxes, keep in mind that when the page loads, the script is executed and event handlers are bound to the controls at that time. However, if the checkboxes are updated within the DOM during an UpdatePanel refresh without a full page reload, the event handlers will not be automatically bound to the new elements. To address this, you will need to rebind the events after a partial page refresh.

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

"Exploring the Mini Drawer feature on Material UI brings up a whole new page for the Link

Currently, I am working on a project that involves updating stocks using Material UI. I have implemented a mini drawer from Material UI, but when I click on the menu link, it routes to a new page instead of rendering on the homepage itself. In my App.JS f ...

I am not receiving any results from the geocoder on Google maps

I am currently working on implementing the JS geocoding api but I keep encountering an error: Failed to load resource: net::ERR_BLOCKED_BY_CLIENT Despite having my api key activated. I have tried the following: Within HTML: <script src="{% stati ...

A guide to parsing JSON files and extracting information

How can I extract the name and status from a JSON object? I've attempted various methods like [0] - [1], as well as trying without, but to no avail. [ { "status": "OK" }, { "id": "1" ...

Encoding large files using the Base64 format

Is there a method to encode a file of, say, 2 gigabytes without splitting it into chunks? I am encountering errors when trying to handle files larger than 2GB due to their size being too large for the filesystem. Breaking the file into smaller chunks is ...

Rotate the image using the handler, not by directly manipulating the image itself

I need help rotating the image using a handler instead of directly on the image itself. I do not want to rotate the image when clicking and rotating it directly. Please do not suggest using Jquery UI rotatable because resizing the image with Jquery UI resi ...

Trigger an event upon completion of the angular $route.reload() function

I am encountering an issue where I need to refresh the route using the following code: $route.reload(); However, I also need to trigger a function just before the template is rendered again. For instance: $("#someTab").click(); Your help would be grea ...

MinifyJS - optimize all .js files within a directory

Seeking assistance to finalize my build process: I am currently transpiling es6 > es5 using babel. Once that is complete, I aim to minify all my .js files recursively with uglifyJS solely using NPM scripts (no grunt or gulp). My Requirements; Convert ...

Error in Next.js: The element type is not valid. Please make sure it is either a string (for built-in components) or a class/function (for composite components) and not undefined

I've encountered an issue while working on my Next.js project where I am trying to import the Layout component into my _app.js file. The error message I received is as follows: Error: Element type is invalid: expected a string (for built-in componen ...

Tips for splitting a container of specific height into sections measuring 80% and 20%

I am working on a container with a fixed position that I want to split into two halves: 80% and 20% at the bottom. This is what I want it to look like: Click here to see the image. Note: The layout should adjust itself when the window is resized. You c ...

Issue with Inline JS not functioning correctly in Flask when integrated with Bootstrap 5

I'm developing a Flask web app with Bootstrap 5 and attempting to incorporate inline JS, but it's not functioning as expected. Specifically, I'm trying to use a simple alert() component, but nothing is displaying on the page. Interestingly ...

What are the best ways to conceptualize the benefits of WebRTC?

I encountered a peculiar issue with the abstraction of the WebRTC offer generation process. It appears that the incoming ice candidates fail to reach the null candidate. While I have been able to generate offers successfully using similar code in the past, ...

performing an AJAX call utilizing both a file and post data

I'm reaching out for help because I'm having trouble sending an ajax request with a data file and two posts included Here is the code snippet: $('#image--1').change(function (e) { e.preventDefault(); var img = new FormData(); ...

Bringing PlayCanvas WebGL framework into a React environment

I am currently working on integrating the PlayCanvas webGL engine into React. PlayCanvas Github Since PlayCanvas operates on JS and the code remains in my index.html file, I am facing challenges in comprehending how it will interact with my React compone ...

Update current properties of objects

I'm feeling like I'm going crazy and could really use some assistance. My predicament involves a function that looks like this: private generateTimeObject(firstObject: someInterface, secondObject?: someInterface) { let firstTime; let secondTi ...

How can we enable a sitemap for web crawlers in a nodejs/express application?

Looking to enable sitemap for web crawlers in nodejs/express? I am trying to figure out where I should place my sitemap folder/files within the application flow and how to allow access for web crawlers. Currently, when visiting domain/sitemap/sitemap.xml, ...

What is the best way to create 2 select boxes that will disable each other if they match, and then automatically choose the second option so they do not match

Is there a way to have 2 select boxes automatically change one option to the second option on one box when they match? Instead of using an "alert", I am looking for a function that can automatically update one option in one of the select boxes if they mat ...

How can I dynamically update the URL parameter based on the selected option in a dropdown list?

There is a single select option box with three options. When a user selects an option, the corresponding value should be appended to the URL (e.g., value=needbyDate). If another option is selected later, the previous value in the URL should be replaced w ...

Switching the default z-index for child elements within an HTML container

According to the specification, elements are typically drawn "in tree order" for in-flow, non-positioned elements of similar block level or float status and identical z-index. This means that elements declared last in the HTML markup appear on top. But wha ...

What is the best way to alphabetically arrange an array of names in a JavaScript.map?

I am attempting to alphabetically sort an array of first names using JavaScript map function. Additionally, I aim to remove the "undefined" row from the final results: function contacts_callback(obj) { var contactinfo = obj.contacts .filter( ...

Observable - transforming two promises into an observable stream

I am facing a common scenario where I am looking to chain two promises together in such a way that if the first promise fails, the second promise needs to be canceled. In the world of 'Promises', the code would look something like this: Fn1.doPr ...