首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >DetailsView编辑和新建按钮不会更改DetailsView的模式

DetailsView编辑和新建按钮不会更改DetailsView的模式
EN

Stack Overflow用户
提问于 2012-09-26 07:46:34
回答 1查看 4.2K关注 0票数 1

我正在尝试编写一个DetailsView,但是缺少一些代码。你能看看这段代码吗,让我知道我遗漏了什么,因为点击编辑按钮或新建按钮不会改变DetailsView的模式,所以我可以在其中输入数据。

这是DetailsView的代码:

代码语言:javascript
复制
<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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-11-07 06:48:48

尝试向DetailsView添加OnItemCommand事件处理程序。

代码语言:javascript
复制
<asp:DetailsView 
        ID="DetailsViewParentsDetails" 
        OnItemCommand="DetailsViewParentsDetails_ItemCommand"
        runat="server" 
        Height="50px" 
        Width="404px"
        AutoGenerateRows="False">

在代码隐藏文件(.cs)中,您需要添加以下内容:

代码语言:javascript
复制
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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12592690

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档