Adding a Confirmation to a Command Button
I'm coming from a Classic ASP background, so I'm used to just using JavaScript to control the client side functionality of command buttons, so when it came time to take control of the Cancel button created by an ASP.NET DetailsView I was stumped. There's probably a better way to do it, but this worked for me.
The command buttons are created in the last row of the DetailsView during the ItemCreated event, so first check if the footer row has been created:
If Not (DetailsView1.FooterRow Is Nothing) Then
The command bar that contains your action buttons it the last element in the Rows collection. Once you have the command bar you can loop through all of its controls and find the one your looking for. I was looking for the Cancel button:
Dim commandRowIndex As Integer = DetailsView1.Rows.Count - 1Dim commandRow As DetailsViewRow = DetailsView1.Rows(commandRowIndex)Dim cell As DataControlFieldCell = CType(commandRow.Controls(0), DataControlFieldCell)' Loop through controls to find Cancel buttonFor Each ctl As Control In cell.Controls If TypeOf ctl Is Button Then Dim btn As Button = CType(ctl, Button) If btn.CommandName = "Cancel" Then btn.ToolTip = "Click here to cancel" btn.CommandName = "Cancel" btn.OnClientClick = "if(!confirm('Do you really want to discard the changes?')){return false;}" End If End IfNext
I wanted to add a confirmation to the Cancel the action, but you could use this example to add functionality to any of the command buttons. The confirmation button is created by adding a client side script to the OnClientClick property:
btn.OnClientClick = "if(!confirm('Do you really want to discard the changes?')){return false;}"
Putting it together:
Protected Sub DetailsView1_ItemCreated(ByVal sender As Object, ByVal e As EventArgs) Handles DetailsView1.ItemCreated' Check that footerRow has been createdIf Not (DetailsView1.FooterRow Is Nothing) Then' Find Command BarDim commandRowIndex As Integer = DetailsView1.Rows.Count - 1Dim commandRow As DetailsViewRow = DetailsView1.Rows(commandRowIndex)Dim cell As DataControlFieldCell = CType(commandRow.Controls(0), DataControlFieldCell)' Loop through controls to find Cancel buttonFor Each ctl As Control In cell.Controls If TypeOf ctl Is Button Then Dim btn As Button = CType(ctl, Button) If btn.CommandName = "Cancel" Then btn.ToolTip = "Click here to cancel" btn.CommandName = "Cancel" btn.OnClientClick = "if(!confirm('Do you really want to discard the changes?')){return false;}" End If End IfNextEnd IfEnd Sub
Redirecting User on Command Click
The next thing I wanted to do was redirect the user to a different page if they confirmed the cancel action. First I created a sub called "Get_Command" and then I called it from the OnItemCommand of the DetailsView.
Here's an example:
<asp:DetailsView ID="DetailsView1" runat="server"AutoGenerateRows="False"DataSourceID="ObjectDataSource1"OnItemCommand = "Get_Command">
And this code goes in your Code Behind file:
Sub Get_Command(ByVal Src As Object, ByVal Args As DetailsViewCommandEventArgs)If Args.CommandName = "Cancel" Then
Response.Redirect("~/yourPage.aspx:")
End IfEnd Sub
Conclusion
I hope somebody finds this post and it saves them a little time. If somebody knows a better way to do this - I'd love to see it.
Remember Me
Powered by: newtelligence dasBlog 1.8.5223.2
Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.
© Copyright 2010, Zenmonkeys
E-mail