Reputation: 39
my question is simple, how do i create a custom save and update button for records entered in a detail view. I dont want to use the ones given. thanks a lot.
Upvotes: 1
Views: 1267
Reputation: 16245
You have a couple of options. One is the OnItemCommand
and you'll roll your own commands.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.detailsview.itemcommand.aspx
The easier way is to use the OnItemInserted
and OnItemUpdating
events. Just send the Insert or Update commands instead as appropriate and you can use easier EventArgs
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.detailsview.iteminserting.aspx
<http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.detailsview.itemupdating.aspx
From these pages, basically what you'll do is capture a command from a button in your DetailsView
.
Custom "Add" commamnd with ItemCommand
Sub CustomerDetailView_ItemCommand(ByVal sender As Object, ByVal e As DetailsViewCommandEventArgs)
' Use the CommandName property to determine which button
' was clicked.
If e.CommandName = "Add" Then
' Do your work here if Add Contact is clicked
End If
End Sub
Easier built in Insert command with ItemInserting
Sub CustomerDetailView_ItemInserting((ByVal sender As Object, ByVal e As DetailsViewInsertEventArgs)
' Access the actual rows with
Some variable1 = e.Values("CustomerID")
Some variable2 = e.Values("CompanyName")
Some variable3 = e.Values("City")
' Do something with them
End Sub
Code front
<asp:DetailsView ID="CustomerDetailView"
DataSourceID="DetailsViewSource"
AutoGenerateRows="false"
DataKeyNames="CustomerID"
AllowPaging="true"
OnItemCommand="CustomerDetailView_ItemCommand"
OnItemInserting="CustomerDetailView_ItemInserting"
OnItemUpdating="CustomerDetailView_ItemUpdating"
runat="server">
<FieldHeaderStyle BackColor="Navy" ForeColor="White" />
<Fields>
<asp:BoundField DataField="CustomerID" HeaderText="Store ID" />
<asp:BoundField DataField="CompanyName" HeaderText="Store Name" />
<asp:BoundField DataField="City" HeaderText="City" />
<asp:TemplateField HeaderText="Name">
<InsertItemTemplate>
<asp:Button ID="btAddContact" runat="server" Text="Add Contact" CommandName="Add" />
Or
<asp:Button ID="btAddContact" runat="server" Text="Add Contact" CommandName="Insert" />
</InsertItemTemplate>
<EditItemTemplate>
<asp:Button ID="btAddContact" runat="server" Text="Save Contact" CommandName="Update" />
</EditItemTemplate>
</asp:TemplateField>
</Fields>
</asp:DetailsView>
Upvotes: 1
Reputation: 22578
You can use any button that implements the IButtonControl
interface. The key is to set the CommandName correctly. Hopefully this will give you the freedom to style your button the way you need to.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.commandname.aspx
You can find a list of the relevant command names here:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listview.itemcommand.aspx
<asp:ImageButton runat="server" ... CommandName="Save" />
<asp:LinkButton runat="server" ... CommandName="Update" />
Upvotes: 0