' Get information about the product bound to the row
Dim drv As System.Data.DataRowView = DirectCast(DetailsView1.DataItem, System.Data.DataRowView)
Dim product As String = drv("InsNam")
db.OnClientClick = String.Format("return confirm('Are you certain you want to delete the {0} product?');", _
product.Replace("'", "\'"))
"Specified argument was out of the range of valid values. Parameter name: index"
- at
(row.Cells(0).Controls(0), LinkButton)
For Each row As DetailsViewRow In DetailsView1.Rows
If row.RowType = DataControlRowType.DataRow Then
' reference the Delete LinkButton
Dim db As LinkButton = CType(row.Cells(0).Controls(0), LinkButton)
' Get information about the product bound to the row
Dim drv As System.Data.DataRowView = DirectCast(DetailsView1.DataItem, System.Data.DataRowView)
Dim product As String = drv("InsNam")
db.OnClientClick = String.Format("return confirm('Are you certain you want to delete the {0} product?');", _
product.Replace("'", "\'"))
End If
Next
Error: Object reference not set to an instance of an object.
db.OnClientClick = String.Format("return confirm('Are you certain you want to delete the {0} product?');", _
product.Replace("'", "\'"))
Here is the updated code
For Each row As DetailsViewRow In DetailsView1.Rows
If row.RowType = DataControlRowType.DataRow Then
' reference the Delete LinkButton
Dim db As LinkButton = CType(row.FindControl("Delete"), LinkButton)
' Get information about the product bound to the row
Dim drv As System.Data.DataRowView = DirectCast(DetailsView1.DataItem, System.Data.DataRowView)
Dim product As String = drv("InsNam")
db.OnClientClick = String.Format("return confirm('Are you certain you want to delete the {0} product?');", _
product.Replace("'", "\'"))
End If
Next
Dim db As LinkButton = CType(row.FindControl("LinkButton1"), LinkButton)
By the way, the easy way to do this is in the source code of the linkbutton -
OnClientClick="return confirm('Are you sure you want to delete this product?')"
EDIT: Using a command field with <asp:CommandField ShowDeleteButton="true" />, your code should look like this:
Protected Sub DetailsView1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles DetailsView1.DataBound
' reference the Delete LinkButton
Dim db As LinkButton = CType(DetailsView1.Rows(0).Cells(0).Controls(0), LinkButton)
' Get information about the product bound to the row
Dim drv As System.Data.DataRowView = DirectCast(DetailsView1.DataItem, System.Data.DataRowView)
Dim product As String = drv("InsNam")
db.OnClientClick = String.Format("return confirm('Are you certain you want to delete the {0} product?');", _
product.Replace("'", "\'"))
End Sub
right, the first example in the tutorial, I was following (link below), used the simpler approach you mentioned. I have gotten that to work in other applications. Here, I am trying to figure out how to set it up with my current configuration in the DetailsView
working with SQL datasource. I used autogenerated command fields to take advantage of the built in functionality and have control of usage. But, in order to use the code you have presented, I need to convert the autogenerated delete into a template. I tried
that and then I realized I needed to try to convert the other commands into templates, which then leads me to needing to add code to execute the commands. Then I wonder, how much do I need the confirmation because of all the other adjustments? I was just
trying to get the code, from the following link, translated that uses the built-in command field to execute confirmation......
......and apply it to a DetailsView instead, without having to make many more adjustments beyond the code behind to apply to the DetailsView.
Also, I noticed the code in the above link works with an objectDataSource where I want it to work with a SQL datasource. I can't figure out how to apply it in my case, it might not be possible. But, your replys give me some hope. thanks
Of course, it is possible. The key is to access the autogenerate delete button in DetailsView. You can handle DataBound event of DetailsView to do it like this:
Protected Sub DetailsView1_DataBound(ByVal sender As Object, ByVal e As EventArgs)
Dim index As Integer = DirectCast(DetailsView1.Controls(0), Table).Rows.Count
'get all row count in DetailsView
Dim lb As LinkButton = DirectCast(DirectCast(DetailsView1.Controls(0), Table).Rows(index - 2).Controls(0).Controls(0), LinkButton)
'usually the autogenerate delete button is in the second last row, then access it
lb.OnClientClick = "return confirm('Are you certain you want to delete this product?');"
'do the same to ClientClick
End Sub
Maybe in your case the row index and controls index is not the same, please try it and you will find it.
I can't get the AutoGenerateDelete to work the way I want . I want to try to implement the code behind to prompt the message using the command field instead. I have followed the following tutorial but can't make the adjustments on the code from using
the GridView to using the DetailsView. Can you help me implemnent?
rivdiv
All-Star
16323 Points
2595 Posts
Re: DetailsView confirm message
Apr 02, 2010 03:57 PM|LINK
try this:
' Get information about the product bound to the row
Dim drv As System.Data.DataRowView = DirectCast(DetailsView1.DataItem, System.Data.DataRowView)
Dim product As String = drv("InsNam")
db.OnClientClick = String.Format("return confirm('Are you certain you want to delete the {0} product?');", _
product.Replace("'", "\'"))
mjta
Member
324 Points
684 Posts
Re: DetailsView confirm message
Apr 02, 2010 04:17 PM|LINK
I get an error-
"Specified argument was out of the range of valid values. Parameter name: index"
- at
(row.Cells(0).Controls(0), LinkButton)
For Each row As DetailsViewRow In DetailsView1.Rows If row.RowType = DataControlRowType.DataRow Then ' reference the Delete LinkButton Dim db As LinkButton = CType(row.Cells(0).Controls(0), LinkButton) ' Get information about the product bound to the row Dim drv As System.Data.DataRowView = DirectCast(DetailsView1.DataItem, System.Data.DataRowView) Dim product As String = drv("InsNam") db.OnClientClick = String.Format("return confirm('Are you certain you want to delete the {0} product?');", _ product.Replace("'", "\'")) End If Nextrivdiv
All-Star
16323 Points
2595 Posts
Re: DetailsView confirm message
Apr 02, 2010 04:34 PM|LINK
Try the following instead, where "LinkButton1" is the ID of your linkbutton
Dim db As LinkButton = CType(row.FindControl("LinkButton1"), LinkButton)
mjta
Member
324 Points
684 Posts
Re: DetailsView confirm message
Apr 02, 2010 04:58 PM|LINK
Error: Object reference not set to an instance of an object.
db.OnClientClick = String.Format("return confirm('Are you certain you want to delete the {0} product?');", _ product.Replace("'", "\'"))Here is the updated code
For Each row As DetailsViewRow In DetailsView1.Rows If row.RowType = DataControlRowType.DataRow Then ' reference the Delete LinkButton Dim db As LinkButton = CType(row.FindControl("Delete"), LinkButton) ' Get information about the product bound to the row Dim drv As System.Data.DataRowView = DirectCast(DetailsView1.DataItem, System.Data.DataRowView) Dim product As String = drv("InsNam") db.OnClientClick = String.Format("return confirm('Are you certain you want to delete the {0} product?');", _ product.Replace("'", "\'")) End If Nextrivdiv
All-Star
16323 Points
2595 Posts
Re: DetailsView confirm message
Apr 02, 2010 05:04 PM|LINK
"Delete" is the ID of your linkbutton? Make sure that is correct.
Otherwise, it should work.
mjta
Member
324 Points
684 Posts
Re: DetailsView confirm message
Apr 02, 2010 05:16 PM|LINK
It is part of the command field. The command button's AutoGenerateDeleteButton is True. Is this affecting it?
rivdiv
All-Star
16323 Points
2595 Posts
Re: DetailsView confirm message
Apr 02, 2010 05:38 PM|LINK
Yeah it is. You would have to use a template field instead. Like this -
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" Text="Delete" CommandName="Delete" />
</ItemTemplate>
</asp:TemplateField>
And in the code-behind, it would be -
Dim db As LinkButton = CType(row.FindControl("LinkButton1"), LinkButton)
By the way, the easy way to do this is in the source code of the linkbutton -
OnClientClick="return confirm('Are you sure you want to delete this product?')"
EDIT: Using a command field with <asp:CommandField ShowDeleteButton="true" />, your code should look like this:
Protected Sub DetailsView1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles DetailsView1.DataBound ' reference the Delete LinkButton Dim db As LinkButton = CType(DetailsView1.Rows(0).Cells(0).Controls(0), LinkButton) ' Get information about the product bound to the row Dim drv As System.Data.DataRowView = DirectCast(DetailsView1.DataItem, System.Data.DataRowView) Dim product As String = drv("InsNam") db.OnClientClick = String.Format("return confirm('Are you certain you want to delete the {0} product?');", _ product.Replace("'", "\'")) End Submjta
Member
324 Points
684 Posts
Re: DetailsView confirm message
Apr 05, 2010 02:42 PM|LINK
right, the first example in the tutorial, I was following (link below), used the simpler approach you mentioned. I have gotten that to work in other applications. Here, I am trying to figure out how to set it up with my current configuration in the DetailsView working with SQL datasource. I used autogenerated command fields to take advantage of the built in functionality and have control of usage. But, in order to use the code you have presented, I need to convert the autogenerated delete into a template. I tried that and then I realized I needed to try to convert the other commands into templates, which then leads me to needing to add code to execute the commands. Then I wonder, how much do I need the confirmation because of all the other adjustments? I was just trying to get the code, from the following link, translated that uses the built-in command field to execute confirmation......
http://www.asp.net/learn/data-access/tutorial-22-vb.aspx
......and apply it to a DetailsView instead, without having to make many more adjustments beyond the code behind to apply to the DetailsView.
Also, I noticed the code in the above link works with an objectDataSource where I want it to work with a SQL datasource. I can't figure out how to apply it in my case, it might not be possible. But, your replys give me some hope. thanks
Qin Dian Tan...
All-Star
113532 Points
12480 Posts
Microsoft
Re: DetailsView confirm message
Apr 07, 2010 03:41 AM|LINK
Hi mjta,
Of course, it is possible. The key is to access the autogenerate delete button in DetailsView. You can handle DataBound event of DetailsView to do it like this:
Protected Sub DetailsView1_DataBound(ByVal sender As Object, ByVal e As EventArgs)
Dim index As Integer = DirectCast(DetailsView1.Controls(0), Table).Rows.Count 'get all row count in DetailsView
Dim lb As LinkButton = DirectCast(DirectCast(DetailsView1.Controls(0), Table).Rows(index - 2).Controls(0).Controls(0), LinkButton)
'usually the autogenerate delete button is in the second last row, then access it
lb.OnClientClick = "return confirm('Are you certain you want to delete this product?');" 'do the same to ClientClick
End Sub
Maybe in your case the row index and controls index is not the same, please try it and you will find it.
Thanks,
If you have any feedback about my replies, please contactmsdnmg@microsoft.com.
Microsoft One Code Framework
mjta
Member
324 Points
684 Posts
Re: DetailsView confirm message
Apr 13, 2010 08:51 PM|LINK
I can't get the AutoGenerateDelete to work the way I want . I want to try to implement the code behind to prompt the message using the command field instead. I have followed the following tutorial but can't make the adjustments on the code from using the GridView to using the DetailsView. Can you help me implemnent?
http://www.asp.net/learn/data-access/tutorial-22-vb.aspx