Call Javascript confirm from code behind

  • Thread starter Thread starter tshad
  • Start date Start date
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
 
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
 
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.
 
Back
Top