Emad-ud-deen
Emad-ud-deen

Reputation: 4864

DetailsView edit and new buttons don't change the mode of the DetailsView

I'm trying to code a DetailsView but have some coding missing. Can you look at this coding and let me know what I'm missing because clicking on the Edit button or the New button will not change the mode of the DetailsView so I can enter data into it.

This is the coding for the 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>

Upvotes: 1

Views: 4341

Answers (1)

B-K
B-K

Reputation: 378

Try adding the OnItemCommand event handler to your DetailsView.

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

In your code-behind file (.cs), you will need to add the following:

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

Upvotes: 2

Related Questions