Invoke a Confirmation Dialogue from ASP.NET server... and receive the selection back on the server

Hello there! I am just starting out with web programming and I've hit a little bump in the road. Here's my current situation:

  1. The user lands on a page and clicks on the "Upload document" button.
  2. The document gets uploaded to the server, is processed, and the page displays the new information obtained from the file.
  3. The user then has the option to click on a "PERFORM" button which triggers a server method.
  4. Within the server method, there is an if statement. If it evaluates to true, everything is fine. However, if it's false, a confirmation dialog is presented to the user.
    • If the user selects NO, the process comes to a halt.
    • If the user chooses YES, a different server method is executed, and then we loop back to step 4.

I have successfully completed points 1, 2, and 3; however, I'm currently stuck on the 4th point...

I believe this difficulty stems from not having a full grasp on how servers and clients interact. While I understand that direct JavaScript to C# calls (and vice versa) are not possible, techniques like Ajax provide ways to achieve this... though I still find them a bit unclear at the moment.

Let me share a snippet of my code for reference:

UploadDoc.aspx

<script type="text/javascript" language="javascript">
    function ConfirmDialog() {
        if (confirm("You are new. Would you like to sign-in?") == true) {
            // call server method
        }
    }

</script>

...

<asp:Button ID="btnUpload" runat="server" Text="UPLOAD DOC" OnClick="btnUpload_Click" />

UploadDoc.aspx.cs

// ...

private bool btnUpload_Click(List<MyStuff> myList)
{
    List<MyStuff> vList = new List<MyStuff>();
    bool up = Upload(vList);
    // ...
}

private bool Upload(List<MyStuff> myList)
{
    bool registered;

    // ... other stuffs...

    if (registered == true)
    {
        // do things...
    }
    else
    {
        // At this point, the confirm dialog should be displayed.
        System.Web.UI.ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "ConfirmDialog()", true);
    }
}

Answer №1

Just to reiterate what you may already be aware of: C# code operates on the server, while Javascript functions run on the client.
Nevertheless, it is possible to call Javascript functions from the server side by executing them during page rendering on the client side.

I suggest delving into the details of the ScriptManager class as it can assist you in achieving this objective.

To be more specific, consider adding something like this within the else block of your Upload function:

ScriptManager.RegisterStartupScript(this, this.GetType(), "confirmDialog", "ConfirmDialog();", true);

As for triggering a postback from the client side to the server, refer to the doPostback method.

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

What is the best way to configure ConfigurationManager to retrieve application settings from various files?

In my development work, I have encountered a situation where I need to interact with a third-party application using an API provided in a DLL. The developer of this application made the decision some time ago to incorporate their own .NET components into t ...

Recognizing Components in Alert using selenium webdriver in C#

I'm facing a challenge on this webpage where there is an alert that contains elements requiring interaction beyond the usual Accept() or Dismiss(). For instance, a datagrid, another button. Any ideas on how to tackle this? ...

Generate Material UI sidebar components that are positioned above all other components

I am currently working on a sidebar component using the Material UI Drawer component. I am looking to add components that align side by side with the sidebar, similar to the green box shown in the image below. https://i.sstatic.net/aAtn6.png After attem ...

Finding the location of a file within a published web component

I am currently working on a webcomponent where I need to include a link tag in the head section and set the href attribute to a folder within a node module. At this stage, during the development of my component, my project structure looks like this: http ...

Updating information within AngularJS select boxes

On my page, I have 3 select boxes. When a user selects an option in the first select box, I want the options in the second select box to update based on the value selected in the first one. Similarly, I want the options in the third select box to change w ...

How to use Jquery to set the selected item in a dropdown menu

I'm facing an issue with my drop-down menu - whenever I select an option, the page redirects to a different URL. What I want is for the selected item in the drop-down menu to become the new page. Below is the code I have: HTML: <select class="Mob ...

Where have I gone astray? (Basic Announcer Bot on Discord)

Just a heads up, I haven't really delved into colds much before, just once or twice previously. The starting Bot I downloaded is from this link, but whenever I try to run it in the command prompt, I encounter this issue: C:\Users\aer\D ...

Creating a React Mui TextField specifically for currency input on iPhone Safari: ensuring that only digits are allowed

I have implemented a Mui TextField component for currency input that displays only the numeric keyboard in the Safari browser. However, I want to restrict users from pasting literal strings into the field and ensure that only currency number inputs are al ...

How can I reset the search input in v-autocomplete (multiple) after selecting a checkbox from the drop-down menu?

I am facing an issue with the v-autocomplete component from Vuetify. Currently, when I enter "Cali" in the search input and select "California" from the dropdown list, the "Cali" value remains in the search input field. I need the entered value to be clear ...

I am looking to showcase images beside individuals' names or photos on my website in a vertical arrangement, similar to how it is done on Facebook

Looking for suggestions on how to display images uploaded by users on my webpage in a way similar to Facebook, with the user's photo displayed beside each image. Any recommendations or website links would be greatly appreciated. Thanks, Santosh Sahu ...

Element on webpage "Expands" When Scrolled into Visibility

After posting a question on Stack Overflow, I noticed discrepancies between the element dimensions reported by Chrome Inspector and Selenium WebDriver. While Chrome Inspector showed w = 979, h = 1961, Selenium returned dimensions of 979 and 1461 respective ...

Using the class method to handle jQuery events alters the context

Is it possible to access the class context from within a method being used as a jQuery event handler? The example below illustrates the scenario: class EventHandler { constructor() { this.msg = 'I am the event handler'; } ...

I am facing an issue where my react app is only rendering in one file and not when imported into another

I've been diving into learning React and experimenting with building a simple app using the useState and useEffect hooks. However, I encountered an issue when exporting and importing my component from index.js to App.js file, and running ReactDOM.rend ...

The array filtering functionality is not functioning as intended

Struggling with correctly filtering data in JavaScript. Here's an example where var1 = 20, var2 = 10, var3 = 30, var4 = 40. This is the code snippet: var variables = ['var1', 'var2', 'var3', 'var4'], values = [ ...

What's causing this MUI React data grid component to be rendered multiple times?

I have developed a wrapper for the MUI Data Grid Component as portrayed: Selection.tsx: import * as React from 'react'; import { DataGrid, faIR, GridSelectionModel } from '@mui/x-data-grid'; import type {} from '@mui/x-data-grid/t ...

Puppeteer - Struggling with setting cookies? Getting an error that says "is missing the following properties from type 'CookieParam': name, value"?

Here is my cookie variable: const cookies = [{ domain: ".example.io", expirationDate: 1234567890, hostOnly: true, httpOnly: true, name: "cookie_name", path: "/", sameSite: "strict", se ...

Retrieving the information from a ContentPlaceHolder using C#

I have a Master Page with several ContentPlaceHolders, and occasionally one of them may not have any content. When both ContentPlaceHolders do have content, I would like to create a visible buffer between them for spacing. However, this buffer should remai ...

Refreshing the Dropdown Menu with Jquery

Looking for a way to create a reusable dropdown menu using css/jquery? Check out this codepen: https://codepen.io/anon/pen/NxXXPg Is there a method to reset the 'active' status when clicking on a blank space or another html element? Here's ...

Troubleshooting: Why is jQuery Autocomplete failing to function with JSON PHP to JavaScript integration?

I need to implement an AutoComplete feature with a list of countries from a SQL database. 1. Using PHP: $sql_list_countries=(SQL request) var_dump: array (size=2) 0 => object(stdClass)[3] public 'meta_value' => string &apos ...

Using TypeScript, invoking a function inside a callback

Here is the link: https://github.com/Uepaa-AG/p2pkit-cordova I am facing a challenge in calling onEnabled function while working with TypeScript. The code example works perfectly without TypeScript. However, with TypeScript, I am struggling to properly c ...