Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

datagrid - confirm entry before posting 2

Status
Not open for further replies.

DebHanleyRI

Programmer
Jun 18, 2002
35
US
I am a newbie...

I have a datagrid in asp.net where our users record the number of steps they have walked since last week - this works great - however - I have been asked to add a confirm button that will basically say "Are you certain you want to post xxx steps today" after the user clicks update.

Is there a way to add a messagebox to a webpage(with yes and no buttons) that will only execute the code if the user says yes?

Here is the code for my datagrid update:

Sub dgWalkers_Update(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)
Dim strName As String
strName = LCase(Session("UserName"))
Dim strCoach As String
strCoach = Session("strCoach")

Dim dblSteps As Double
dblSteps = "0"
'check steps for numeric value
If IsNumeric(CType(e.Item.Cells(5).Controls(0), TextBox).Text) Then
Dim sErr As String, spath1

Dim WalkerID As String = e.Item.Cells(0).Text
Dim dblTotalSteps As Double = e.Item.Cells(1).Text
Dim dblTotalDays As Double = e.Item.Cells(2).Text
Dim dblTotalMiles As Double = e.Item.Cells(3).Text
Dim dtLastReported As Date = e.Item.Cells(4).Text
Dim dblSteps2 As Double = CType(e.Item.Cells(5).Controls(0), TextBox).Text
dblSteps = dblSteps2
'Dim strActive As TextBox = e.Item.Cells(6).Controls(0)

Dim stractive As String = DirectCast(e.Item.Cells(6).FindControl("txtActive"), TextBox).Text
'Dim stractive As String = e.Item.Cells(6).ID("txtactive")
' days Out - hidden column
Dim intDaysOut As Integer = e.Item.Cells(8).Text

' prevent blanks or anything other than Y or N
stractive = UCase(Trim(stractive))
If stractive = "" Then stractive = "Y"
If stractive = "Y" Or stractive = "N" Then

Else
sErr = "Active should be Y or N"
If sErr <> "" Then
spath1 = "<script language='javascript'>alert('"
spath1 = spath1 + sErr
spath1 = spath1 + "');</script>"
Page.RegisterStartupScript("sPath1", spath1)

Exit Sub

End If
End If

Dim strSql As String

Dim dblNewTotalSteps As Double
dblNewTotalSteps = dblTotalSteps + dblSteps

Dim intdays As Integer
'total # of days between start date and now
intdays = DateDiff(DateInterval.Day, Session("activeDte"), Now)

Dim intNewDays As Integer
intNewDays = intdays + 1
'subtract days out
intNewDays = (intNewDays - intDaysOut)

Dim dblNewMiles As Double
If dblNewTotalSteps <> 0 Then
dblNewMiles = dblNewTotalSteps / 2000
Else
dblNewMiles = 0
End If


'here is where I would like the messagebox to appear - if Yes, do the following. If no, drop out



If strCoach = UCase("Y") Or strName = WalkerID Then
strSql = "UPDATE walkers set "
strSql = strSql & " lastReported = @lastReported, "
strSql = strSql & "totalSteps = @totalSteps, totalDays = @totalDays, Active = @Active, Miles = @Miles, "
strSql = strSql & " added = @added, addedBy = @addedBy "
strSql = strSql & " WHERE (walker = " & "'" & WalkerID & "'" & " )"
Dim MyConn As SqlClient.SqlConnection = New SqlClient.SqlConnection(ConfigurationSettings.AppSettings("MyDBConnection"))

Dim Cmd As New SqlClient.SqlCommand(strSql, MyConn)
Cmd.Parameters.Add(New SqlClient.SqlParameter("@lastReported", Now))
Cmd.Parameters.Add(New SqlClient.SqlParameter("@Active", UCase(stractive)))
Cmd.Parameters.Add(New SqlClient.SqlParameter("@totalSteps", dblNewTotalSteps))
Cmd.Parameters.Add(New SqlClient.SqlParameter("@totalDays", intNewDays))
Cmd.Parameters.Add(New SqlClient.SqlParameter("@Miles", dblNewMiles))
Cmd.Parameters.Add(New SqlClient.SqlParameter("@added", Now))
Cmd.Parameters.Add(New SqlClient.SqlParameter("@addedBy", strName))

MyConn.Open()
Cmd.ExecuteNonQuery()
MyConn.Close()

'Audit
'Days = diff between last reported and today

Dim intDaysDiff As Integer
intDaysDiff = DateDiff(DateInterval.Day, dtLastReported, Now)

strSql = "insert into walkersAudit (walker, teamName, "
strSql = strSql & "Active, activeDate, lastReported, "
strSql = strSql & "totalSteps, totalDays, steps, days, miles, added, addedBy )"
strSql = strSql & "(Select walker, teamName, "
strSql = strSql & "Active, activeDate, lastReported, "
strSql = strSql & "totalSteps, totalDays, " & "'" & dblSteps & "'" & ",'" & intDaysDiff & "'" & ",'" & dblTotalMiles & "',"
strSql = strSql & " added, addedBy"
strSql = strSql & " from walkers where walker = " & "'" & WalkerID & "'" & " ) "


Dim MyConn2 As SqlClient.SqlConnection = New SqlClient.SqlConnection(ConfigurationSettings.AppSettings("MyDBConnection"))
Dim Cmd2 As New SqlClient.SqlCommand(strSql, MyConn2)
MyConn2.Open()
Cmd2.ExecuteNonQuery()
MyConn2.Close()
'added 2/26/08
dblSteps = 0
'strSql = ""

'deactivate user
If UCase(stractive) = "N" Then
Dim dtInactive As Date
dtInactive = Now
strSql = "insert into walkersOut Values (" & "'" & WalkerID & "', " & "'" & dtInactive & "'," & "'" & dtInactive & "'," & "'0')"
Dim MyConn3 As SqlClient.SqlConnection = New SqlClient.SqlConnection(ConfigurationSettings.AppSettings("MyDBConnection"))
Dim Cmd3 As New SqlClient.SqlCommand(strSql, MyConn3)
MyConn3.Open()
Cmd3.ExecuteNonQuery()
MyConn3.Close()
End If

'EditItemIndex to -1 and rebind the DataGrid
dgWalkers.EditItemIndex = -1
LoadWalkerTeamData()
If strCoach = "Y" Then
LoadInactive()
Else
LoadWalker()
End If

Else

sErr = "You do not have permission to update this walker!!!"
If sErr <> "" Then
spath1 = "<script language='javascript'>alert('"
spath1 = spath1 + sErr
spath1 = spath1 + "');</script>"
Page.RegisterStartupScript("sPath1", spath1)

Exit Sub
End If

End If

Else
Dim sErr As String, spath1
sErr = "Steps must be a numeric Value"
If sErr <> "" Then
spath1 = "<script language='javascript'>alert('"
spath1 = spath1 + sErr
spath1 = spath1 + "');</script>"
Page.RegisterStartupScript("sPath1", spath1)

Exit Sub
End If

End If




End Sub



not all who wander are lost....
 
Yes, you can. Here's a simple example that you should be able to incorporate into your page:



-------------------------------------------------------

Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]
[URL unfurl="true"]http://mdssolutions.co.uk[/url] - Delivering professional ASP.NET solutions
[URL unfurl="true"]http://weblogs.asp.net/marksmith[/url]
 
Hello Mark,

Thanks for looking into this...

I tried the code in the link - however - the update button is a template button on the datagrid and onClientClick is not an option fron the update on the grid.

Is it possible to have a messagebox that can return a yes or no value from the update_datagrid? This way, If yes I can update the datagris, if no I can exit the sub without updating.


This is where I tried the onclientclick...

<EditItemTemplate> <asp:LinkButton runat="server" Text="Update" CommandName="Update" OnClientClick="return confirm('Are you sure you want to update this record?');"></asp:LinkButton>&nbsp;
<asp:LinkButton runat="server" Text="Cancel" CommandName="Cancel" CausesValidation="false"></asp:LinkButton>
</EditItemTemplate>

not all who wander are lost....
 
Give all these LinkButtons the same CSS class. Then using Javascript you can loop through all your anchor tags as part of a window.onload event handler. For each anchor with this class you can then add your onclick event handlers. So, if all these LinkButtons has the class 'do_confirm' just add this Javascript to an external JS file...

Code:
window.onload = function() {
  var anchors = document.getElementsByTagName('a');
  for (var i = 0; i < anchors.length; i++) {
    if (anchors[i].className == 'do_confirm') {
      anchors[i].onclick = function() {
        return confirm('Are you sure?');
      }
    }
  }
}

I've just written this out by hand so apologies if it's not perfect but you get the idea I hope. Note that it's always best to dynamically write Javascript event handlers from an external file (as opposed to including event handlers as attributes of HTML elements) to maintain the separation between HTML and client scripts, and also to ensure your HTML reads as semantic XML.
 
Or you can use ca8msm's link to do it. You don't need the OnClientClick event, OnClick is suitable. No need to do all that javascript if you can add an attribute to the Button when you databind the rows.
 
Thank you everyone - here is what I ended up with(after 16 hours of looking at it):

in my HTML:
<input type="hidden" id="Hidden1" name="Hidden1" runat="server">

my vb code:
Protected Hidden1 As System.Web.UI.HtmlControls.HtmlInputHidden

In my page load:

Private Sub Page_Load

If IsPostBack AndAlso Request("__EVENTTARGET") = "Hidden1" Then
If Hidden1.Value = "true" Then
Hidden1.Value = "false"
Commit_Steps()
Else
End If
End If

End Sub

In the grid's update click event, I placed the values of each cell on the selcted line into a session variable and moved the db updates to a new sub - in doing this, I am able to get the values, store them and give my yes/no box to the user:
Sub dgWalkers_Update(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)
Dim sErr As String, spath1
Dim strName As String
strName = LCase(Session("UserName"))

Dim strCoach As String
strCoach = Session("strCoach")

Dim dblSteps As Double
dblSteps = "0"
Session("dblSteps") = dblSteps
'check steps for numeric value
If IsNumeric(CType(e.Item.Cells(5).Controls(0), TextBox).Text) Then

Dim WalkerID As String = e.Item.Cells(0).Text
Session("WalkerID") = WalkerID

Dim dblTotalSteps As Double = e.Item.Cells(1).Text
Session("dblTotalSteps") = dblTotalSteps

Dim dblTotalDays As Double = e.Item.Cells(2).Text
Session("dblTotalDays") = dblTotalDays

Dim dblTotalMiles As Double = e.Item.Cells(3).Text
Session("dblTotalMiles") = dblTotalMiles

Dim dtLastReported As Date = e.Item.Cells(4).Text
Session("dtLastReported") = dtLastReported

Dim dblSteps2 As Double = CType(e.Item.Cells(5).Controls(0), TextBox).Text
dblSteps = dblSteps2
Session("dblSteps") = dblSteps2

Dim stractive As String = DirectCast(e.Item.Cells(6).FindControl("txtActive"), TextBox).Text

' days Out - hidden column
Dim intDaysOut As Integer = e.Item.Cells(8).Text
Session("intDaysOut") = intDaysOut


' prevent blanks or anything other than Y or N
stractive = UCase(Trim(stractive))
If stractive = "" Then stractive = "Y"
If stractive = "Y" Or stractive = "N" Then
Session("strActive") = stractive
Else
sErr = "Active should be Y or N"
If sErr <> "" Then
spath1 = "<script language='javascript'>alert('"
spath1 = spath1 + sErr
spath1 = spath1 + "');</script>"
Page.RegisterStartupScript("sPath1", spath1)
Exit Sub
End If
End If

Dim strSql As String

Dim dblNewTotalSteps As Double
dblNewTotalSteps = dblTotalSteps + dblSteps
Session("dblNewTotalSteps") = dblNewTotalSteps

Dim intdays As Integer
'total # of days between start date and now
intdays = DateDiff(DateInterval.Day, Session("activeDte"), Now)

Dim intNewDays As Integer
intNewDays = intdays + 1
'subtract days out
intNewDays = (intNewDays - intDaysOut)
Session("intNewDays") = intNewDays

Dim dblNewMiles As Double
If dblNewTotalSteps <> 0 Then
dblNewMiles = dblNewTotalSteps / 2000
Else
dblNewMiles = 0
End If
Session("dblNewMiles") = dblNewMiles

Dim scriptString As String, strCommitSteps As String
If dblSteps = 0 Then
strCommitSteps = "You have chosen to de-activate: " & Session("WalkerID") & ". Click OK to accept, Cancel to re-enter "
Else
strCommitSteps = "You have entered: " & Session("DblSteps") & " steps for " & Session("WalkerID") & ". Click OK to accept, Cancel to re-enter "
End If

scriptString = "<script language=JavaScript>"
scriptString += "document.getElementById('" + Hidden1.ClientID + "').value = window.confirm('" & strCommitSteps & " ');"
scriptString += GetPostBackEventReference(Hidden1, String.Empty) + ";"
scriptString += "</script>"
Page.RegisterStartupScript("WindowConfirmScript", scriptString)

Else
'Dim sErr As String, spath1
sErr = "Steps must be a numeric Value"
If sErr <> "" Then
spath1 = "<script language='javascript'>alert('"
spath1 = spath1 + sErr
spath1 = spath1 + "');</script>"
Page.RegisterStartupScript("sPath1", spath1)

Exit Sub
End If

End If





End Sub

A new sub that will do the writes to the db using the session variables created on the grid's update click event:
Private Sub Commit_Steps()
Dim strSql As String, sErr As String, sPath1 As String
Dim strName As String
strName = LCase(Session("UserName"))

Dim dtLastReported As Date
dtLastReported = Session("dtLastReported")

Dim strCoach As String
strCoach = Session("strCoach")

Dim dblSteps As Double
dblSteps = Session("dblsteps")

Dim dblNewTotalSteps As Double
dblNewTotalSteps = Session("dblNewTotalSteps")

Dim intNewDays As Integer
intNewDays = Session("intNewDays")

Dim dblTotalMiles As Double
dblTotalMiles = Session("dblTotalMiles")

Dim dblNewMiles As Double
dblNewMiles = Session("dblNewMiles")

Dim strActive As String
strActive = Session("strActive")

Dim WalkerID As String
WalkerID = Session("WalkerID")

If strCoach = UCase("Y") Or strName = WalkerID Then
strSql = "UPDATE walkers set "
strSql = strSql & " lastReported = @lastReported, "
strSql = strSql & "totalSteps = @totalSteps, totalDays = @totalDays, Active = @Active, Miles = @Miles, "
strSql = strSql & " added = @added, addedBy = @addedBy "
strSql = strSql & " WHERE (walker = " & "'" & WalkerID & "'" & " )"
Dim MyConn As SqlClient.SqlConnection = New SqlClient.SqlConnection(ConfigurationSettings.AppSettings("MyDBConnection"))

Dim Cmd As New SqlClient.SqlCommand(strSql, MyConn)
Cmd.Parameters.Add(New SqlClient.SqlParameter("@lastReported", Now))
Cmd.Parameters.Add(New SqlClient.SqlParameter("@Active", UCase(Session("strActive"))))
Cmd.Parameters.Add(New SqlClient.SqlParameter("@totalSteps", dblNewTotalSteps))
Cmd.Parameters.Add(New SqlClient.SqlParameter("@totalDays", intNewDays))
Cmd.Parameters.Add(New SqlClient.SqlParameter("@Miles", dblNewMiles))
Cmd.Parameters.Add(New SqlClient.SqlParameter("@added", Now))
Cmd.Parameters.Add(New SqlClient.SqlParameter("@addedBy", strName))

MyConn.Open()
Cmd.ExecuteNonQuery()
MyConn.Close()

'Audit
'Days = diff between last reported and today

Dim intDaysDiff As Integer
intDaysDiff = DateDiff(DateInterval.Day, dtLastReported, Now)

strSql = "insert into walkersAudit (walker, teamName, "
strSql = strSql & "Active, activeDate, lastReported, "
strSql = strSql & "totalSteps, totalDays, steps, days, miles, added, addedBy )"
strSql = strSql & "(Select walker, teamName, "
strSql = strSql & "Active, activeDate, lastReported, "
strSql = strSql & "totalSteps, totalDays, " & "'" & dblSteps & "'" & ",'" & intDaysDiff & "'" & ",'" & dblTotalMiles & "',"
strSql = strSql & " added, addedBy"
strSql = strSql & " from walkers where walker = " & "'" & WalkerID & "'" & " ) "


Dim MyConn2 As SqlClient.SqlConnection = New SqlClient.SqlConnection(ConfigurationSettings.AppSettings("MyDBConnection"))
Dim Cmd2 As New SqlClient.SqlCommand(strSql, MyConn2)
MyConn2.Open()
Cmd2.ExecuteNonQuery()
MyConn2.Close()
'added 2/26/08
dblSteps = 0
'strSql = ""

'deactivate user
If UCase(strActive) = "N" Then
Dim dtInactive As Date
dtInactive = Now
strSql = "insert into walkersOut Values (" & "'" & WalkerID & "', " & "'" & dtInactive & "'," & "'" & dtInactive & "'," & "'0')"
Dim MyConn3 As SqlClient.SqlConnection = New SqlClient.SqlConnection(ConfigurationSettings.AppSettings("MyDBConnection"))
Dim Cmd3 As New SqlClient.SqlCommand(strSql, MyConn3)
MyConn3.Open()
Cmd3.ExecuteNonQuery()
MyConn3.Close()
End If

'EditItemIndex to -1 and rebind the DataGrid
dgWalkers.EditItemIndex = -1
LoadWalkerTeamData()
If strCoach = "Y" Then
LoadInactive()
Else
LoadWalker()
End If

Else

sErr = "You do not have permission to update this walker!!!"
If sErr <> "" Then
sPath1 = "<script language='javascript'>alert('"
sPath1 = sPath1 + sErr
sPath1 = sPath1 + "');</script>"
Page.RegisterStartupScript("sPath1", sPath1)

Exit Sub
End If

End If
End Sub

Thanks Again - it was great how I was able to take a little from each post and get it all together - thank you!!!

not all who wander are lost....
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top