How to display server-side variables in an alert using JavaScript in ASP.NET

I need to display a server-side variable in a JavaScript alert.

 <asp:Button id="btnDelete" runat="server" class="deleteicon" Text='<%# Eval("iuser_id") %>' OnClick="deleteclick" onclientclick="return confirm('Are you sure you want to delete this record?'+<%# Eval("struser_name") %>);"/>

This button is used for deleting records within a gridview. When clicked, I want to show an alert with user confirmation and the name of the user being deleted. However, I am encountering a server tag formatting error.

Appreciate any assistance,

Answer №1

REVISED

To incorporate a JavaScript click function in the rowDataBound event of the gridview, follow this safest and most straightforward approach. The code is outlined below.

protected void GridView1_RowDataBond(object source, GridViewRowEventArgs e) 
    { 
        if (e.Row.RowType == DataControlRowType.DataRow) 
        { 
            Button btnAlertStatus = (Button)e.Row.FindControl("btnAlertStatus"); 

            DataRowView drv = (DataRowView)e.Row.DataItem; 

            string username = drv["User_name"].ToString(); 

            btnAlertStatus.Attributes.Add("onclick", "return confirm('Are you sure you want to delete this record?"+ username + "')"); //error due to missing bracket in JavaScript
        } 
    }

UPDATED

Consider implementing the following JavaScript function:

function confirmMsg()
{

  var Variable = '<%= ServerVaraible %>';
  return confirm('Are you sure you want to delete this record?'+ Variable );
}

Include the following HTML element:

 <asp:Button id="btnDelete" runat="server" class="deleteicon" Text='<%# Eval("iuser_id") %>' OnClick="deleteclick" onclientclick="return confirmMsg();"/>

Answer №2

 <asp:Button id="btnDelete" runat="server" class="deleteicon" Text='<%# Eval("userid") %>' OnClick="deleteEntry" onclientclick='return confirm("Are you certain you wish to remove this item? <%# Eval("username") %>");'/>

Update: Try using different types of quotations

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

Transmitting Several Pictures at Once Through WhatsApp-WebJS

I have encountered a challenge while attempting to send multiple images in a single WhatsApp message using the WhatsApp-WebJS library. Despite receiving a "success" confirmation without any errors, the images and message fail to appear on WhatsAp ...

Error message: The function send() cannot be applied to the object received by request.post() in Node

As I embark on testing the functionalities of my node.js website using chai and mocha, I encountered an issue when running npm test. The error message displayed is: ' TypeError: request.post(...).send is not a function' Referencing the code sni ...

Struggling to make the upvoting feature function properly in the Thinkster MEAN Stack Tutorial

While following the MEAN Stack tutorial on this website, I decided to modify my code to utilize Controller as instead of using $scope as demonstrated in their examples. I am encountering an issue with the upvote functionality. Whenever I click to upvote a ...

Tips for automatically closing all other divs when one is opened

I have multiple divs structured like this: <div id="income"> <h5 onclick="toggle_visibility('incometoggle');">INCOME</h5> <div id="incometoggle"> <h6>Income Total</h6> </div> </div> <d ...

Activate $digest when a variable is modified by a node.js function

I am currently working on developing an application using node-webkit and AngularJS. Everything was going smoothly until I tried to make Angular watch over a "legacy variable" using the method outlined in this answer, which led to an Error: [$rootScope:inp ...

The React.js .map function encountered an error while trying to map the results from Firebase

As a newcomer to the realm of React and Firebase, I find myself struggling with arrays and objects. It seems like the way my data is formatted or typed does not play well with the .map method. Despite scouring Stack Overflow for answers, none of the soluti ...

The initial request does not include the cookie

My server.js file in the express application has the following code: var express = require('express'); var fallback = require('express-history-api-fallback'); var compress = require('compression'); var favicon = require(&apos ...

Discovering the solution to populating and building a tree structure using jsTree in conjunction with SQL Server, addressing the challenges associated with the

My current challenge involves using JSTREE to display a list of system modules. The issue arises from the fact that, according to the jsTree documentation, I need to use # in my query to create the tree structure. However, when I execute the following quer ...

Having trouble with setting up ENV variables while deploying the app on Heroku

I recently deployed my node.js app to Heroku, and I'm facing some issues with its functionality. The app loads fine, but when I try to sign in, it breaks down. It seems like the environment variables are not loading correctly, specifically the db vari ...

Issue when Updating Component State: React child cannot be an object

I'm currently in the process of familiarizing myself with React and how to manage component state. My goal is to build a component, set up the initial state using a constructor, make a GET request to the server to fetch an array of objects, and then ...

Overcoming validation hurdles with PHP and JavaScript

Our form pages have a majority of required fields, which are verified using JavaScript before being sent to a backend PHP app. Here is the form tag we utilize: <form name="order" method="post" action="http://company.com/config/_process.php?" id="order ...

docker throwing error due to missing webpack configuration file

I've encountered an issue while trying to run my web app using webpack and docker. It works fine when I run "npm start" but not when I run it with docker. Below are the errors that I am facing: > webpack-dev-server --open --progress --config webpa ...

Tips on causing a forEach loop to pause for a regex substitution to occur

I have a project in progress for an email web app where I am working on replacing certain keywords (first name, last name, email) with the actual properties of the user. As of now, I am going through a list of recipients and modifying the email content to ...

Refresh the content of a div showcasing live Twitter updates automatically

Currently, I am experimenting with the Twitter APIv1.1 and am working on implementing a box that will showcase my most recent tweets. You can view the progress here: However, my goal is to have this box update automatically whenever I tweet. As a beginner ...

Prevent the div from moving beyond the boundaries of its container while anim

After experimenting with a javascript image panner that I put together from various code snippets, I have decided to switch to a simpler approach. However, I need some guidance on a few things. Below is the code for the left and right buttons: <div id ...

GridView's concealed field data

What is the method to retrieve a hidden column's value in a GridView in asp.net? ...

Guide on eliminating lines that start with a particular character

I need help figuring out how to remove all lines of code that start with #include. Here's an example: #include 'utils/namespace.js' #include 'utils/constants.js' #include 'utils/helpers.js' #include 'utils/json2.js& ...

Best practices for including jQuery in ASP.NET (or any other external JavaScript libraries)

Can you explain the distinctions among these three code samples below? Is there a preferred method over the others, and if so, why? 1.Page.ClientScript.RegisterClientScriptInclude(typeof(demo), "jQuery", Re ...

What is the method to find the biggest number in an array?

I'm encountering challenges with this piece of code and it seems impossible to locate a resolution. Take a look at the code snippet: function findHighestValue(numbers) {} const highestValue = findHighestValue([1, 9, 5]); console.log(highestValue); ...

Is there a way to identify the element causing an error in promise.map and then either log it to the console or skip it using code?

When running the code provided below, I encounter a 500 error for certain URLs in my list due to the structure of the page. The error occurs specifically on the line .map((htmlOnePage, index) => when some URLs lead to invalid pages, causing the prog ...