2013-01-10 23 views
0

我似乎無法讓我的Listview更新命令工作。這是我的DataGrid:ItemCommand更新和ItemUpdating不能在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中的用戶控件中,因此用戶控件是動態創建的。我有一個偷偷摸摸的疑問,它與綁定網格的回發有關。但如果我不這樣做,listview不會填充,除非頁面的初始加載。

回答

0

我決定採取不同的方法,因爲我無法使更新工作。相反,我只是使用Select命令和SelectedIndexChanging事件來填充我當前用於插入的控件集。 ItemTemplate中的鏈接按鈕標記爲「編輯」,但使用CommandName =「選擇」。不完美的解決方案,但它的工作原理