1

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

4

2 回答 2

1

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

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

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>
于 2012-11-29T18:06:16.447 回答
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" />
于 2012-11-29T18:05:06.547 回答