Having difficulty executing JavaScript code from the VB code behind

Recently, I have encountered a strange issue while working with ASP/VB.net. In my code behind VB file, I am trying to call a JavaScript function, but it seems like nothing is happening. Surprisingly, I have used a very similar method on several other pages successfully, but for some reason, it fails to work on this particular page. My main goal is to prompt the user with a yes/no type question. The response is then saved in a hidden field named confirm_value, after which the JS script should click a button again if the user answers "yes" or chooses to continue.

<asp:HiddenField  runat="server" id ="confirm_value"/>

Unfortunately, the JavaScript function never seems to execute. I even attempted a basic alert message saying "Hi There", although that didn't trigger either. What could be causing the JS code not to run?

VB Code:

If -1000 < RadNumericTextBox1.Text Then
        If confirm_value.Value <> "Yes" Then
            Dim cs As ClientScriptManager = Page.ClientScript
            ' Check to see if the startup script is already registered.
            If (Not cs.IsStartupScriptRegistered(Me.GetType(), "ConfirmScriptRXOrder")) Then
                Dim cstext1 As String = "Confirm('This item has less in stock than your order. Do you want to order it anyway?');"
                'Dim cstext1 As String = "alert('Hi there!');"
                cs.RegisterStartupScript(Me.GetType(), "ConfirmScriptRXOrder", cstext1, True)
            End If
            'return here, JS will re-click submit button after okaying the message,.
            Return
        Else
            confirm_value.Value = "No"
            Mycart(ddCyl.SelectedValue.ToString, ddSph.SelectedItem.Text, ddCyl.SelectedItem.Text, RadNumericTextBox1.Text, tbtray.Text)

            RadGrid1.DataBind()
        End If
    End If

JS Code:

<script type = "text/javascript">
    function Confirm(message) {
        if (confirm(message)) {
            var x = document.getElementById('<%= confirm_value.ClientID%>');
            x.value = 'Yes';
            document.getElementById('<%=btnOrder.ClientID%>').click();
        } else {
            var x = document.getElementById('<%= confirm_value.ClientID%>');
            x.value = 'No';
        }
    }
</script>

Answer №1

It may seem strange to provide the solution to my own question, but the answer was not immediately apparent from my initial post. Surprisingly, it had nothing to do with the code I shared. I decided to simplify the page by keeping only the submit button, JS, and VB call. Much to my surprise, it worked! Slowly reintroducing code revealed that the issue arose because the button was nested within an asp:updatepanel. Moving the button outside of the update panel resolved the problem, even though it compromised the visual balance. Therefore, if you encounter a similar situation, refer back to this post for guidance!

Answer №2

While I'm not a Java guru...

It seems like the name of your java function should be "ConfirmScriptRXOrder" instead of just "Confirm".

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

Sequelize querying using the `WHERE NOT EXISTS()` condition

I am currently working with a many-to-many relationship setup in my database: var Genres = db.define('Movie', { name: { type: Sequelize.STRING(100), allowNull: false }, description: { type:Sequelize.STRING(), ...

Express req.files returns null when using jQuery FormData

For client side functionality, I am utilizing Bootstrap and jQuery. On the server side, my technology stack includes node.js and express. The form structure is displayed below: <form id="signupform" action=""> <h2&g ...

Trouble with linkage in jQuery for AJAX requests in an external .js document

My asp.net application has a master file that includes the jquery.js file. On another page that uses this master page, I have my own jquery code. I attempted to move this code to an external file and then include it in my .aspx file. While most functions ...

Error: Unable to access the 'location' property because it is undefined

Having trouble uploading a product along with an image using AWS S3 (Multer and MulterS3). Every time I try it in Postman, I get the error message "TypeError: Cannot read property 'location' of undefined" specifically pointing to the line where t ...

Assigning a Node.js exported function to a variable

Is there a way to save an exported function as a variable without executing it right away, in order to use Promise.all? I noticed that when I assign the function to a variable, it automatically runs. How can I prevent this from happening during assignment? ...

Retrieve all items on the webpage using the pattern "_ followed by 10 random characters" through JavaScript

On my webpage, there is a table containing table rows in the following format: <tr id="_C15DKNWCSV">text</tr> I am attempting to scan the webpage and extract all table rows that have an id in the format: id="_(10 RANDOM CHARACTERS)" I want ...

Leveraging jQuery to extract a key-value pair and assign it to a new property by

I'm struggling with extracting a value from a key-value pair and inserting it into the rel property of an anchor tag. Even though I try to split the code and place the value correctly, it doesn't seem to work as expected. Instead of applying the ...

Having trouble with npm install, unable to successfully install any node modules after cloning my project from git

I recently pulled my project from a git repository and encountered issues while attempting to run npm install. Despite trying different solutions like running npm install --save core-js@^3 to address the core-js error, I keep receiving the same message pr ...

What is the best way to download a modified canvas with filters using the solutions from stackoverflow?

There have been numerous inquiries regarding how to apply CSS filters to an image, with some solutions that don't utilize CSS filters but still achieve the desired outcome. However, for someone new to JavaScript like myself, these answers can be confu ...

Saving the AJAX response object in a global variable - Important fields are not being retrieved

Currently, I am developing an asynchronous webpage in Grails wherein I invoke a controller and display the response as a D3.js network. To ensure further usability, I saved the object as a global variable. Despite the successful execution of the function u ...

Unable to display response following the submission of a POST request

I'm encountering an issue when attempting to display the response of a post request using node and request. While I can see the response in the console within the service, it does not make its way to the controller. Any insights on why this may be hap ...

Sending messages on Discord.js at one-minute intervals

Hey there, I'm currently facing an issue while trying to automate a message sending process on Discord. The specific error that keeps popping up is: bot.sendMessage is not a function I'm puzzled as to why this error occurs, so I've include ...

How to eliminate looping animations with jQuery

While looping through items, I am encountering an issue where an animation triggers when the loop returns to the first div. I simply need a way to switch between divs without any animations on a set interval. $(document).ready(function () { function s ...

Hide preloader in AngularJS once repeater has completed execution

Is there a way to hide a preloader div only after all data has finished loading in an ng-repeat loop? Check out this interactive example on Plunker: http://plnkr.co/edit/ilgOZzIy2axSi5Iy85C7?p=preview Here is the HTML code: <div ng-co ...

Differentiate input elements with custom styling

I'm experiencing an issue while trying to style a specific form field in Angular 7. The style doesn't seem to be applying properly. Below is my form structure: <ul> <li> <mat-form-field *ngIf="number"> <input ma ...

Switching the keyboard language on the client side of programming languages

I'm interested in altering the keyboard language when an input element changes. Is it possible to modify the keyboard language using client-side programming languages? And specifically, can JavaScript be used to change the keyboard language? ...

Can AdonisJS 4.1.0 support conditional queries?

I am exploring the capabilities of AdonisJs query builder by referring to their documentation at Currently, I am attempting to replicate a scenario similar to the one demonstrated under the 'Conditional Queries' section: const query = Database. ...

Is there a way to fetch and display property changes in Vue Material's md-dialog?

In the component hierarchy, there is a parent component containing a child, which further contains an md-dialog component. The dialog is opened from the parent component using the following code: ParentComponent.vue <template> <div> < ...

react componentdidupdate triggers never-ending iteration

When I trigger an API call to elasticsearch using onChange, it prompts a list for autocomplete. To make sure that my store is updated before rerendering, I included componentDidMount so that I am not lagging behind by one tick. Here is the code snippet: c ...

Tips for safely executing an SQL query with electron.js

I have a new project where I need to interact with an SQL database on the local network, but it's not located on the same system I'm working on (not SQLExpress). So far, I've figured out how to collect user input on a webpage and send that ...