Click here to Skip to main content
16,013,207 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
.aspx


XML
<asp:ListView ID="productListView" runat="server" DataKeyNames="ProductID" ItemPlaceholderID="_placeholder" OnItemDataBound="productListView_ItemDataBound"
 OnItemEditing="productListView_ItemEditing" OnItemCanceling="productListView_ItemCanceling" OnItemUpdating="productListView_ItemUpdating"
 OnItemInserting="productListView_ItemInserting" InsertItemPosition="LastItem" OnItemDeleting="productListView_ItemDeleting">

       <LayoutTemplate>

            <table id="headerTable" runat="server" >
            <tr id="Tr1" runat="server">
            <th id="Th11" runat="server">Image</th>
            <th id="Th3" runat="server">Product Name</th>
            <th id="Th5" runat="server">Category</th>
            <th id="Th1" runat="server">Description</th>
            <th id="Th10" runat="server">Show In ProductList</th>

            </tr>
            <tr id="_placeholder" runat="server"></tr>
            </table>

        </LayoutTemplate>

        <ItemTemplate>
                <tr runat="server" id="_placeholder" >
                    <td>
                    <asp:Image ID="imgProduct" runat="server" />
                    </td>
                    <td>
                    <asp:Label ID="NameLabel" runat="server" Text="<%# Bind('Name') %>"></asp:Label>
                    </td>

                    <td>
                    <asp:Label ID="CategoryNameLabel" runat="server" Text="<%# Bind('ProductCategory.CategoryName') %>"></asp:Label>
                    </td>

                    <td>
                    <asp:Label ID="DescriptionLabel" runat="server" Text="<%# Bind('Description') %>"></asp:Label>
                    </td>

                    <td>
                    <asp:Label ID="ShowInProductListLabel" runat="server" Text="<%# Bind('ShowInProductList') %>"></asp:Label>
                    </td>
                    <td>
                    <asp:Button ID="Button1" runat="server" CommandName="Delete" Text="Delete" />
                    <asp:Button ID="Button2" runat="server" CommandName="Edit" Text="Edit" />
                    </td>
                </tr>
            </ItemTemplate>

            <EditItemTemplate>
                <tr runat="server" id="_placeholder" >
                    <td>
                        <asp:FileUpload ID="imgProduct2" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Image") %>' />
                    </td>
                    <td>
                        <asp:TextBox ID="NameTextBox" runat="server" Text="<%# Bind('Name') %>"></asp:TextBox>
                    </td>

                    <td>
                        <asp:DropDownList ID="ProductCategoryDDL" runat="server"></asp:DropDownList>
                    </td>


                    <td>
                        <asp:TextBox ID="DescriptionTextBox" runat="server" Text="<%# Bind('Description') %>"></asp:TextBox>
                    </td>

                    <td>
                        <asp:DropDownList ID="ShowInProductListDDL" runat="server">
                            <asp:ListItem>Yes</asp:ListItem>
                            <asp:ListItem>No</asp:ListItem>
                        </asp:DropDownList>
                    </td>
                    <td>
                        <asp:Button ID="UpdateButton" runat="server" CommandName="Update" Text="Update" />
                        <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Cancel" />
                    </td>
                </tr>
            </EditItemTemplate>

            <InsertItemTemplate>
                <tr runat="server" id="_placeholder">
                    <td>
                        <asp:TextBox ID="NameLabelTextBox" runat="server" Text="<%# Bind('NameLabel') %>"></asp:TextBox>
                    </td>

                    <td>
                        <asp:TextBox ID="CategoryNameTextBox" runat="server" Text="<%# Bind('ProductCategory.CategoryName') %>"></asp:TextBox>
                    </td>


                    <td>
                        <asp:Button ID="UpdateButton" runat="server" CommandName="Insert" Text="Insert" />
                    </td>
                </tr>
            </InsertItemTemplate>


</asp:ListView>



Code Behind

protected void productListView_ItemEditing(object sender, ListViewEditEventArgs e)
{

productListView.EditIndex = e.NewEditIndex;
DropDownList PDDL = (DropDownList)productListView.Items[e.NewEditIndex].FindControl("ProductCategoryDDL");
productListView.EditIndex = e.NewEditIndex;
DataBind();


}

I am not able to fine the Dropdownlist in code behind for the Product list Listview dropdownlist PDDL is always "null", any suggestions what is wrong in the code??

Thank you,
Chetan.
Posted

1 solution

I think you should data bind earlier. Try this way.

C#
protected void productListView_ItemEditing(object sender, ListViewEditEventArgs e)
{
productListView.EditIndex = e.NewEditIndex;
DataBind();
DropDownList PDDL = (DropDownList)productListView.Items[e.NewEditIndex].FindControl("ProductCategoryDDL");
productListView.EditIndex = e.NewEditIndex;
}
 
Share this answer
 
Comments
cnjadhav 18-Oct-11 10:01am    
If i do that, it seems to find the controls but i get the same "null" reference error for OnitemDataBound event imgproduct, here's my event
protected void productListView_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{

Product p = (Product)e.Item.DataItem;
Image imgProduct = (Image)e.Item.FindControl("imgProduct");
imgProduct.ImageUrl = "/ShoppingCart/ProductPics.ashx?pid" + p.ProductID.ToString() + "&s" + Settings["product_picSize"].ToString();

}
}
wonder-FOOL 18-Oct-11 11:14am    
So I am assuming you are getting the null reference error in this line " imgProduct.ImageUrl = "/ShoppingCart/ProductPics.ashx?pid" + p.ProductID.ToString() + "&s" + Settings["product_picSize"].ToString();" if so make sure the values are correct and not "NULL" also check if the path exists otherwise there shouldnt be a problem.
cnjadhav 18-Oct-11 12:04pm    
its the Line before that, where the Image is not been found, Image imgProduct = (Image)e.Item.FindControl("imgProduct"); imgProduct is "null"
cnjadhav 18-Oct-11 11:35am    
its the Line before that, where the Image is not been found,
Image imgProduct = (Image)e.Item.FindControl("imgProduct");
imgProduct is "null"
Wimpie Ratte 17-Feb-12 8:24am    
thanks Orcun. this suggested solution worked for me. (calling databind() before searching for the control)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900