2012-11-29 61 views
1

我的問題很簡單,我如何創建一個自定義的保存和更新按鈕在記錄中輸入一個詳細視圖。我不想使用給定的。非常感謝。自定義保存按鈕在asp.net

+0

我試圖在我的圖像按鈕上添加CommandName。就像插入等,但它不起作用。 – Sonic

回答

0

您可以使用任何實現了接口的按鈕。關鍵是正確設置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" /> 
+0

做這個工作的詳細信息視圖..因爲我想它和頁面刷新,但沒有更新。 – Sonic

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捕獲來自按鈕的命令。

自定義 「添加」 commamnd與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 

更容易建立在Insert命令與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>