2016-08-12 112 views
0

我的網站在c#&是完全動態的。菜單欄也是動態的。每個導航菜單都有子菜單..因爲菜單需要從數據庫構建我已經使用Listview控件。在我的菜單網站中,我需要在一個父級Listview中使用2個子Listview。所以它看起來像這樣。嵌套的ListView無法從父列表視圖獲取值

父菜單列表視圖有子菜單列表視圖&子菜單列表視圖,有一個孩子在列表視圖,我需要表現出任何一個與產品相關的父列表視圖類別..

我的子菜單已妥善建造,但裏面如果我嘗試綁定另一個列表視圖,然後它顯示我的錯誤(對象引用沒有設置爲對象的一個​​實例。)然後,我嘗試使用手動SQL查詢,而沒有從ItemDataBound定義的where子句,然後它也顯示相同的錯誤。

我的菜單看起來像 enter image description here

<asp:ListView ID="mainMenu" runat="server" DataKeyNames="name" OnItemDataBound="OnItemDataBound"> 
    <ItemTemplate> 
      <li><asp:HyperLink ID="mainLinks" runat="server" NavigateUrl='<%# Eval("name", "~/{0}") %>' Text='<%# Eval("name") %>'></asp:HyperLink> 

       <ul class="super-child"> 
        <asp:ListView ID="childMenu" runat="server" DataKeyNames="name"> 
         <ItemTemplate> 
          <div class="super-child-inner"> 
           <div class="list-child"> 
           <li><asp:HyperLink ID="cat3" runat="server" NavigateUrl='<%# Eval("category") & Eval("name", "/{0}") %>' Text='<%# Eval("name") %>'></asp:HyperLink></li> 
          </div> 

          <div class="recommended"> 
          <asp:ListView ID="recommendedProducts" runat="server"> 
           <ItemTemplate> 
            <div class="rec-img"> 
              <asp:Image ID="recImg" runat="server" ImageUrl='<%# Eval("image") %>' /> 
            </div> 
            <p class="rec-name"><asp:HyperLink ID="recLink" runat="server" NavigateUrl='<%# Eval("category") & Eval("name", "/{0}") %>' Text='<%# Eval("name") %>'></asp:HyperLink></p> 
           </ItemTemplate> 
           </asp:ListView> 
          </div> 
         </div> 
        </ItemTemplate> 
       </asp:ListView> 
      </ul> 
     </li> 
    </ItemTemplate> 
</asp:ListView> 

的ItemDataBound代碼

protected void onItemDataBound(object sender, ListViewItemEventArgs e) 
{ 
    if (e.Item.ItemType == ListViewItemType.DataItem) { 
     ListViewDataItem itm = (ListViewDataItem)e.Item; 
     string name = mainMenu.DataKeys(itm.DataItemIndex)("name"); 
     ListView childMenu = e.Item.FindControl("childMenu") as ListView; 
     ListView recommendedProducts = e.Item.FindControl("recommendedProducts") as ListView; 
     string constr = ConfigurationManager.ConnectionStrings("conio").ConnectionString; 
     using (MySqlConnection con = new MySqlConnection(constr)) { 
      using (MySqlCommand cmd = new MySqlCommand()) { 
       cmd.CommandText = "SELECT * FROM product_categories WHERE category = '" + name + "' and status = 'active'"; 
       cmd.Connection = con; 
       using (MySqlDataAdapter sda = new MySqlDataAdapter(cmd)) { 
        DataTable dt = new DataTable(); 
        sda.Fill(dt); 
        childMenu.DataSource = dt; 
        childMenu.DataBind(); 
       } 
      } 
     } 

     try { 
      string constr2 = ConfigurationManager.ConnectionStrings("conio").ConnectionString; 
      using (MySqlConnection con = new MySqlConnection(constr2)) { 
       using (MySqlCommand cmd = new MySqlCommand()) { 
        cmd.CommandText = "SELECT ID,slug,product_name,image FROM products WHERE (category Like @category) and status = 'active' Limit 1"; 
        cmd.Connection = con; 
        cmd.Parameters.AddWithValue("@categoty", "%" + name + "%"); 
        using (MySqlDataAdapter sda = new MySqlDataAdapter(cmd)) { 
         DataTable dt = new DataTable(); 
         sda.Fill(dt); 
         recommendedProducts.DataSource = dt; 
         recommendedProducts.DataBind(); 
        } 
       } 
      } 
     } catch (Exception ex) { 
      Response.Write(ex); 
     } 
    } 
} 

(在子菜單,在這裏我想顯示力量的銀行類的一個產品空白空間(右側))更新(例外)

System.NullReferenceException:未設置對象引用t o一個對象的實例。在E:\ MY WEB \ brandstik.com1 \ MasterPage.master.vb中的MasterPage.onItemDataBound(對象發件人,ListViewItemEventArgs e)中:line 79System.NullReferenceException:未將對象引用設置爲對象的實例。在E:\ MY WEB \ brandstik.com1 \ MasterPage.master.vb中的MasterPage.onItemDataBound(對象發件人,ListViewItemEventArgs e)中:line 79System.NullReferenceException:未將對象引用設置爲對象的實例。在E:\ MY WEB \ brandstik.com1 \ MasterPage.master.vb中的MasterPage.onItemDataBound(對象發件人,ListViewItemEventArgs e)中:line 79System.NullReferenceException:未將對象引用設置爲對象的實例。在E:\ MY WEB \ brandstik.com1 \ MasterPage.master.vb中的MasterPage.onItemDataBound(對象發件人,ListViewItemEventArgs e)中:line 79System.NullReferenceException:未將對象引用設置爲對象的實例。在E:\ MY WEB \ brandstik.com1 \ MasterPage.master.vb中的MasterPage.onItemDataBound(對象發件人,ListViewItemEventArgs e)中:line 79System.NullReferenceException:未將對象引用設置爲對象的實例。在E:\ MY WEB \ brandstik.com1 \ MasterPage.master.vb中的MasterPage.onItemDataBound(對象發件人,ListViewItemEventArgs e)中:line 79System.NullReferenceException:未將對象引用設置爲對象的實例。在E:\ MY WEB \ brandstik.com1 \ MasterPage.master.vb中的MasterPage.onItemDataBound(對象發件人,ListViewItemEventArgs e)中:line 79System.NullReferenceException:未將對象引用設置爲對象的實例。在E:\ MY WEB \ brandstik.com1 \ MasterPage.master.vb中的MasterPage.onItemDataBound(對象發件人,ListViewItemEventArgs e)中:line 79System.NullReferenceException:未將對象引用設置爲對象的實例。在E:\ MY WEB \ brandstik.com1 \ MasterPage.master.vb中的MasterPage.onItemDataBound(對象發件人,ListViewItemEventArgs e)中:line 79System.NullReferenceException:未將對象引用設置爲對象的實例。在MasterPage.onItemDataBound(對象發件人,ListViewItemEventArgs e)如E:\ MY WEB \ brandstik.com1 \ MasterPage.master.vb:線79

+0

我認爲名稱存在問題 - 在您的DOM ListView中,ID是= recommendedProduct,但您試圖將數據綁定到ListView recommendedProducts,因此請先檢查它。 – SilentCoder

+0

@codelahiru Ys您是對的我在那裏犯了錯誤,但我用正確的名稱改了它,但問題仍然是一樣的 – SUN

+0

除了'對象引用沒有設置爲對象的實例嗎?這意味着在將數據綁定到該實例之前,您並未從實例創建任何新對象。 – SilentCoder

回答

0

有爲了解決該問題可以理解兩個主要概念。

  • 範圍ID的值
  • 定時初始化

的關於ID值的範圍:

ItemTemplate內的一個元素的ID將只有這個範圍內的可用項目。否則,多個項目將生成具有重複ID值的元素。有了childMenu中的兩個項目,您希望哪一個可以通過FindControl返回?

關於時間,ListView的嵌套項目將僅在DataSource被分配,而DataBind被執行後被創建。這意味着,在執行childMenu.DataSource = dt; childMenu.DataBind();之前,您不能指望recommendedProducts存在。

在下面的例子中,你複雜的數據邏輯取代了簡單而有效的調試值:

protected void Page_Load(object sender, EventArgs e) 
{ 
    mainMenu.DataSource = new[] { "M1", "M2" }; 
    mainMenu.DataBind(); 
} 

protected void mainMenu_ItemDataBound(object sender, ListViewItemEventArgs e) 
{ 
    var childMenu = e.Item.FindControl("childMenu") as ListView; 

    // will always be null, because ID scope is not correct 
    ListView recommendedProducts1 = e.Item.FindControl("recommendedProducts") as ListView; 

    // query the correct ID scope, but Items will be empty 
    foreach (var item in childMenu.Items) 
    { 
     var recommendedProducts2 = item.FindControl("recommendedProducts") as ListView; 
    } 

    // initialize sub-items 
    childMenu.DataSource = new object[] { "C1", "C2" }; 
    childMenu.DataBind(); 

    // query the correct ID scope, with items 
    foreach (var item in childMenu.Items) 
    { 
     // finally, recommendedProducts for each childmenu-item within the currently processed childmenu is accessible 
     var recommendedProducts3 = item.FindControl("recommendedProducts") as ListView; 
     recommendedProducts3.DataSource = new[] { "Best" }; 
     recommendedProducts3.DataBind(); 
    } 
} 

我的測試ASPX爲了完整起見:

<asp:ListView ID="mainMenu" runat="server" OnItemDataBound="mainMenu_ItemDataBound"> 
    <LayoutTemplate> 
     <ul> 
      <li id="itemPlaceholder" runat="server"/> 
     </ul> 
    </LayoutTemplate> 
    <ItemTemplate> 
     <li runat="server"> 
      <%# Container.DataItem %> 
      <asp:ListView ID="childMenu" runat="server"> 
       <LayoutTemplate> 
        <ul> 
         <li id="itemPlaceholder" runat="server"/> 
        </ul> 
       </LayoutTemplate> 
       <ItemTemplate> 
        <li runat="server"> 
         <%# Container.DataItem %> 
         <div class="recommended"> 
          <asp:ListView ID="recommendedProducts" runat="server"> 
           <ItemTemplate> 
            <div style="background-color:green"> 
             <%# Container.DataItem %> 
            </div> 
           </ItemTemplate> 
          </asp:ListView> 
         </div> 
        </li> 
       </ItemTemplate> 
      </asp:ListView> 
     </li> 
    </ItemTemplate> 
</asp:ListView> 

希望這有助於理解,當以及如何訪問您的控件。

+0

好的。我改變了,但仍然沒有解決問題,並得到相同的錯誤.. – SUN

+0

@SUN現在我很好奇...這是你的代碼或問題的問題?如果這是一個代碼問題:你在評論中說你已經調試過代碼 - 應該很明顯的是'recommendedProducts'爲null。現在是什麼? – grek40

+0

在我的更新中,從代碼更改ListView名稱後發生完全相同的錯誤,但我不明白爲什麼錯誤仍然保持不變。 – SUN