asp.net中的自定义保存按钮

时间:2012-11-29 17:35:58

标签: asp.net vb.net detailsview

我的问题很简单,如何为详细视图中输入的记录创建自定义保存和更新按钮。我不想使用给定的。非常感谢。

2 个答案:

答案 0 :(得分:1)

你有几个选择。一个是OnItemCommand,你将推出自己的命令。 http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.detailsview.itemcommand.aspx

更简单的方法是使用OnItemInsertedOnItemUpdating事件。只需根据需要发送插入更新命令,您就可以更轻松地使用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

从这些页面中,您基本上要做的是从DetailsView中的按钮捕获命令。

使用ItemCommand

自定义“添加”commamnd
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

使用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

代码前端

<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>

答案 1 :(得分:0)

您可以使用任何实现IButtonControl界面的按钮。关键是正确设置CommandName。希望这会让您自由地按照自己的方式设置按钮的样式。

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.commandname.aspx

您可以在此处找到相关命令名称列表:

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" />