They are the same. Just put the similar LinkButton to TemplateField in DetailsView, not using AutoGenereateDelete or ButtonField. For the delete button you have to set the CommandName to Delete. Then it will fire the ItemUpdating event.
<asp:LinkButton ID="DeleteButton" runat="server" CausesValidation="False" CommandName="Delete" Text="Delete" OnClientClick="return confirm('Are you certain you want to delete this product?');"> </asp:LinkButton>
I am not sure why you cannot make it work. It is quite simple, just set the OnClientClick property.
I can get the LinkButton to work. The problem I have here is that I also use a New and Edit. I don't have enough experience to wire up all the commands and executions, that's why I like using the Command Field or AutoGenerate - because all the commands
and executions are automatic. Also, I like the AutoGenerate because at times I want to disable the New button and using AutoGenerate is the easiest way I have found to control that. Lastly, I want to try to put all the LinkButtons / Commands into one row.
Command Field and AutoGenerate do this. If I try this with Template Field and Link Buttons, it stretches the Header column out (since there is no footer in DetailsView).
I was hoping to be able to set AutoGenerate (because I can disable in code behind where simply checking Enable Command doesn't allow for disable) and apply the steps in the tutorial to a DetailsView. The method you showed me is from Step 2 of the tutorial.
I am trying to do Step 3 in the tutorial and change it to apply to a DetailsView. Is this possible? How?
That's ok. You can use AutogenerateButton. I have given the solution, which I have tested it. It works perfectly. What you need to do is to give the correct row index and control index for your DetailsView. The indexes from my code are for my DetailsView,
so you need to change them. I think it is not a tough job. Just try and try again for those indexes, you can use Response.Write to output the current control to find button control.
If you still cannot figure out it, please post the DetailsView. Maybe I can help you find it.
When I run your code, the delete executes without the confirmation. When you say (in your code comment) to add to ClientClick, where and how is that set. I think you mean in the DetailsView asp code, but I do not see ClientClick as an option. I see "OnItemCommand"
or "OnItemDeleting". Would one of these be used? If so, what do I set it equal to? Or do I need to set it to the LinkButton ClientClick in asp? With the AutoGenerate option, the command rows or LinkButtons are not explicitly stated. The only asp reference
I see to the LinkBUttons are AutoGenerateDelete="True", etc.
The OnClientClick is not a event, is a property of Button itself. Also I have give you the way to set it in my code.
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?');"
End Sub
What you need to do is only to find the autogenerate button like what I did. Of course, the layout of your DetailsView is different from mine. So I think you need to try the way above to find your auto-button level by level. I mean which row placed Button,
which control in the row is the button you want. You have to try, and you can call Response.Write() method to output the current control you get to see what it is and how far you get the button. I think it is a good practise to do it to improve the debug skill
for you.
If you cannot handle it, just use Button in TemplateField. You just set the "Delete", "Edit", "Insert" to CommandName of Button or LinkButton. They will do data operations automatically, too.
Here is the code for add confirm message to the delete button for your post DetailsView above. Just handle DataBound event of DetailsView.
Protected Sub DetailsView1_DataBound(ByVal sender As Object, ByVal e As EventArgs)
Dim index As Integer = DirectCast(DetailsView1.Controls(0), Table).Rows.Count
Response.Write(DirectCast(DetailsView1.Controls(0), Table).Rows(index - 2).Controls(0).Controls(2))
Dim lb As LinkButton = DirectCast(DirectCast(DetailsView1.Controls(0), Table).Rows(index - 2).Controls(0).Controls(2), LinkButton)
lb.OnClientClick = "return confirm('Are you certain you want to delete this product?');"
End Sub
"***.Controls(0).Controls(2)" is Delete button. "***.Controls(0).Controls(0)" is Edit button. "***.Controls(0).Controls(2)" is new button.
Qin Dian Tan...
All-Star
113532 Points
12480 Posts
Microsoft
Re: DetailsView confirm message
Apr 14, 2010 02:42 AM|LINK
Hi mjta,
They are the same. Just put the similar LinkButton to TemplateField in DetailsView, not using AutoGenereateDelete or ButtonField. For the delete button you have to set the CommandName to Delete. Then it will fire the ItemUpdating event.
<asp:LinkButton ID="DeleteButton" runat="server" CausesValidation="False" CommandName="Delete" Text="Delete" OnClientClick="return confirm('Are you certain you want to delete this product?');"> </asp:LinkButton>
I am not sure why you cannot make it work. It is quite simple, just set the OnClientClick property.
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 14, 2010 02:29 PM|LINK
I can get the LinkButton to work. The problem I have here is that I also use a New and Edit. I don't have enough experience to wire up all the commands and executions, that's why I like using the Command Field or AutoGenerate - because all the commands and executions are automatic. Also, I like the AutoGenerate because at times I want to disable the New button and using AutoGenerate is the easiest way I have found to control that. Lastly, I want to try to put all the LinkButtons / Commands into one row. Command Field and AutoGenerate do this. If I try this with Template Field and Link Buttons, it stretches the Header column out (since there is no footer in DetailsView).
I was hoping to be able to set AutoGenerate (because I can disable in code behind where simply checking Enable Command doesn't allow for disable) and apply the steps in the tutorial to a DetailsView. The method you showed me is from Step 2 of the tutorial. I am trying to do Step 3 in the tutorial and change it to apply to a DetailsView. Is this possible? How?
Qin Dian Tan...
All-Star
113532 Points
12480 Posts
Microsoft
Re: DetailsView confirm message
Apr 15, 2010 03:09 AM|LINK
Hi mjta,
That's ok. You can use AutogenerateButton. I have given the solution, which I have tested it. It works perfectly. What you need to do is to give the correct row index and control index for your DetailsView. The indexes from my code are for my DetailsView, so you need to change them. I think it is not a tough job. Just try and try again for those indexes, you can use Response.Write to output the current control to find button control.
If you still cannot figure out it, please post the DetailsView. Maybe I can help you 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 15, 2010 02:55 PM|LINK
When I run your code, the delete executes without the confirmation. When you say (in your code comment) to add to ClientClick, where and how is that set. I think you mean in the DetailsView asp code, but I do not see ClientClick as an option. I see "OnItemCommand" or "OnItemDeleting". Would one of these be used? If so, what do I set it equal to? Or do I need to set it to the LinkButton ClientClick in asp? With the AutoGenerate option, the command rows or LinkButtons are not explicitly stated. The only asp reference I see to the LinkBUttons are AutoGenerateDelete="True", etc.
Here is my code....hope it helps....
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" AutoGenerateInsertButton="True" AutoGenerateRows="False" BorderColor="#003366" BorderStyle="Solid" BorderWidth="1px" CellPadding="4" DataKeyNames="HosId" DataSourceID="SqlDataSource1" ForeColor="#333333" GridLines="None" Height="50px"> <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> <CommandRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="Blue" /> <RowStyle BackColor="#EFF3FB" BorderColor="#003366" BorderStyle="Solid" BorderWidth="1px" Width="100px" /> <FieldHeaderStyle BackColor="#DEE8F5" Font-Bold="True" Width="100px" Wrap="False" /> <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="left" /> <Fields> <asp:BoundField DataField="HosId" HeaderText="HosId" visible="false" SortExpression="HosId" /> <asp:BoundField DataField="HosNam" HeaderText="Name" SortExpression="HosNam" /> <asp:BoundField DataField="HosCit" HeaderText="City" SortExpression="HosCit" /> <asp:BoundField DataField="HosSta" HeaderText="State" SortExpression="HosSta" /> <asp:BoundField DataField="HosPhNum" HeaderText="Phone" SortExpression="HosPhNum" /> <asp:BoundField DataField="IPAddress" visible="false" HeaderText="IPAddress" SortExpression="IPAddress" /> <asp:BoundField DataField="Date" visible="false" HeaderText="Date" SortExpression="Date" /> <asp:BoundField DataField="UserID" visible="false" HeaderText="UserID" SortExpression="UserID" /> <asp:BoundField DataField="ExtIPAdd" visible="false" HeaderText="ExtIPAdd" SortExpression="ExtIPAdd" /> </Fields> <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> <EditRowStyle BackColor="#2461BF" /> <AlternatingRowStyle BackColor="White" /> </asp:DetailsView>Qin Dian Tan...
All-Star
113532 Points
12480 Posts
Microsoft
Re: DetailsView confirm message
Apr 16, 2010 02:59 AM|LINK
Hi mjta,
The OnClientClick is not a event, is a property of Button itself. Also I have give you the way to set it in my code.
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?');"
End Sub
What you need to do is only to find the autogenerate button like what I did. Of course, the layout of your DetailsView is different from mine. So I think you need to try the way above to find your auto-button level by level. I mean which row placed Button, which control in the row is the button you want. You have to try, and you can call Response.Write() method to output the current control you get to see what it is and how far you get the button. I think it is a good practise to do it to improve the debug skill for you.
If you cannot handle it, just use Button in TemplateField. You just set the "Delete", "Edit", "Insert" to CommandName of Button or LinkButton. They will do data operations automatically, too.
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 16, 2010 12:46 PM|LINK
do I set the response.write() on the databinding event and does something go in the ()?
Also, are all the Controls(0) in the code referring to the same thing? I'm not sure how to interpret Controls(0).Controls(0).
thanks
Qin Dian Tan...
All-Star
113532 Points
12480 Posts
Microsoft
Re: DetailsView confirm message
Apr 19, 2010 03:08 AM|LINK
Hi mjta,
Here is the code for add confirm message to the delete button for your post DetailsView above. Just handle DataBound event of DetailsView.
Protected Sub DetailsView1_DataBound(ByVal sender As Object, ByVal e As EventArgs)
Dim index As Integer = DirectCast(DetailsView1.Controls(0), Table).Rows.Count
Response.Write(DirectCast(DetailsView1.Controls(0), Table).Rows(index - 2).Controls(0).Controls(2))
Dim lb As LinkButton = DirectCast(DirectCast(DetailsView1.Controls(0), Table).Rows(index - 2).Controls(0).Controls(2), LinkButton)
lb.OnClientClick = "return confirm('Are you certain you want to delete this product?');"
End Sub
"***.Controls(0).Controls(2)" is Delete button. "***.Controls(0).Controls(0)" is Edit button. "***.Controls(0).Controls(2)" is new button.
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 19, 2010 02:13 PM|LINK
Got it. thanks
mjta
Member
324 Points
684 Posts
Re: DetailsView confirm message
Apr 19, 2010 06:34 PM|LINK
If I click on Edit and the DetailsVIew goes into Edit mode, I get the message when I click Cancel in Edit mode. How would you suggest resolving this?
mjta
Member
324 Points
684 Posts
Re: DetailsView confirm message
Apr 19, 2010 07:11 PM|LINK
I set some condtions to check for current mode and page count....that seems to fix the Edit mode issue. thanks