Having trouble with accessing controls within an UpdatePanel?

I am attempting to access Page Controls at Page_Load, execute a database query, and modify the visibility of controls.

Here is the Code:

foreach (Control thiscontrol in ContentPlaceHolderBody.Controls) {
    try {
        if (thiscontrol.ID.Contains("TextBox") || thiscontrol.ID.Contains("Label")) {
            string temp = thiscontrol.ID;
            bool IsValid = db.Roles.Any(a => a.controlName == temp);
            if (!IsValid)
                thiscontrol.Visible = false;
        }
        else if (thiscontrol.ID.Contains("UpdatePanel")) {
            foreach (Control UPcontrols in ((UpdatePanel)thiscontrol).ContentTemplateContainer.Controls) {
                if (UPcontrols.ID.Contains("TextBox") || UPcontrols.ID.Contains("DropDownList")) {
                    bool UPIsValid = db.Roles.Any(a => a.controlName == UPcontrols.ID);
                    if (!UPIsValid)
                        UPcontrols.Visible = false;
                }
            }
        }
    }
    catch { }
}

The main issue lies with the UPcontrols! It should be retrieving the controls within the UpdatePanel, but for some reason it only works in debug mode!

When I set a breakpoint, everything functions correctly, however, when I run the web application, it fails to locate any components within the UpdatePanel...

Answer №1

Test out this code snippet:

ControlCollection updatePanelControls = updatepanel1.Controls;
ControlCollection contentTemplateControls = updatePanelControls[0].Controls;

initializeControls(contentTemplateControls);

public void initializeControls(ControlCollection controls)
{

    foreach (Control control in controls) {
        if (control is TextBox)
            ((TextBox)control).Text = "";


        if (control is Panel) {
            ControlCollection panelControls = control.Controls;

            foreach (Control panelControl in panelControls) {
                if (panelControl is TextBox)
                    ((TextBox)panelControl).Text = "";
            }
        }
    }
}

First, locate controls within the updatepanel and then find controls within the contentTemplate containing all the controls.

Answer №2

This design appears to be quite eccentric. It's rather unconventional to use control IDs for this type of functionality.

However, in order to successfully navigate through all controls on the page, a recursive method is necessary. The current method may not function properly if the UpdatePanel is nested within another control.

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

Illuminate XHR 500 (Server Side Issue)

I'm currently troubleshooting an issue with this AJAX script, but I'm unable to identify the specific problem. The Chrome developer tools are indicating a "500 (Internal Server Error), doWork @ (index):16 , onclick @ (index):28" Is there anyone ...

Encountering a Laravel Axios error when using Vue: status code 419 issue

Issue: When users interact with my application by posting questions, I am encountering a problem. Upon clicking the Ask button to submit the question using Vue.js and Axios, about 70% of the time the submission is successful. However, approximately 30% of ...

Fill an object with data and send it using jQuery's ajax function

I'm facing an issue in my JSP page with multiple input tags having the same ID 'opt'. My goal is to extract the values of all these input tags and generate a list as shown below: Votes= { vote={opt:'one'}, vote={opt:'two&apos ...

Despite the onsubmit returning false, the submit operation still goes through

I seem to have hit a roadblock yet again. My registration form incorporates three JavaScript functions which display the desired output when triggered by an onchange event within my HTML form. These functions generate a Bootstrap alert while the user is co ...

I'm attempting to showcase an HTML file by using res.sendFile within the Express framework

As part of my workflow implementation, I have set up a scenario where upon user login, the credentials are sent via Ajax to an Express route for verification. If the user exists, the route responds with a message "authorised" triggering a second Ajax call ...

The function WebForm_DoCallback is not recognized

Encountering an error where WebForm_DoCallback is undefined. UPDATE WebForm_DoCallback("AccountPageControl1", "FileSave~" + fileName, CVFileSavedServerResponse, null, null, true); function CVFileSavedServerResponse(param, context) { } Why isn't ...

Having trouble with Ajax functionality on the portlet configuration JSP page in Liferay 6.2?

Currently developing a MVCPortlet (Liferay 6.2). Curious about why this ajax call functions as expected on a standard jsp within my portlet, but fails to work on the configuration page of the portlet (the jsp that appears when you click on the top right c ...

The issue persists as the Ajax request continues to return false

I have created a verification program using an ajax request: $("#codeSbmt").click(function(){ var $input = $("#fourDigit"); codeCheck("codeTest.php?q=" + $input); }) function codeCheck(url) { var xh ...

URL not passing on variable

Here is the code I have for a basic 'change email' script. I'm currently struggling to get it working and can't figure out what's wrong. <?php if (isset($_GET['u'])) { $u = $_GET['u']; } if (isset($_POS ...

Can you explain the contrast between uploading files with FileReader versus FormData?

When it comes to uploading files using Ajax (XHR2), there are two different approaches available. The first method involves reading the file content as an array buffer or a binary string and then streaming it using the XHR send method, like demonstrated he ...

Transferring data through Ajax, Jquery, and PHP: A seamless process

My goal is to develop an attendance button that adds +1 to the total number of people attending when clicked by the user. I am looking for a way to achieve this without having to refresh the page. Ideally, I would like to connect it to a database to upda ...

Execute AJAX function when loading a form populated from a MySQL/PHP database

I am currently working on a drop-down menu that is being populated from a MySQL table using PHP. The goal is to have a second drop-down menu triggered based on the selection made in the first form. This functionality works perfectly when a selection is mad ...

Sending Code Through POST Using jQuery

Users can input code in a textarea with the id of 'code_box'. <textarea id = 'code_box'></textarea> The following jQuery uses ajax to send the code to a PHP script for processing. $('#submit_button').click(funct ...

Is it possible for a PHP form to generate new values based on user input after being submitted?

After a user fills out and submits a form, their inputs are sent over using POST to a specified .php page. The question arises: can buttons or radio checks on the same page perform different operations on those inputs depending on which one is clicked? It ...

Deleting an element from HTML using jQuery

In the midst of creating a system that allows users to construct their own navigation structure, I have encountered a stumbling block. The idea is that when a user lands on the site, they are presented with a list of available topics from which they can ch ...

Script for tracking user activity on Facebook and Google platforms

I currently have Google tracking conversion set up, but now I also need to implement Facebook pixel tracking. My existing Google Head Script code is as follows: <script> (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||fu ...

Using AJAX and jQuery to refresh a specific div when a specific button is clicked

I have a function that uses AJAX to update votes when clicked. Here is the code: $(function() { $(".vote").click(function() { var id = $(this).attr("id"); var name = $(this).attr("name"); var dat ...

How to send an html form with php, store it in a MySQL Database, and utilize Ajax and jQuery for

As a beginner in PHP form creation, I have been exploring various tutorials and combining different techniques to create my form. However, I am facing challenges as none of the tutorials cover everything comprehensively from beginning to end. While I beli ...

Creating an AJAX request in Play 2.x by using a shared button

Although I have successfully utilized AJAX requests in the past using a form and adding return false to prevent page refresh, I recently encountered an issue when moving my JavaScript into a separate file. The @ commands now fail, making it difficult for m ...

Exploring the wonders of jQuery Ajax within Yii

I am developing a custom shop application and I have a query. In my project, the implementation of Ajax in Yii involves: <?php echo CHtml::ajaxLink( '', array("cart/add/id/$item->id"), array( ' ...