0

我似乎无法让我的 Listview 更新命令工作。这是我的数据网格:

<asp:ListView ID="lvECOItems" runat="server" 
    EnableViewState="False" DataKeyNames="ID,ECOID"
    onitemupdating="lvECOItems_ItemUpdating" 
    EnableModelValidation="True" 
    onitemcommand="lvECOItems_ItemCommand" 
    onitemcanceling="lvECOItems_ItemCanceling" 
    onitemdeleting="lvECOItems_ItemDeleting" 
    onitemediting="lvECOItems_ItemEditing">
    <AlternatingItemTemplate>
        <div class="listViewRow" style="border-bottom: solid 1px #C0C0C0; background-color: #fff; padding-top: 5px; padding-bottom: 5px;">
            <table style="width: 100%;" class="data-table">
                <tr>
                    <td style="width: 3%;"><asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit"></asp:LinkButton></td>
                    <td style="width: 8%;"><asp:Label ID="DocumentNumber" runat="server" Text='<%# Eval("DocumentNumber") %>'></asp:Label></td>
                    <td style="width: 10%;"><asp:Label ID="title" runat="server" Text='<%# Eval("Title") %>'></asp:Label></td>
                    <td style="width: 2%; text-align:center;"><asp:Label ID="RevisionFrom" runat="server" Text='<%# Eval("RevisionFrom") %>'></asp:Label></td>
                    <td style="width: 2%; text-align:center;"><asp:Label ID="To" runat="server" Text='<%# Eval("To") %>'></asp:Label></td>
                    <td style="width: 1%; text-align:right;"><asp:ImageButton ID="btnDelete" ToolTip="Delete this record" ImageUrl="~/images/delete.jpg" runat="server" OnClientClick="return confirm('Are you sure you want to delete this record?')" CausesValidation="False" CommandName="Delete" Text="Delete"></asp:ImageButton></td>
                </tr>
            </table>
            <asp:Label ID="IDLabel" Visible="false" runat="server" Text='<%# Eval("ID") %>' ></asp:Label>
            <asp:Label ID="ECOIDLabel" Visible="false" runat="server" Text='<%# Eval("ECOID") %>' ></asp:Label>
        </div>
    </AlternatingItemTemplate>
    <LayoutTemplate>
        <div class="listViewHeader">
            <table style="width: 100%; border-bottom: solid 1px #808080; border-top: solid 1px #808080;" class="data-table">
                <tr>
                    <td class="rowheader" style="width: 3%;"></td>
                    <td class="rowheader" style="width: 8%;"></td>
                    <td class="rowheader" style="width: 10%;"></td>
                    <td class="rowheader" style="width: 4%; text-align:center; border-bottom: solid 1px #fff; font-style: italic;" colspan="2">Revision</td>
                </tr>
                <tr>
                    <td class="rowheader" style="width: 3%;"></td>
                    <td class="rowheader" style="width: 8%;">Document #</td>
                    <td class="rowheader" style="width: 10%;">Title</td>
                    <td class="rowheader" style="width: 2%; text-align:center;">From</td>
                    <td class="rowheader" style="width: 2%; text-align:center;">To</td>
                    <td class="rowheader" style="width: 1%;"></td>
                </tr>
            </table>
        </div>
       <asp:PlaceHolder runat="server" ID="itemPlaceholder"></asp:PlaceHolder>
    </LayoutTemplate>
    <EmptyItemTemplate><div></div></EmptyItemTemplate>
    <InsertItemTemplate></InsertItemTemplate>
    <SelectedItemTemplate></SelectedItemTemplate>
    <EmptyDataTemplate>
        <div class="listViewEmptyDataTemplate" style="width: 650px;">
            <span>
                This list does not contain any items.
            </span>
        </div>
    </EmptyDataTemplate>
    <EditItemTemplate>
        <div class="listViewEditRow" style="background-color: #fff; border-top: solid 1px #000; border-bottom: solid 1px #000; padding-top: 10px; padding-left: 10px">
            <asp:Label ID="IDLabel" Visible="false" runat="server" Text='<%# Bind("ID") %>' ></asp:Label>
            <asp:Label ID="ECOIDLabel" Visible="false" runat="server" Text='<%# Bind("ECOID") %>' ></asp:Label>
            <div style="float: left;">
                <table class="data-table">
                    <tr>
                        <td style="width: 175px;">Document Number:</td>
                        <td colspan="3"><asp:Textbox ID="txtDocumentNumber" Width="300px" runat="server" Text='<%# Bind("DocumentNumber") %>'></asp:Textbox></td>
                    </tr>
                    <tr>
                        <td style="width: 175px;">Title:</td>
                        <td colspan="3"><asp:Textbox ID="txttitle" Width="300px" runat="server" Text='<%# Bind("Title") %>'></asp:Textbox></td>
                    </tr>
                    <tr>
                        <td style="width: 175px;">Revision From:</td>
                        <td><asp:Textbox ID="txtRevisionFrom" Width="50px" runat="server" Text='<%# Bind("RevisionFrom") %>'></asp:Textbox></td>
                        <td>To:</td>
                        <td><asp:Textbox ID="txtTo" runat="server" Width="50px" Text='<%# Bind("To") %>'></asp:Textbox></td>
                    </tr>
                </table>
            </div>
            <div class="clear" style="margin-top: 5px;">&nbsp;</div>
            <div style="padding-bottom: 5px;">
                <asp:Button ID="LinkButton4" runat="server" Width="75px" style="margin-right: 10px;" CausesValidation="False" ValidationGroup="vgUpdate" CommandName="Update" Text="Update"></asp:Button>
                <asp:Button ID="LinkButton3" runat="server" Width="75px" ForeColor="#B22222" CausesValidation="False" CommandName="Cancel" Text="Cancel"></asp:Button>
            </div>
            <asp:RequiredFieldValidator ID="vDocumentNumber" runat="server" 
                ControlToValidate="txtDocumentNumber" ValidationGroup="vgUpdate" Display="None" 
                ErrorMessage="Document # is required"></asp:RequiredFieldValidator>
            <asp:ValidationSummary ID="ValidationSummary2" ValidationGroup="vgUpdate" runat="server" ShowMessageBox="True" ShowSummary="False" HeaderText="The record cannot be updated due to the following:" />
        </div>
    </EditItemTemplate>
    <ItemTemplate>
        <div class="listViewRow" style="border-bottom: solid 1px #C0C0C0; background-color: #fff; padding-top: 5px; padding-bottom: 5px;">
            <table style="width: 100%;" class="data-table">
                <tr>
                    <td style="width: 3%;"><asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit"></asp:LinkButton></td>
                    <td style="width: 8%;"><asp:Label ID="DocumentNumber" runat="server" Text='<%# Eval("DocumentNumber") %>'></asp:Label></td>
                    <td style="width: 10%;"><asp:Label ID="title" runat="server" Text='<%# Eval("Title") %>'></asp:Label></td>
                    <td style="width: 2%; text-align:center;"><asp:Label ID="RevisionFrom" runat="server" Text='<%# Eval("RevisionFrom") %>'></asp:Label></td>
                    <td style="width: 2%; text-align:center;"><asp:Label ID="To" runat="server" Text='<%# Eval("To") %>'></asp:Label></td>
                    <td style="width: 1%; text-align:right;"><asp:ImageButton ID="btnDelete" ToolTip="Delete this record" ImageUrl="~/images/delete.jpg" runat="server" OnClientClick="return confirm('Are you sure you want to delete this record?')" CausesValidation="False" CommandName="Delete" Text="Delete"></asp:ImageButton></td>
                </tr>
            </table>
            <asp:Label ID="IDLabel" Visible="false" runat="server" Text='<%# Eval("ID") %>' ></asp:Label>
            <asp:Label ID="ECOIDLabel" Visible="false" runat="server" Text='<%# Eval("ECOID") %>' ></asp:Label>
        </div>
    </ItemTemplate>
</asp:ListView>

和我的ItemCommand活动:

protected void lvECOItems_ItemCommand(object sender, ListViewCommandEventArgs e)
{
    Label ecoItemIDLabel = (Label)e.Item.FindControl("IDLabel");
    string id = ecoItemIDLabel.Text;
    // get the list item id from the ListView
    if (!String.IsNullOrEmpty(this.ecoID) && !String.IsNullOrEmpty(id))
    {
        if (e.CommandName == "Delete")
        {
            ECOItemsRepository ecoItemsRepository = new ECOItemsRepository();
            ECOItems ecoItems = new ECOItems();
            ecoItems.ECOID = this.ecoID;
            ecoItems.ID = Int32.Parse(id);
            ecoItemsRepository.Delete(ecoItems);
        }
        else if (e.CommandName == "Update")
        {
            ECOItemsRepository ecoItemsRepository = new ECOItemsRepository();
            ECOItems ecoItems = new ECOItems();

            Label IDLabel = (Label)e.Item.FindControl("IDLabel");
            TextBox txtDocumentNumber = (TextBox)e.Item.FindControl("txtDocumentNumber");
            TextBox txttitle = (TextBox)e.Item.FindControl("txttitle");
            TextBox txtRevisionFrom = (TextBox)e.Item.FindControl("txtRevisionFrom");
            TextBox txtTo = (TextBox)e.Item.FindControl("txtTo");

            ecoItems.ECOID = this.ecoID;
            ecoItems.ID = Int32.Parse(IDLabel.Text);
            ecoItems.DocumentNumber = txtDocumentNumber.Text;
            ecoItems.title = txttitle.Text;
            ecoItems.RevisionFrom = txtRevisionFrom.Text;
            ecoItems.To = txtTo.Text;
            ecoItemsRepository.Edit(ecoItems);
        }
        BindListView();
    }
}

我什至在尝试这个ItemUpdating事件:

protected void lvECOItems_ItemUpdating(object sender, ListViewUpdateEventArgs e)
{
    ECOItemsRepository ecoItemsRepository = new ECOItemsRepository();
    ECOItems ecoItems = new ECOItems();
    Label IDLabel = (Label)lvECOItems.EditItem.FindControl("IDLabel");
    TextBox txtDocumentNumber = (TextBox)lvECOItems.EditItem.FindControl("txtDocumentNumber");
    TextBox txttitle = (TextBox)lvECOItems.EditItem.FindControl("txttitle");
    TextBox txtRevisionFrom = (TextBox)lvECOItems.EditItem.FindControl("txtRevisionFrom");
    TextBox txtTo = (TextBox)lvECOItems.EditItem.FindControl("txtTo");

    ecoItems.ECOID = this.ecoID;
    ecoItems.ID = Int32.Parse(IDLabel.Text);
    ecoItems.DocumentNumber = txtDocumentNumber.Text;
    ecoItems.title = txttitle.Text;
    ecoItems.RevisionFrom = txtRevisionFrom.Text;
    ecoItems.To = txtTo.Text;
    ecoItemsRepository.Edit(ecoItems);

    this.lvECOItems.EditIndex = -1;
    BindListView();

最后,我的绑定方法:

protected void BindListView()
{
    if (!String.IsNullOrEmpty(this.ecoID))
    {
        ECOItemsRepository ecoItemsRepository = new ECOItemsRepository();
        this.lvECOItems.DataSource = ecoItemsRepository.GetECOItems(ecoID);
        this.lvECOItems.DataBind();
        this.lvECOItems.Enabled = true;
    }
    else
    {
        this.lvECOItems.Enabled = false;
    }
}

当我单击编辑链接时,ListView 会像它应该那样进入编辑模式。

当我单击更新按钮时,EditItemTemplate它不会触发ItemCommand事件。删除和取消都有效。

ItemUpdating事件根本不会触发。

注意:此 ListView 位于 sharepoint webpart 内的用户控件中,因此用户控件是动态创建的。我有一个偷偷摸摸的怀疑它与在回发上绑定网格有关。但如果我不这样做,则列表视图不会填充,除非页面的初始加载。

4

1 回答 1

0

我决定采用不同的方法,因为我无法使更新正常工作。相反,我只使用 Select 命令和 SelectedIndexChanging 事件来填充我当前用于插入的控件集。ItemTemplate 中的链接按钮标记为“编辑”,但使用 CommandName="Select"。不是一个完美的解决方案,但它有效。

于 2013-01-15T22:41:28.563 回答