Verification message appears once the SQL database has provided the necessary information to the data table

 function alertbox() {
    var mode = document.getElementById('<%= hdnMode.ClientID %>').value;
    if (mode == "EDIT")
        return false;

    if (confirm("Is the same data present against that ID?") == true) {
        document.getElementById('<%= hdnYesNo.ClientID %>').value = "COPY";
    }
    else {
        document.getElementById('<%= hdnYesNo.ClientID %>').value = "CANCEL";
    }
 }

The confirmation message above should appear after retrieving data from SQL.

Page.ClientScript.RegisterStartupScript(this.GetType(),"CallMyFunction",
"MyFunction()",true);

How to use it from code-behind and how to retrieve the return value based on 'COPY' or 'CANCEL'.

Answer №1

Here is an example I recently created as part of a testing application.

To begin, I utilized the ScriptManager to incorporate specific javascript files for the webpage:

<body>
    <form id="form1" runat="server">
    <div>        
        <asp:ScriptManager ID="ScriptManager1" runat="server">
            <Scripts>
                <asp:ScriptReference Path="~/JS/tester.js" />
            </Scripts>
        </asp:ScriptManager>                
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" 
                    Text="Call database" />                
    </div>
    </form>
</body>

You can add more JS files inside the <Scripts> tag to ensure they are loaded with the webpage.

The content of my tester.js file includes:

function alertbox(data) {
    alert("Completed the database operation with following data = " + data);
}

In the code-behind, I have set up a button on the webpage that performs database operations and alerts the user once completed:

protected void Button1_Click(object sender, EventArgs e)
{
     Thread.Sleep(2000);
     int sqlReturnValue = ExecuteTheQuery();
     ScriptManager.RegisterStartupScript(this, typeof(string), "Database Completed", "alertbox(" + sqlReturnValue  + ");", true);
}

This will trigger the Javascript function alertbox.

(Please note, this example demonstrates achieving a specific task)

Update:

A similar outcome can also be achieved using ClientScript. Add a script tag in the head section:

<head runat="server">
    <title>Test Page</title>
    <script src="JS/tester.js" type="text/javascript"></script>
</head>

In the button click code-behind:

protected void Button1_Click(object sender, EventArgs e)
{
    Thread.Sleep(2000);
    ClientScript.RegisterStartupScript(this.GetType(), "Database Completed", "alertbox(23);", true);
}

For further understanding of ClientScript and ScriptManager, refer to this question.

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

Page Not Found: Troubleshooting Express Server Issue

Currently, I am working on creating a basic parser using Node/Express and Cheerio. However, even though the server is running smoothly, I am unable to load any pages in my browser. Below is the code snippet from server.js: var express = require('expr ...

What is the method for retrieving hotels from a database based on their proximity to a specific set of latitude and longitude coordinates?

I have a database table with latitude, longitude, and hotel locations. I want to create a feature that will show hotels near a specific point defined by latitude and longitude. Code Snippet function findNearbyHotels() { $this->lo ...

Can SessionId be retrieved in IHttpContext.LogRequest while using an IHttpModule?

When considering logging requests using a custom IHttpModule, the IHttpContext.LogRequest event may appear to be an obvious choice. However, I am encountering a challenge in that I wish to include the session id in the log, which is unavailable for most re ...

Leveraging Facebook Pixel within a DNN-powered website

Is there a way to integrate the Facebook pixel into a website created using the DNN platform? ...

The withRouter function in React Router does not automatically inject the router

Desiring to implement withRouter on my primary React component named 'App'. You can view the documentation here. This is how I utilize it: import React from "react"; import { render } from "react-dom"; import {Router, Link, hashHistory, Rout ...

Comparison of WWSAPI and C#/.net <serviceCertificate> configurations in App.config

We are developing a C++/WWSAPI client to establish connectivity with a third-party C#/.net server. A C#/.net client sample is available which successfully communicates with the server through the following configuration in its App.config (client.exe.config ...

Use Javascript to conceal a div element if there are no links present

I am working on a website using ModX CMS and I am attempting to hide or remove a div element when it does not contain any anchor tags. How can I achieve this? I have already attempted the following code without success: jQuery(function($) { if ($(".pages ...

Overlap one element entirely with another

Currently, I am working on a way for an element called overlayElement to completely cover another element known as originalElement. The overlayElement is an iFrame, but that detail may not be significant in this scenario. My goal is to ensure that the over ...

The List containing instances of a <custom class> appears to be reverting back to its original state

Issue: In the code snippet below, there seems to be a problem with the way the CalcBalance() method is removing tblAccount objects from the Me.Accounts property. After each loop, the removed object reappears in the property, causing unexpected behavior. ...

Error: Attempting to access 'toString' property on an undefined value - redis

const updateLeaderboards = async () => { try { const users = await User.findAll(); const leaderboardData = []; users.forEach((user) => { // Add user data to the leaderboardData array in the required format leaderboardData.p ...

Vue.js encounters a null value for "$slots.default()[0].el" when dynamically passing a slot using v-for

Is there a way to access the HTMLElement of a dynamically passed slot when using v-for? I'm having trouble as the element (el) always seems to be empty. app.vue : <template> <div id="app"> <prod> <span> ...

Is there a way to use regular expressions in C# to verify specific conditions?

I am dealing with a situation like this If I input the text in the following format: 9 - Only numbers should be allowed s - Only special characters should be allowed a - Only alphabets should be allowed x - Alphanumeric characters should be allowed ...

What is the method to create a polygon in its entirety by utilizing a for loop within Javascript?

After successfully using the Canvas of HTML to draw a convex polygon, I encountered an issue. In the provided code snippet, t2 represents an array of points with getX() and getY() methods. The drawing function performs as expected until it reaches the seg ...

The old DLL is causing a reference error when called upon

While working with a TwitchLib reference and utilizing Newtonsoft.Json 7.0.0, I encountered an error every time I visited the page that calls Twitch lab. The error message displayed is: "Could not load file or assembly 'Newtonsoft.Json, Version=7.0.0 ...

Is it possible to create a custom options array in React-Select?

I've been working with react-select using the package from . The required format for the options prop is {value:something, label:something}. I have a list of objects with additional key-value pairs and I'm wondering if there's a way to avoi ...

Connecting to a remote MySQL database using ASP.NET

What is the best way to establish a connection between asp.net intranet pages and a remote mysql database? I have full access to both servers. Typically, I link my asp.net intranet pages to a sql server within our internal company network. Connecting to a ...

React app experiencing inconsistent loading of Google Translate script

In my React application, I have integrated the Google Translate script to enable translation functionality. However, I am facing an issue where the translator dropdown does not consistently appear on every page visit. While it sometimes loads properly, oth ...

The value in the textarea will stay unchanged even after the form has been submitted

I recently resolved my previous issue, but now I am seeking advice on how to prevent a textarea from clearing its input after submitting a form. You can view the jsFiddle here: http://jsfiddle.net/rz4pnumy/ Should I modify the HTML form? <form id="for ...

Tips for streaming a partially generated video file in a web browser

My current issue involves a program that generates a video in a specific file format, and I want to display this video file in popular browsers like Firefox, Chrome, or any other browser using an HTML5 tag. The problem arises when the video file is only 5 ...

Updating the value of an Angular select on change is not being reflected

If the select element is changed, the value should be set to something different from what was selected. There's a feature in place that only allows 4 item types to be selected at a time; if more than 4 are chosen, the value reverts back to its origin ...