I have an entry page that needs to prompt the user some times (with a javascript confirm) but not always. Right now, I have a javascript onclick event on the button that calls a PageMethod. (Let's say it's called 'CheckToPrompt'.)
CheckToPrompt checks my database and returns a 'yes' or 'no' to my javascript 'CheckSuccess' function.
I was able to get my code to work in the shared/static web method for checking if I need to prompt, but the javascript 'CheckSuccess' needs to raise a .Net event if the user confirms. Since an event raise can't be in a shared/static method, I have this function do a PostBack.
I'd rather not have to do a whole PostBack, but raise the event the same way it would if I left the button alone, and let it do an Ajax postback. (This was the original design before the prompting requirement.)
Here's my desired logic flow:
User clicks 'Add'
.Net checks for existing records
If no existing records, Raise the 'Add' event
If existing records, prompt with javascript.confirm
If confirm == 'yes' raise the Add event
Here's the current code, which works, but with a full postback at the end:
' .Net
btnAdd.Attributes.Add("onclick", "javascript:DoAdd('" & ddAddCType.ClientID & "', '" & _
txtAddCitationNumber.ClientID & "', '" & txtCitRange.ClientID & "', '" & txtCitAmount.ClientID & "')")
// JScript File
function DoAdd(typeId, addNumId, rangeId, amountId)
{
var citType = document.getElementById(typeId).value;
var startNum = document.getElementById(addNumId).value;
var citRange = document.getElementById(rangeId).value; //txtCitRange
var citQty = document.getElementById(amountId).value; //txtCitAmount
var endNum;
if (citRange != '')
{ endNum = citRange; }
else
{ endNum = startNum + citQty; }
PageMethods.CheckIfCitationsExist(citType, startNum, endNum, CheckSuccess, CallFailed);
}
'.Net
<System.Web.Services.WebMethod()> _
Public Shared Function CheckIfCitationsExist(ByVal citType As String, ByVal startNum As String, ByVal endNum As String) As String
Dim thisCitations As New IAppsData.Law.Citations
If IsNumeric(startNum) AndAlso IsNumeric(endNum) Then
If thisCitations.CheckBatch(citType, CType(startNum, Integer), CType(endNum, Integer)) Then
Return "yes"
Else
Return "no"
End If
Else
Return "no"
End If
End Function
// JScript File
function CheckSuccess(res)
{
if (res == 'yes')
{
if (confirm("Citations have already been assigned in this range. Are you sure?"))
{
<%= PostBackStr %>
}
}
else
{
<%= PostBackStr %>
}
}
'.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
PostBackStr = Page.ClientScript.GetPostBackEventReference(Me, "AddArg")
If Page.IsPostBack Then
Dim eventArg As String = Request("__EVENTARGUMENT")
If eventArg = "AddArg" Then
RaiseEvent Add(sender, e)
End If
End If
End Sub
Can you see what I am doing, and what I want to do?