2012-09-25 84 views
1

我試圖編寫DetailsView但缺少一些編碼。你可以看看這個編碼,並讓我知道我錯過了什麼,因爲單擊編輯按鈕或新建按鈕不會改變DetailsView的模式,所以我可以輸入數據。DetailsView編輯和新按鈕不會改變DetailsView模式

這是在DetailsView編碼:

<asp:UpdatePanel 
    ID="UpdatePanelParentsSummary" 
    runat="server" 
    UpdateMode="Conditional"> 

    <ContentTemplate> 
     <asp:DetailsView 
      ID="DetailsViewParentsDetails" 
      runat="server" 
      Height="50px" 
      Width="404px" 
      AutoGenerateRows="False"> 

      <Fields> 
       <asp:TemplateField ShowHeader="False"> 

        <ItemTemplate> 
         <asp:Button 
          ID="ButtonEdit" 
          runat="server" 
          CausesValidation="False" 
          CommandName="Edit" 
          Text="Edit" /> 

         &nbsp; 
         <asp:Button 
          ID="ButtonNew" 
          runat="server" 
          CausesValidation="False" 
          CommandName="New" 
          Text="New" /> 

         &nbsp; 
         <asp:Button 
          ID="ButtonDelete" 
          runat="server" 
          CausesValidation="False" 
          CommandName="Delete" 
          Text="Delete" /> 

          <AjaxToolKit:ConfirmButtonExtender ID="deleteButtonConfirmation" 
           runat="server" 
           ConfirmText='<%# "You are about to remove: " & vbcr & 
            Eval("FatherName") & vbcr & Eval("MotherName") & "!!!" & 
            vbcrlf & "Are you sure you want to do this?" & vbcrlf & 
            "Clicking the OK button will delete this parent." %>' 
           Enabled="True" 
           TargetControlID="ButtonDelete"> 

          </AjaxToolKit:ConfirmButtonExtender> 
        </ItemTemplate> 

        <EditItemTemplate> 
         <asp:Button 
          ID="ButtonUpdate" 
          runat="server" 
          CausesValidation="True" 
          CommandName="Update" 
          Text="Update" /> 

         &nbsp; 
         <asp:Button 
          ID="ButtonCancelUpdates" 
          runat="server" 
          CausesValidation="False" 
          CommandName="Cancel" 
          Text="Cancel" /> 
        </EditItemTemplate> 

        <InsertItemTemplate> 
         <asp:Button 
          ID="ButtonInsert" 
          runat="server" 
          CausesValidation="True" 
          CommandName="Insert" 
          Text="Insert" /> 

         &nbsp; 
         <asp:Button 
          ID="ButtonCancelInsert" 
          runat="server" 
          CausesValidation="False" 
          CommandName="Cancel" 
          Text="Cancel" /> 
        </InsertItemTemplate> 

       </asp:TemplateField> 

       <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" 
        ReadOnly="True" SortExpression="ID" Visible="False" /> 

       <asp:BoundField 
        DataField="FatherName" 
        HeaderText="Father's Name:"> 

        <ItemStyle ForeColor="Blue" /> 
       </asp:BoundField> 

       <asp:BoundField 
        DataField="MotherName" 
        HeaderText="Mother's Name:"> 

        <ItemStyle ForeColor="Blue" /> 
       </asp:BoundField> 

       <asp:BoundField 
        DataField="FatherOccupation" 
        HeaderText="Father's Occupation:"> 

        <ItemStyle ForeColor="Blue" /> 
       </asp:BoundField> 

       <asp:BoundField 
        DataField="FatherEmploymentPlace" 
        HeaderText="Father's Employment Place:"> 

        <ItemStyle ForeColor="Blue" /> 
       </asp:BoundField> 

       <asp:BoundField 
        DataField="FatherWorkPhone" 
        HeaderText="Father's Work Phone:"> 

        <ItemStyle ForeColor="Blue" /> 
       </asp:BoundField> 

      </Fields> 

      <HeaderTemplate> 
       <%#IIf(Eval("FatherName") = Nothing, 
        "Adding New Student", "Details For: " & Eval("FatherName") & " *** " & Eval("MotherName"))%>    
      </HeaderTemplate> 
     </asp:DetailsView> 
    </ContentTemplate> 
</asp:UpdatePanel> 
+0

刪除的UpdatePanel找到你自己的錯誤 - 更新面板隱藏JavaScript中的錯誤引發您只能查看如果您打開JavaScript控制檯 – Aristos

+0

感謝您的答覆。我嘗試刪除UpdatePanel,但按鈕仍然不會將DetailsView放入編輯或新模式。沒有錯誤信息,只有模式不會像按鈕沒有做任何事情那樣改變。我不知道Java所以JavaScript不會幫助我。你能告訴我我錯過了什麼額外的編碼嗎?謝謝。 –

回答

2

嘗試添加OnItemCommand事件處理程序到您的DetailsView。

<asp:DetailsView 
     ID="DetailsViewParentsDetails" 
     OnItemCommand="DetailsViewParentsDetails_ItemCommand" 
     runat="server" 
     Height="50px" 
     Width="404px" 
     AutoGenerateRows="False"> 

在您的代碼隱藏文件(的.cs),你將需要添加以下內容:

protected void DetailsViewParentsDetails_ItemCommand(object sender, DetailsViewCommandEventArgs e) 
{ 
    if (e.CommandName.Equals("New")) 
    { 
     this.DetailsViewParentsDetails.ChangeMode(DetailsViewMode.Insert); 
     this.DetailsViewParentsDetails.DataBind(); 
    } 
    else if (e.CommandName.Equals("Edit")) 
    { 
     this.DetailsViewParentsDetails.ChangeMode(DetailsViewMode.Edit); 
     this.DetailsViewParentsDetails.DataBind(); 
    } 
} 

MSDN doc on DetailsView.ItemCommand - http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.detailsview.itemcommand(v=vs.100).aspx

相關問題