The use of <% ... %> is restricted in a client-side button click event

I need to create a function that redirects to a different page depending on the value in the query string. This is for an asp.net web form page, and the function should be triggered when a cancel button (a devexpress button) is clicked.

function OnCancelClick(s, e) {
    if (confirm('Leaving this page will require you to reselect benefits. Are you sure you want to leave?')) {
        var callingPage = document.getElementById("<%= CallingPage.ClientID %>").value;
        alert("Calling Page: " + callingPage);
        if (callingPage == "AddEmployee.aspx") {
            window.location.href = ResolveUrl('~/Member/Maintenance/AddEmployee.aspx?from=VerifyPage');
        } else if (callingPage == "AddDependentMember.aspx") {

        }
    }

The ID of the hidden field in which I store the calling page information is called CallingPage. I set its value during page load, but I am encountering an error even before the page has finished loading, saying "The Controls collection cannot be modified because the control contains code blocks."

Answer №1

Set the CallingPage.ClientID to a server-side variable that is publicly accessible:

public partial class <class / page name> : System.Web.UI.Page
{
    public string currentPage = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        this.currentPage = CallingPage.ClientID;

        ...
    }
}

Next, in your JavaScript code:

var currentPage = document.getElementById("<%= this.currentPage %>").value;

Answer №2

This particular issue is specific to devexpress controls and presents a unique challenge. It seems that using <% %> within the js clientside click event of a devexpress button click event is not allowed. To work around this limitation, I utilized devexpress's hidden control and successfully resolved the issue. Thank you

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

Link numerous number-type inputs together

I'm looking to create a form with 6 input fields of type number. Each label will have specified min and max values. The challenge is that if I set one field to be between 0-50, for example in red, then the next field in yellow can't be from 49-70 ...

"Step-by-step guide on uploading a base64 PDF file to Amazon S3 using the dotnet SDK and PutObject

I am facing an issue while attempting to upload a base64 pdf file to S3 using the dotnet SDK. Even though the file gets uploaded successfully, it appears as a blank pdf on S3. Could there be something I am overlooking? I suspect that the problem might ste ...

Combining keys array and values array to create an object in JavaScript

I currently have the following arrays: var keys = [ "color", "size" ]; var values = [ "blue", "medium" ]; My goal is to transform them into an object like this: { color: "blue", size: "medium" } In Python, you can achieve this easily with dict(zip(keys ...

React Native Error: Uncaught JavaScript Exception - Invalid Syntax

Encountering a strange error when trying to launch my RN app on iOS 8: Unhandled JS Exception: SyntaxError No additional information provided. Has anyone else come across this issue? Interestingly, the application runs fine on iOs 9+. xCode version: 8 ...

Performing a cURL request with C#

I am attempting to execute the following curl command within my C# console application: curl -d "text=This is a block of text" \ http://api.repustate.com/v2/demokey/score.json I have reviewed the suggestions presented in this similar question ...

Is it possible to delete an element from both local storage and HTML by referencing its ID?

Experience a simple flashcard game where you enter a question and answer to create a new flash card stored as an object within the cards array. The newly created flash card is also displayed by appending a new element to the flash cards section on the webp ...

Can Google Maps be initialized asynchronously without the need for a global callback function?

Currently, I am working on developing a reusable drop-in Module that can load Google Maps asynchronously and return a promise. If you want to take a look at the code I have constructed for this using AngularJS, you can find it here. One issue I have enco ...

The ASP.NET form does not display the md-button in the appropriate size

I have an ASP.NET form where I am trying to insert a md-button, but the button seems to resize and does not fit the row as desired. I have searched for documentation on this issue, tried adjusting the layout of the button, placing it in a md-input-contain ...

Tips for implementing WebSockets in a NativeScript application

Is there anyone who can provide guidance on utilizing WebSockets in NativeScript? I am working with VueJS in NativeScript and attempting to retrieve data using ws. Any assistance on this matter would be greatly appreciated. Thank you ...

What is causing the child table (toggle-table) to duplicate every time a row in the parent table is clicked?

After creating a table containing GDP data for country states, I've noticed that when a user clicks on a state row, the child table displaying district GDP appears. However, there seems to be an issue with the child table as it keeps repeating. I&apos ...

Edit any member with solely non-null values

I have a pair of classes that do not inherit from each other (besides being Objects): class A { Member1 m1; Member2 m2; } class B { Member1 m3; Member2 m4; } In each class, every member is matched with a member of the same type in the opposite c ...

Error: Loop Program Encountered Unexpected Token Syntax

Every time I attempt to run this code, a syntax error (unexpected token) appears. As someone who is still learning JavaScript, I am struggling to identify the root cause of this issue. var x = 1; var example = function(){ for(var y = 0; y < ...

Using the `scrollIntoView()` method to continuously scroll through elements in Puppeteer

My goal is to extract a list of posts while the page is continuously loading. I am using scrollIntoView() for each element within a loop. Currently, my code looks like this temporarily. When the page loads, the result bounces out without any errors. for (l ...

Despite a successful request, the React Redux action was not dispatched

I'm currently working on adding authentication to my project. While the user registration part seems to be working, the actions are not being dispatched. Strangely, a similar action for fetching data is working fine and dispatching the actions. Here&a ...

Ways to extract data from a JSON object

When using my web application, the Web API responds with the following JSON object: [ { "templateID":1, "template":"{\r\n \"Body\": \"sample date hete hee. Name\"\r\n}" }, { "templateI ...

Modifying the display aspect of the webpage (.aspx) using the code-behind logic in the corresponding (.aspx.cs

I'm working on a 'classic' ASP.NET app (.NET 3.5) that has a standard runat="server" form with server-side controls and an 'Execute' asp:button. When the 'Execute' button is clicked, the code-behind function executeButton ...

What steps can I take to ensure a mySQL transaction is not executed simultaneously in a Node.js Express application?

I'm facing a challenge that seems quite simple, yet it's causing me a lot of frustration. I just need to ensure that a MySQL database transaction is mutually exclusive, meaning a second call should wait until the first one finishes. Despite tryin ...

bespoke ErrorControllerAttribute

I am working on a solution to display all errors on the same page without redirecting or changing the content. I want the error to be shown where it occurred, using something like a JavaScript alert. I thought about loading an error page as a PartialView ...

Is it possible to bind HTML within an AngularJS partial but encountering an inexplicable issue?

I'm facing an issue with my ng-repeat that involves using a partial HTML file to loop through and display items on a page. The variables within the partial are not displaying, but interestingly enough, the variable {{rule.name}} outside of the partial ...

What is the best way to extract data from a directory and store it in an array?

import * as fs from 'fs'; const filesDirectory: string = './src/200k'; function findUniqueValues() { let valuesArr : string[] = []; fs.readdir(filesDirectory, function(error, files){ files.forEach(function(file){ fs.read ...