Confirm messagebox

  • Thread starter Thread starter Eric
  • Start date Start date
E

Eric

Hi,

I have a gridview with the last column to be a buttonfield.
This is a delete button.
When the user clicks it, the RowCommand event will be fired and the
rowcommand property holds the line number.

I would like from the rowcommand event, to show a popup asking the user if
he wants to continue, like a javascript confirm box.
I can put up the box with

ClientScript.RegisterStartupScript(Me.GetType(), "confirm",
"<script>confirm('" + Msg + "');</script>")

but how can I get it to go to link1 if the answer is ok?

something like: ok pressed --> "~\default.aspx?userclickedOK=true"

is this at all possible?

rg,
Eric
 
Hi,

I have a gridview with the last column to be a buttonfield.
This is a delete button.
When the user clicks it, the RowCommand event will be fired and the
rowcommand property holds the line number.

I would like from the rowcommand event, to show a popup asking the user if
he wants to continue, like a javascript confirm box.
I can put up the box with

ClientScript.RegisterStartupScript(Me.GetType(), "confirm",
"<script>confirm('" + Msg + "');</script>")

but how can I get it to go to link1 if the answer is ok?

something like: ok pressed --> "~\default.aspx?userclickedOK=true"

is this at all possible?

rg,
Eric

Hi Eric,

you can do it from the code behind, using RowDataBound method. Here's
an example on VB

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal
e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles
GridView1.RowDataBound
If (e.Row.RowType = DataControlRowType.DataRow) Then
If (e.Row.RowState = DataControlRowState.Normal Or
e.Row.RowState = DataControlRowState.Alternate) Then
Dim l As LinkButton = e.Row.Cells(0).Controls(0) <----
this must be change acc. to your grid setup
l.Attributes.Add("onclick", "javascript:return confirm
('Are you sure?')") <--- this is your confirm script
End If
End If
End Sub

If user pressed OK then RowDeleting Event get fired

Protected Sub GridView1_RowDeleting(ByVal sender As Object, ByVal
e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles
GridView1.RowDeleting
....
End Sub

and this is where you can programmatically redirect to the new page.

Hope this helps
 
Alexey Smirnov said:
Hi Eric,

you can do it from the code behind, using RowDataBound method. Here's
an example on VB

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal
e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles
GridView1.RowDataBound
If (e.Row.RowType = DataControlRowType.DataRow) Then
If (e.Row.RowState = DataControlRowState.Normal Or
e.Row.RowState = DataControlRowState.Alternate) Then
Dim l As LinkButton = e.Row.Cells(0).Controls(0) <----
this must be change acc. to your grid setup
l.Attributes.Add("onclick", "javascript:return confirm
('Are you sure?')") <--- this is your confirm script
End If
End If
End Sub

If user pressed OK then RowDeleting Event get fired

Protected Sub GridView1_RowDeleting(ByVal sender As Object, ByVal
e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles
GridView1.RowDeleting
....
End Sub

and this is where you can programmatically redirect to the new page.

Hope this helps
.

Hi,

I applied this code to my page.
When the user clicks on de delete button, the popup does appear. Clicking on
cancel will just remove the popup, clicking on OK does a page refresh, but
the RowDeleting event is not fired.

How can I get it fired?

here is my code about the creation of the column:
Dim clBT As New ButtonField
clBT.HeaderText = ""
clBT.HeaderStyle.BackColor = gridbackcolor
clBT.HeaderStyle.ForeColor = gridforecolor
clBT.HeaderStyle.Font.Name = "Arial"
clBT.HeaderStyle.Font.Bold = True
clBT.HeaderStyle.Font.Size = fontsize
clBT.Text = "delete"
clBT.ItemStyle.Font.Size = 8
clBT.ItemStyle.Font.Name = "Arial"
clBT.ItemStyle.Font.Underline = True
clBT.ButtonType = ButtonType.Link
clBT.ItemStyle.Width = 40
clBT.ItemStyle.HorizontalAlign = HorizontalAlign.Center
ctrl.Columns.Add(clBT)

where ctrl is the gridview.
This is the 14th column.

The following is in the RowDatabound event:

If e.Row.RowType = DataControlRowType.DataRow Then
If e.Row.RowState = DataControlRowState.Normal Or e.Row.RowState =
DataControlRowState.Alternate Then
Try
If e.Row.Cells(14).Controls.Count > 0 Then
Dim lb As LinkButton = e.Row.Cells(14).Controls(0)
lb.Attributes.Add("onclick", "javascript:return confirm('Are you
sure?')")
End If
Catch ex As Exception
End Try
End If
End If

What can I do now?

rg,
Eric
 
Eric said:
Hi,

I applied this code to my page.
When the user clicks on de delete button, the popup does appear. Clicking on
cancel will just remove the popup, clicking on OK does a page refresh, but
the RowDeleting event is not fired.

How can I get it fired?

here is my code about the creation of the column:
Dim clBT As New ButtonField
clBT.HeaderText = ""
clBT.HeaderStyle.BackColor = gridbackcolor
clBT.HeaderStyle.ForeColor = gridforecolor
clBT.HeaderStyle.Font.Name = "Arial"
clBT.HeaderStyle.Font.Bold = True
clBT.HeaderStyle.Font.Size = fontsize
clBT.Text = "delete"
clBT.ItemStyle.Font.Size = 8
clBT.ItemStyle.Font.Name = "Arial"
clBT.ItemStyle.Font.Underline = True
clBT.ButtonType = ButtonType.Link
clBT.ItemStyle.Width = 40
clBT.ItemStyle.HorizontalAlign = HorizontalAlign.Center
ctrl.Columns.Add(clBT)

where ctrl is the gridview.
This is the 14th column.

The following is in the RowDatabound event:

If e.Row.RowType = DataControlRowType.DataRow Then
If e.Row.RowState = DataControlRowState.Normal Or e.Row.RowState =
DataControlRowState.Alternate Then
Try
If e.Row.Cells(14).Controls.Count > 0 Then
Dim lb As LinkButton = e.Row.Cells(14).Controls(0)
lb.Attributes.Add("onclick", "javascript:return confirm('Are you
sure?')")
End If
Catch ex As Exception
End Try
End If
End If

What can I do now?

rg,
Eric

I think I solved it now, after a click on OK the rowcommand event is fired,
but with cancel it isn't.
So can use that since I don't use it for anything else.

Not sure if it is the right solution, though.

rg.
Eric
 
I think I solved it now, after a click on OK the rowcommand event is fired,
but with cancel it isn't.
So  can use that since I don't use it for anything else.

That's usual. You clicked on Delete, it asked you to confirm and you
canceled it. What event you want to fire if you don't want to proceed?

If you need to have something on Cancel, improve your javascript.
Instead of using direct "return confirm", get its value and use as you
need...

x = return confirm(...);
if (x)
{
....
}
else
{
....
}
 
Back
Top