【问题标题】:DetailsView edit and new buttons don't change the mode of the DetailsViewDetailsView 编辑和新按钮不会改变 DetailsView 的模式
【发布时间】:2012-09-25 23:46:33
【问题描述】:

我正在尝试对 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>

【问题讨论】:

  • 删除 UpdatePanel 以自行查找错误 - 更新面板隐藏 javascript throws 中的错误,只有打开 javascript 控制台才能查看
  • 感谢您的回复。我尝试删除 UpdatePanel,但按钮仍然没有将 DetailsView 置于编辑或新模式。没有错误消息,只有模式永远不会改变,就像按钮没有做任何事情一样。我不懂 Java,所以 Javascript 帮不了我。你能告诉我我错过了哪些额外的编码吗?谢谢。

标签: asp.net vb.net webforms detailsview


【解决方案1】:

尝试将 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-16
    • 1970-01-01
    • 2016-11-22
    • 2011-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多