2009-06-15 57 views
4

我正在使用listview來顯示項目列表和嵌套列表視圖以顯示每個項目的功能列表。父級和子級列表視圖都需要能夠插入,編輯和刪除操作。它適用於父級列表視圖。但是當我嘗試編輯子項時,編輯按鈕不會將其帶入編輯模式。你能告訴我我的代碼中缺少什麼嗎?如何編輯嵌套列表視圖中的數據

<asp:ListView ID="lvParent" runat="server"     
       OnItemDataBound="lvParent_ItemDataBound" 
       onitemcanceling="lvParent_ItemCanceling" onitemcommand="lvParent_ItemCommand" 
       DataKeyNames="ItemID" onitemdeleting="lvParent_ItemDeleting" 
       oniteminserting="lvParent_ItemInserting" > 
       <LayoutTemplate>           
        <asp:PlaceHolder ID="itemPlaceholder" runat="server"></asp:PlaceHolder> 
        <div align="right"> 
         <asp:Button ID="btnInsert" runat="server" Text="ADD Item" onclick="btnInsert_Click"/> 
        </div> 
       </LayoutTemplate> 
       <ItemTemplate> 
        <table runat="server" cellpadding="0" cellspacing="0" border="0" width="100%"> 
         <tr> 
          <td> 
           <div id="dvDetail"> 
            <span >Description</span> 

            <asp:TextBox ID="txtDescription" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Description") %>' TextMode="MultiLine" ></asp:TextBox> 

           </div>         
           <div id="dvFeature" > 
            <span>Feature List</span>                 
            <asp:ListView ID="lvChild" runat="server" 
             InsertItemPosition="LastItem" 
             DataKeyNames="FeatureID" OnItemCommand="lvChild_ItemCommand" 
             OnItemCanceling="lvChild_ItemCanceling" OnItemDeleting="lvChild_ItemDeleting" 
             OnItemEditing="lvChild_ItemEditing" OnItemInserting="lvChild_ItemInserting" OnItemUpdating="lvChild_ItemUpdating" 
             DataSource='<%# DataBinder.Eval(Container.DataItem, "FeatureList") %>' > 
             <LayoutTemplate> 
              <ul > 
               <asp:PlaceHolder runat="server" ID="itemPlaceHolder" ></asp:PlaceHolder>                                
              </ul> 
             </LayoutTemplate> 
             <ItemTemplate> 
              <li> 
               <span class="dvList"><%# DataBinder.Eval(Container.DataItem, "FeatureTitle")%></span>             

               <div class="dvButton" > 
                <asp:ImageButton ID="btnEdit" runat="server" ImageUrl="/Images/edit_16x16.gif" AlternateText= "Edit" CommandName="Edit" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "FeatureID") %>' Width="12" Height="12" /> 
                <asp:ImageButton ID="btnDelete" runat="server" ImageUrl="/Images/delete_16x16.gif" AlternateText= "Delete" CommandName="Delete" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "FeatureID") %>' Width="12" Height="12" /> 

               </div> 

              </li> 

             </ItemTemplate> 
             <EditItemTemplate> 
              <li> 
               <asp:TextBox ID="txtFeature" Text='<%# DataBinder.Eval(Container.DataItem, "FeatureTitle")%>' runat="server"></asp:TextBox> 

               <div class="dvButton"> 
                <asp:ImageButton ID="btnUpdate" runat="server" ImageUrl="/Images/ok_16x16.gif" AlternateText= "Update" CommandName="Update" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "FeatureID") %>' Width="12" Height="12" /> 
                <asp:ImageButton ID="btnCancel" runat="server" ImageUrl="/Images/delete_16x16.gif" AlternateText= "Cancel" CommandName="Cancel" Width="12" Height="12" CausesValidation="false" /> 

               </div> 

              </li> 
             </EditItemTemplate> 
             <InsertItemTemplate>            
               <asp:TextBox ID="txtFeature" runat="server"></asp:TextBox> 

               <div class="dvButton"> 
                <asp:ImageButton ID="btnInsert" runat="server" ImageUrl="/Images/ok_16x16.gif" AlternateText= "Insert" CommandName="Insert" Width="12" Height="12" /> 
                <asp:ImageButton ID="btnCancel" runat="server" ImageUrl="/Images/delete_16x16.gif" AlternateText= "Cancel" CommandName="Cancel" Width="12" Height="12" CausesValidation="false" /> 

               </div> 

             </InsertItemTemplate> 
            </asp:ListView> 
           </div>         
          </td> 

         </tr> 
         <tr> 
          <td align="right"> 
           <div id="dvButton" > 
            <asp:Button ID="btnSave" runat="server" Text="Save" 
             CommandName="Save" 
             CommandArgument='<%# DataBinder.Eval(Container.DataItem, "ItemID") %>' /> 
            <asp:Button ID="btnDelete" runat="server" Text="Delete" CssClass="Cancel" 
             CommandName="Delete" 
             CommandArgument='<%# DataBinder.Eval(Container.DataItem, "ItemID") %>' /> 
           </div> 
          </td> 

         </tr>     
        </table>     
       </ItemTemplate>     

      </asp:ListView> 

代碼背後:

protected void Page_Load(object sender, EventArgs e) 
     {  
      if (Page.IsPostBack == false) 
      {     
       BindData(); 
      }   

     } 

private void BindData() 
     { 
      MyDataContext data = new MyDataContext(); 
      var result = from itm in data.ItemLists 
         where itm.ItemID == iItemID 

         select new 
         { 
          itm.ItemID, 
          itm.Description,        
          FeatureList = itm.Features 
         }; 

      lvParent.DataSource = result; 
      lvParent.DataBind(); 

     } 

protected void lvChild_ItemEditing(object sender, ListViewEditEventArgs e) 
     { 
      ListView lvChild = sender as ListView;    

      lvChild.EditIndex = e.NewEditIndex; 

      lvChild.DataBind(); 

     } 

編輯:

protected void lvChild_ItemEditing(object sender, ListViewEditEventArgs e) 
      { 
       ListView lvChild = sender as ListView;    

       lvChild.EditIndex = e.NewEditIndex; 

       lvChild.DataBind(); 

      } 

如果我使用 「lvChild.DataBind()」 中的 'ItemEditing' 事件,子項的總榜單消失如果我點擊'編輯'

protected void lvChild_ItemEditing(object sender, ListViewEditEventArgs e) 
      { 
       ListView lvChild = sender as ListView;    

       lvChild.EditIndex = e.NewEditIndex;    


      } 

如果我得到在ItemEditing事件中刪除'lvChild.Databind'後,點擊兩次'編輯'按鈕後它會進入編輯模式。雖然它顯示EditItemTemplate的文本框控件,但它顯示爲空白文本框(不綁定現有的值進行編輯)。

回答

2

這是一個有趣的問題。幾乎可以肯定是一個數據綁定問題。爲了進入編輯模式,你必須做兩兩件事:

1)設置EditIndex
2)調用DataBind()

在嵌套中繼器的情況下,雖然...什麼時候渲染被調用?我懷疑你必須在PARENT上調用DataBind()才能正確渲染所有東西。因此,您可能必須重新設置EditIndex,因爲您正在重新綁定父項。

編輯: 行...我只是試圖用嵌套的GridView,我沒有DataBind()父,以獲得子網格進入編輯模式。現在我必須冷靜地回答自己的答案。 :|

+0

讓你爲之驕傲的人:) – keni 2014-01-10 11:11:55

0

希望能爲某人服務。

這裏是我的代碼來獲取工作:

1)我有一個ListView至極編輯時保持用戶控制。該用戶cotnrol具有自身內部

<asp:ListView runat=server ID=C_LV_MyObjects DataKeyNames="Id" 
    OnItemDataBound=DataBoundMyObjects OnItemEditing=ItemEditing 
    > 
<LayoutTemplate> 
      <table runat=server id="itemPlaceholderContainer"> 
       <tr> 
        <th> 
         Description 
        </th> 
       </tr> 
       <tr runat="server" id="itemPlaceholder"> 
       </tr> 
      </table> 
     </LayoutTemplate> 
     <ItemTemplate> 
      <tr> 
       <td> 
        text... 
       </td> 
       <td> 
        <asp:LinkButton runat="server" CommandName="Edit" Text="Edit"></asp:LinkButton> 
       </td> 
       <td> 
        <asp:LinkButton runat="server" CommandName="Delete" Text="Delete"></asp:LinkButton> 
       </td> 
     </ItemTemplate> 
     <EditItemTemplate> 
      <tr> 
       <td colspan=3> 
        <MyTag:MyUC ID=C_UC_MyUserControl runat=server 
           OnEditing=MyObjectEditing 
           /> 
       </td> 
      </tr> 
     </EditItemTemplate> 
     <EmptyDataTemplate> 
      No results found! 
     </EmptyDataTemplate> 
    </asp:ListView> 

列表視圖代碼C#這個列表視圖如下:

public int EditIndexComposition; 

protected void ItemEditing(object sender, ListViewEditEventArgs e) 
     { 
      C_LV_MyObjects.EditIndex = e.NewEditIndex; 
      C_LV_MyObjects.DataBind(); 
     } 

     protected void MyObjectEditing(object sender, EventArgs e) 
     { 
      ListViewEditEventArgs MyEvent = (ListViewEditEventArgs)e; 
      if (MyEvent != null) 
       EditIndexComposition= MyEvent.NewEditIndex; 

      C_LV_MyObjects.DataBind(); 

     } 

     protected void DataBoundMyObjects(object sender, ListViewItemEventArgs e) 
     { 
      MyUC uc = (MyUC)e.Item.FindControl("C_UC_MyUserControl"); 

      if (uc!=null) 
      { 
       uc.EditIndex = EditIndexComposition; 
       ListViewDataItem dataItem = (ListViewDataItem)e.Item; 
       MyObject obj= (MyObject)dataItem.DataItem; 
       uc.DataSource=Myservice.GetDatasource(obj.Id); 
       uc.DataBind(); 

      } 
     } 

和我的用戶的代碼如下:

<asp:PlaceHolder runat="server" ID="C_PH_ObjComposition"> 
    <asp:ListView runat="server" ID="C_LV_AppaltatoreComposizione" DataSource="<% # DataSource %>" 
     DataKeyNames="Id" OnItemEditing="ItemEditing"> 

      etc... 

<ItemTemplate> 
      <tr> 
       <td> 
        <asp:LinkButton runat="server" CommandName="Edit" Text="Edit"></asp:LinkButton> 
       </td> 
      </tr> 
     </ItemTemplate> 
     <EditItemTemplate> 
      <tr> 
       <td> 
        Edit Mode 
       </td> 
      </tr> 
     </EditItemTemplate> 
    </asp:ListView> 
</asp:PlaceHolder> 

與以下代碼c#:

public int EditIndex 
     { 
      get {return C_LV_ObjComposition.EditIndex;} 
      set { C_LV_ObjComposition.EditIndex=value;} 
     } 

public event EventHandler Editing; 

protected void ItemEditing(object sender, ListViewEditEventArgs e) 
     { 
      C_LV_ObjComposition.EditIndex = e.NewEditIndex; 

      if (Editing != null) 
       Editing(this, e); 

     } 

當點擊innerlistview的編輯按鈕時,我們存儲被點擊的索引,並在第一個容器用戶控件中觸發一個函數。該函數將存儲全局值索引cliked並觸發outter列表的數據綁定。這樣做我們可以獲得onitemdatabound,它將以適當的值重新創建usercontrol,然後可以在usercontrol的數據綁定之前分配編輯行的索引。

這一切,如果你有任何問題,請隨時回答..

的Ciao!