2011-04-20 51 views
0

我正在使用FormView控件,並且我已經成功地獲得了除刪除以外的所有工作。因此,情況如下:如何在C#中的FormView控件中刪除後續頁面

只要網頁首次打開網頁時加載該記錄,我就可以刪除窗體視圖顯示的任何記錄。如果我打開網頁,然後點擊頁面2,3,4等等,然後嘗試刪除其中一個記錄,或者即使我回到第一頁並嘗試刪除那個,我得到一個對象null參考。引用指的是我用作數據鍵的ID和用於刪除記錄的必需參數。在我看來,當我點擊不同的頁面時,綁定中沒有設置ID(<'%#GoalID%>')。

protected void goalsFormView_ItemDeleting(object sender, FormViewDeleteEventArgs e) 
    { 
     string strGoalID = e.Keys["GoalID"].ToString(); 
     Guid goalID = new Guid(strGoalID); 
     string strTaskSetID = e.Keys["TaskSetID"].ToString(); 
     Guid taskSetID = new Guid(strTaskSetID); 
     DeleteRecord(goalID, taskSetID); //call delete method 

     BindFormView(); //rebind details view to reflect changes made 
    } 


<asp:FormView ID="goalsFormView" runat="server" AllowPaging="True" 
               CssClass="formView" DataKeyNames="GoalID, TaskSetID" 
               EmptyDataText="There is no data to display" HeaderText="Weekly Goals Form" 
               OnItemDeleting="goalsFormView_ItemDeleting" 
               OnItemUpdating="goalsFormView_ItemUpdating" 
               OnModeChanging="goalsFormView_ModeChanging" 
               OnPageIndexChanging="goalsFormView_PageIndexChanging"> 
               <HeaderStyle BackColor="#1E3F7F" ForeColor="White" /> 
               <ItemTemplate> 
                <table class="formView" style="width: 800px"> 
                 <tr> 
                  <td> 
                   Goal 
                  </td> 
                  <td style="width: 520px"> 
                   <asp:Label ID="weeklyGoalLabel" runat="server" Text='<%# Eval("WeeklyGoal") %>'></asp:Label> 
                  </td> 
                  <td> 
                   &nbsp; 
                  </td> 
                 </tr> 
                 <tr> 
                  <td> 
                   &nbsp; 
                  </td> 
                  <td style="width: 520px"> 
                   &nbsp; 
                  </td> 
                  <td> 
                   &nbsp; 
                  </td> 
                 </tr> 
                 <tr> 
                  <td> 
                   Tasks 
                  </td> 
                  <td style="width: 520px"> 
                   &nbsp; 
                  </td> 
                  <td> 
                   Estimated Hours 
                  </td> 
                 </tr> 
                 <tr> 
                  <td> 
                   &nbsp; 
                  </td> 
                  <td style="width: 520px"> 
                   &nbsp; 
                  </td> 
                  <td> 
                   &nbsp; 
                  </td> 
                 </tr> 
                 <tr> 
                  <td> 
                   &nbsp;1. 
                  </td> 
                  <td style="width: 520px"> 
                   <asp:Label ID="taskLabel1" runat="server" Text='<%# Eval("Task1") %>'></asp:Label> 
                  </td> 
                  <td> 
                   <asp:Label ID="estHours1" runat="server" CssClass="hours" 
                    Text='<%# Eval("T1EstimatedHours") %>'></asp:Label> 
                  </td> 
                 </tr> 
                 <tr> 
                  <td> 
                   &nbsp;2. 
                  </td> 
                  <td style="width: 520px"> 
                   <asp:Label ID="taskLabel2" runat="server" Text='<%# Eval("Task2") %>'></asp:Label> 
                  </td> 
                  <td> 
                   <asp:Label ID="estHours2" runat="server" CssClass="hours" 
                    Text='<%# Eval("T2EstimatedHours") %>'></asp:Label> 
                  </td> 
                 </tr> 
                 <tr> 
                  <td> 
                   &nbsp;3. 
                  </td> 
                  <td style="width: 520px"> 
                   <asp:Label ID="taskLabel3" runat="server" Text='<%# Eval("Task3") %>'></asp:Label> 
                  </td> 
                  <td> 
                   <asp:Label ID="estHours3" runat="server" CssClass="hours" 
                    Text='<%# Eval("T3EstimatedHours") %>'></asp:Label> 
                  </td> 
                 </tr> 
                 <tr> 
                  <td> 
                   &nbsp;4. 
                  </td> 
                  <td style="width: 520px"> 
                   <asp:Label ID="taskLabel4" runat="server" Text='<%# Eval("Task4") %>'></asp:Label> 
                  </td> 
                  <td> 
                   <asp:Label ID="estHours4" runat="server" CssClass="hours" 
                    Text='<%# Eval("T4EstimatedHours") %>'></asp:Label> 
                  </td> 
                 </tr> 
                 <tr> 
                  <td> 
                   &nbsp;5. 
                  </td> 
                  <td style="width: 520px"> 
                   <asp:Label ID="taskLabel5" runat="server" Text='<%# Eval("Task5") %>'></asp:Label> 
                  </td> 
                  <td> 
                   <asp:Label ID="estHours5" runat="server" CssClass="hours" 
                    Text='<%# Eval("T5EstimatedHours") %>'></asp:Label> 
                  </td> 
                 </tr> 
                 <tr> 
                  <td> 
                   &nbsp; 
                  </td> 
                  <td style="width: 520px"> 
                   <asp:LinkButton ID="editButton" runat="server" CommandName="Edit">Edit</asp:LinkButton> 
                   &nbsp;<asp:LinkButton ID="deleteButton" runat="server" CommandName="Delete">Delete</asp:LinkButton> 
                  </td> 
                  <td> 
                   &nbsp; 
                  </td> 
                 </tr> 
                 <tr> 
                  <td class="invisibleRows" style="width: 40px"> 
                   Goal ID 
                  </td> 
                  <td class="invisibleRows" style="width: 520px"> 
                   <asp:Label ID="GoalID" runat="server" Text='<%# Bind("GoalID") %>'></asp:Label> 
                  </td> 
                  <td class="invisibleRows"> 
                   &nbsp; 
                  </td> 
                 </tr> 
                 <tr> 
                  <td class="invisibleRows"> 
                   Task Set ID 
                  </td> 
                  <td class="invisibleRows" style="width: 520px"> 
                   <asp:Label ID="TaskSetID" runat="server" Text='<%# Bind("TaskSetID") %>'></asp:Label> 
                  </td> 
                  <td class="invisibleRows"> 
                   &nbsp; 
                  </td> 
                 </tr> 
                </table> 
               </ItemTemplate> 
               <EditItemTemplate> 
                <table class="formView" style="width: 800px"> 
                 <tr> 
                  <td> 
                   Goal 
                  </td> 
                  <td style="width: 520px"> 
                   <asp:TextBox ID="WeeklyGoalUpdateBox" runat="server" CssClass="goalsandtasks" 
                    Text='<%# Bind("WeeklyGoal") %>'></asp:TextBox> 
                  </td> 
                  <td> 
                   &nbsp; 
                  </td> 
                 </tr> 
                 <tr> 
                  <td> 
                   &nbsp; 
                  </td> 
                  <td style="width: 520px"> 
                   &nbsp; 
                  </td> 
                  <td> 
                   &nbsp; 
                  </td> 
                 </tr> 
                 <tr> 
                  <td> 
                   Tasks 
                  </td> 
                  <td style="width: 520px"> 
                   &nbsp; 
                  </td> 
                  <td> 
                   Estimated Hours 
                  </td> 
                 </tr> 
                 <tr> 
                  <td> 
                   &nbsp; 
                  </td> 
                  <td style="width: 520px"> 
                   &nbsp; 
                  </td> 
                  <td> 
                   &nbsp; 
                  </td> 
                 </tr> 
                 <tr> 
                  <td> 
                   &nbsp;1. 
                  </td> 
                  <td style="width: 520px"> 
                   <asp:TextBox ID="Task1UpdateBox" runat="server" CssClass="goalsandtasks" 
                    Text='<%# Bind("Task1") %>'></asp:TextBox> 
                  </td> 
                  <td> 
                   <asp:TextBox ID="estHours1UpdateBox" runat="server" CssClass="hours" 
                    Text='<%# Bind("T1EstimatedHours") %>'></asp:TextBox> 
                  </td> 
                 </tr> 
                 <tr> 
                  <td> 
                   &nbsp;2. 
                  </td> 
                  <td style="width: 520px"> 
                   <asp:TextBox ID="Task2UpdateBox" runat="server" CssClass="goalsandtasks" 
                    Text='<%# Bind("Task2") %>'></asp:TextBox> 
                  </td> 
                  <td> 
                   <asp:TextBox ID="estHours2UpdateBox" runat="server" CssClass="hours" 
                    Text='<%# Bind("T2EstimatedHours") %>'></asp:TextBox> 
                  </td> 
                 </tr> 
                 <tr> 
                  <td> 
                   &nbsp;3. 
                  </td> 
                  <td style="width: 520px"> 
                   <asp:TextBox ID="Task3UpdateBox" runat="server" CssClass="goalsandtasks" 
                    Text='<%# Bind("Task3") %>'></asp:TextBox> 
                  </td> 
                  <td> 
                   <asp:TextBox ID="estHours3UpdateBox" runat="server" CssClass="hours" 
                    Text='<%# Bind("T3EstimatedHours") %>'></asp:TextBox> 
                  </td> 
                 </tr> 
                 <tr> 
                  <td> 
                   &nbsp;4. 
                  </td> 
                  <td style="width: 520px"> 
                   <asp:TextBox ID="Task4UpdateBox" runat="server" CssClass="goalsandtasks" 
                    Text='<%# Bind("Task4") %>'></asp:TextBox> 
                  </td> 
                  <td> 
                   <asp:TextBox ID="estHours4UpdateBox" runat="server" CssClass="hours" 
                    Text='<%# Bind("T4EstimatedHours") %>'></asp:TextBox> 
                  </td> 
                 </tr> 
                 <tr> 
                  <td> 
                   &nbsp;5. 
                  </td> 
                  <td style="width: 520px"> 
                   <asp:TextBox ID="Task5UpdateBox" runat="server" CssClass="goalsandtasks" 
                    Text='<%# Bind("Task5") %>'></asp:TextBox> 
                  </td> 
                  <td> 
                   <asp:TextBox ID="estHours5UpdateBox" runat="server" CssClass="hours" 
                    Text='<%# Bind("T5EstimatedHours") %>'></asp:TextBox> 
                  </td> 
                 </tr> 
                 <tr> 
                  <td> 
                   &nbsp; 
                  </td> 
                  <td style="width: 520px"> 
                   <asp:LinkButton ID="updateButton" runat="server" CommandName="Update">Update</asp:LinkButton> 
                   &nbsp;<asp:LinkButton ID="cancelButton" runat="server" CommandName="Cancel">Cancel</asp:LinkButton> 
                  </td> 
                  <td> 
                   &nbsp; 
                  </td> 
                 </tr> 
                 <tr> 
                  <td class="invisibleRows" style="width: 40px"> 
                   Goal ID 
                  </td> 
                  <td class="invisibleRows" style="width: 520px"> 
                   <asp:Label ID="GoalIDUpdateLabel" runat="server" Text='<%# Bind("GoalID") %>'></asp:Label> 
                  </td> 
                  <td class="invisibleRows"> 
                   &nbsp; 
                  </td> 
                 </tr> 
                 <tr> 
                  <td class="invisibleRows"> 
                   Task Set ID 
                  </td> 
                  <td class="invisibleRows" style="width: 520px"> 
                   <asp:Label ID="TaskSetIDUpdateLabel" runat="server" 
                    Text='<%# Bind("TaskSetID") %>'></asp:Label> 
                  </td> 
                  <td class="invisibleRows"> 
                   &nbsp; 
                  </td> 
                 </tr> 
                </table> 
               </EditItemTemplate>              
              </asp:FormView> 

回答

0

我想出了我的問題的答案。而不是使用e.Keys [「GoalID」] ToString(); 我試過((Label)goalsFormView.FindControl(「GoalID」)).Text。

雖然我明白了這一點,但我仍然歡迎關於e.Keys爲什麼不起作用的答案。謝謝。

+0

如果回答您的問題,請將其標記爲已接受。這將您的問題從未回答的問題列表中解決。 – 2011-04-21 05:49:07

相關問題