Hashem AL-Rifai
Hashem AL-Rifai

Reputation: 173

DetailsView_ModeChanged method does NOT find controls in a certain mode using FindCotrol method

I have a DetailsView control about a store products.

When I hit the "Edit" button of the DetailsView control, I want to bind a DropDownList to list products categories and select the current product category in it.

I used the method "ModeChanged" to select the current product category like this:

Edit: Markup:

<asp:DetailsView ID="dtlProduct" runat="server" 
        DataSourceID="ProductDetailsLinqDataSource" AutoGenerateRows="False" 
        DataKeyNames="ProductID">

        <Fields>
            <asp:BoundField DataField="ProductName"
                SortExpression="ProductName" />
            <asp:TemplateField>
            <ItemTemplate>
                <asp:Label Text='<%# Eval("ProductCategory.CategoryName") %>' runat="server" />
            </ItemTemplate>
            <EditItemTemplate>
                <asp:DropDownList ID="ddlCategory" runat="server" DataSourceID="LDS_ProductsCategories" 
                    DataTextField="CategoryName" DataValueField="CategoryID" Width="200px">
                </asp:DropDownList>
                <asp:LinqDataSource ID="LDS_ProductsCategories" runat="server" 
                    ContextTypeName="ProductsDataClassesDataContext" 
                    Select="new (CategoryID, CategoryName)" TableName="ProductCategories">
                </asp:LinqDataSource>
            </EditItemTemplate>
            </asp:TemplateField>
        </Fields>
    </asp:DetailsView>

Code Behind:

protected void dtlProduct_ModeChanged(object sender, EventArgs e)
{
    if (dtlProduct.CurrentMode == DetailsViewMode.Edit)
    {
        ProductsDataClassesDataContext dc = new ProductsDataClassesDataContext();
        var categoryID = (from c in dc.Products
                     where c.ProductID == (int)dtlProduct.DataKey.Value
                     select c.ProductCategoryID).FirstOrDefault();

        if (categoryID != null)
        {
            DropDownList ddl = dtlProduct.FindControl("ddlCategory") as DropDownList;
            ddl.Items.FindByValue(categoryID.ToString()).Selected = true;
        }
    }
}

the FindControl method DOES NOT find the "ddlCategory" (returns null) although it's present in the EditTemplateField.

I don't know what's going wrong!

I'm thinking to use "DropDownList's PreRender" event for doing the purpose I aim, but I want to know what is wrong!

Many thanks....

Upvotes: 1

Views: 291

Answers (1)

FiveTools
FiveTools

Reputation: 6030

It looks like you need to find your edit container first. Looking at your question, if I understand correctly - I may suggest using the Databound event and bind the dropdown list there.

Check out this link: http://weblogs.asp.net/sukumarraju/archive/2009/11/22/binding-drop-down-list-control-when-details-view-is-in-edit-mode.aspx

I'm also thinking you should move your productcategeories datasource:

  <asp:LinqDataSource ID="LDS_ProductsCategories" runat="server" 
                    ContextTypeName="ProductsDataClassesDataContext" 
                    Select="new (CategoryID, CategoryName)" TableName="ProductCategories">
                </asp:LinqDataSource>

to outside of the edit template (it can exist outside of the detailsview).

Upvotes: 1

Related Questions