Guide to displaying a quick message and then redirecting the page upon clicking a button in ASP.NET

Hello, I am encountering an issue with a submit button on my page. I want the user to be prompted with a message saying "Are you sure to submit that page really" when the submit button is clicked, and if they click yes, then it should redirect to another page. I have tried using the following code:

RegisterStartupScript("myAlert", "<script>alert('Are you sure about to submit the test?')</script>");
Response.Redirect("Result.aspx");

However, the page is being redirected without showing the prompt message to the user. How can I make this work properly? Using Asp.net c#.

Answer №1

If you want to accomplish this, try the following:

<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="javascript:return 
confirm('Are you sure you want to submit the test?');" OnClick="Button1_Click" />

Next, in the code behind...

protected void Button1_Click(object sender, EventArgs e)
{
    Response.Redirect("Result.aspx");
}

Answer №3

While I can't recall the precise syntax, you must capture the value that was clicked and use it to decide whether to return true or false. Returning true will result in the page being submitted.

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

Execute JavaScript code once the XMLHttpRequest has completed execution

I'm facing an issue where the JavaScript code is executing faster than the XMLHttpRequest. I am hesitant to resolve it using: setTimeout(function() {}, 100); Below is a snippet of my code: function change_country(id) { if (window.XMLHttpReques ...

Having trouble retrieving the desired data from the JSON file

My current code is not giving me the expected results while trying to access JSON values with jQuery. Any suggestions on how I can resolve this issue? // JSON response: [{ "private": "8044553.0" }, { "governmentdocs": "98952.0" }, { "officiald ...

The cookie's expiration time is being set to 1 hour, rather than the specified maxAge duration

My file contains a cookie that consistently sets to 1 hour as the default, even when I specify a maxAge of 12 seconds. res.cookie("my-cookies", req.body.id_token, {maxAge: 12000, httpOnly: false}); ...

Retrieve information from a LINQ query and convert it to JSON format

How can I retrieve data from LINQ in Json format? I tried this code snippet but it doesn't work public ActionResult GenerateShop() { LinqDataContext context = new LinqDataContext(); IEnumerable<shops> shops = context.s ...

Update and change the property based on a condition in Ramda without the need for a lens

I am looking to modify properties for an object in ramda.js without using lenses. Given the data provided, I need to: If objects in array properties in a and b do not have the property "animationTimingFunction", then add the property key "easing" with a ...

Anonymous self-executing functions with parameters from an external scope

Recently, I stumbled upon the code snippet below while following a tutorial. const increment = (function(){ return function incrementbytwo (number){ return number+2; } })(); console.log(increment(1)); The result of the code above is 3. ...

JavaScript on Ruby on Rails stops functioning in js.erb file

I have encountered an issue with pagination using AJAX in a view. Initially, I had two paginations working perfectly fine with their respective AJAX calls. However, when I tried to add a third pagination (following the same method as the previous two), it ...

What is the proper way to link an AJAX response image stream to the image control?

I have successfully implemented an image control on my ASPX page with the following code: <div> <div class="ajaxdata"> <asp:Image ID="image2" runat="server"></asp:Image> </div> <a class="ajaxcall">cl ...

Utilizing jQuery event delegation with the use of .on and the unique reference of (

Questioning the reference in a delegated .on event: Example: $('#foo').on('click', $('.bar'), function() { console.log(this); }); In this scenario, `this` will point to #foo. How can I access the specific bar element tha ...

Retrieving live comments from YouTube streams for a customized React application

Currently working on developing a React Webapp to organize and showcase superchats during a live stream. My initial attempt involved utilizing the YouTube LiveChat API, but I hit a roadblock as it requires authentication from the live stream owner, which ...

Is there a better approach to accomplishing this task using jQuery?

http://jsfiddle.net/bGDME/ My goal is to display only the selected content within the scope and hide the rest. The method I used feels a bit cumbersome. I'm open to suggestions on how to improve this. Any guidance would be greatly appreciated. Tha ...

Utilizing ng-bind-html alongside ng-controller

Injecting insecure html into a <div> is part of my current task: <div class="category-wrapper" ng-bind-html="content"></div> The angularjs "code" in this html snippet ($scope.content) includes the following: <script type='text/ ...

Defining Exact Beginning and Ending Points for Individual Elements in a C# String Array

I am trying to display the names of all files in a specific folder in a listbox. However, the listbox is showing the full file path along with the file name. I have attempted multiple approaches to remove the unnecessary parts of the file path such as "C: ...

jQuery AJAX File Upload problem in Internet Explorer

Encountering an issue with Internet Explorer While this script with AJAX and jQuery functions perfectly in other browsers, it fails to do so in IE index.html <form enctype="multipart/form-data" method="post"> <input name="file" type="file" ...

Convert numerical values to currency format

Here is a number 5850 that I would like to format as currency. Format Example 1: 5850 => $58.50 Format Example 2: 9280 => $92.80 I am utilizing the function below: Number.prototype.formatMoney = function(decPlaces, thouSeparator, decSeparat ...

Issue with Vue Multiselect auto-suggestion functionality

I've been utilizing the [vue-multiselect] library for my project. [1]: https://www.npmjs.com/package/vue-multiselect. Within a form, I have multiple multiselect dropdowns. The issue I'm facing is with the browser's autocomplete feature. I&a ...

What is the process for defining a UML class diagram scanner to scan declarations?

Can anyone advise on the correct way to declare a Scanner in a UML class diagram for Java? Should it be represented as +scan: Scanner, or +scan: Scanner(System.in), or is there another preferred format? Any other necessary additions I should include? Than ...

Hold off until the script is successfully downloaded and executed, then patiently wait for the DOM to finish loading

I'm facing a challenge with running Javascript conditionally without it being in requirejs form. The script is located on the same server/domain as the requesting page, where an ajax call needs to be made. Is there a foolproof way to ensure that an a ...

What sets apart assigning a React ref using a callback versus setting it directly?

Is there a practical difference between directly setting a ref and setting it via a callback with the element as an argument? The functionality remains the same, but I am curious about any potential distinctions. Consider this react hook component: const ...

Developing a custom Tag Helper for HtmlHelper.Raw

Trying to incorporate a Tag Helper class into my asp.net core project that outputs the raw content. Below is my implementation: public class RawTagHelper : TagHelper { public RawTagHelper(IHtmlHelper _) { HtmlHelper = _; } privat ...