开发者

How to delete subsequent pages in a FormView control in C#

I am using a FormView control and I have been successful in getting pretty much everything working except delete. The situation is thus:

I can delete any record that the formview is showing as long as the web page loads that record first when I first open the webpage. If I open the web page then click page 2,3,4 or so on, and then try to delete one of those records, or even if I go back to the first page and try to delete that one, I get an object null reference. The reference refers to the ID that I'm using as a data key and required parameter to delete the record. It seems to me that the ID is not getting set in the bind(<'%#GoalID %>') when I click a different page.

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" 
              开发者_如何学Python                                  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>


I figured out the answer to my question. Rather than using e.Keys["GoalID"].ToString(); I tried ((Label)goalsFormView.FindControl("GoalID")).Text.

Although I figured this out, I still welcome answers as to why e.Keys... didn't work. Thank you.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜