Call Javascript confirm from code behind

T

tshad

In 3.5 is there a way to call a javascript confirm box from the code - not a
button - and get the response back before continuing on?

In my program, I want to be able to check the database to see if a company
exists after the save button is pressed and if it does not, then popup a box
asking if the user really wants to add this new company or not.

If so, continue with the save, if not stop the processing.

Thanks,

Tom
 
W

William Niver

Howdy Tom!
I haven't created all the code for you, just the concept of how to create
the cycle of JavaScript calls CodeBehind and CodeBehind calls JavaScript
Back.

Hope it helps!

William

Default.aspx:

Code View:

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb"
Inherits="_Default" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Test</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<a href="javascript:LinkButton1.click();">Click to Cause Postback</a>
<div style="display: none;">
<asp:UpdatePanel ID="upnl_Main" runat="server"
ChildrenAsTriggers="false" UpdateMode="conditional">
<ContentTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" Text="" />
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>


Code Behind:

Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub LinkButton1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles LinkButton1.Click
'Do your lookups, checking and validation here.
Dim ScriptBuilder As New StringBuilder
'Build custom Javascript depending on your results.
ScriptBuilder.Append("function MyScript() {" + Environment.NewLine)
ScriptBuilder.Append(" alert('Howdy, I am dynamically created
javascript.');" + Environment.NewLine)
ScriptBuilder.Append("}" + Environment.NewLine)
ScriptBuilder.Append("MyScript();" + Environment.NewLine)
'register the javascript to the update panel control and update that
panel.
ScriptManager.RegisterStartupScript(upnl_Main, upnl_Main.GetType(),
"MyScript", ScriptBuilder.ToString, True)
upnl_Main.Update()
End Sub

End Class
 
N

Nanda Lella[MSFT]

You can use a web service to acheive this. Call a webserver on button click
from javascript and return true or false based on your criteria..

--

Thank You,
Nanda Lella,
http://www.ctrlf5.net

This Posting is provided "AS IS" with no warranties, and confers no rights.
 
Joined
Jan 27, 2010
Messages
27
Reaction score
0
How to create a customized confirmation alert in javascript

When you implement a delete feature in your application, a very basic additional feature which is always expected to be implemented is a confirmation box, so that the user gets a prompt before doing some delete activity.

For implementing this, there is already a Javascript function called confirm().
The syntax for this is very simple and i am sure every developer is aware of this. The syntax is as follow:

var confirm = confirm("Are you sure you want to do this?");
if(confirm)
{
//do your delete
return false;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,520
Members
44,996
Latest member
rainocode

Latest Threads

Top