2010-12-04 7 views
1

ストア製品に関するDetailsViewコントロールがあります。DetailsView_ModeChangedメソッドはFindCotrolメソッドを使用して特定のモードでコントロールを見つけられません

DetailsViewコントロールの "編集"ボタンを押すと、DropDownListをバインドして、製品カテゴリを一覧表示し、その中に現在の製品カテゴリを選択する必要があります。

私はこのような現在の製品カテゴリを選択する方法 "ModeChanged" を使用:

編集:マークアップ:背後に

<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> 

コード:

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; 
     } 
    } 
} 

FindControlメソッドはありませんがEditTemplateFieldには存在していますが、 "ddlCategory"(nullを返します)が見つかりました。

私は何がうまくいかないのですか?

私は目的を達成するために "DropDownListのPreRender"イベントを使用することを考えていますが、何が間違っているか知りたいのです!

ありがとうございました。

答えて

1

編集コンテナを最初に探す必要があるようです。あなたの質問を見れば、私が正しく理解している - 私はDataboundイベントを使用し、そこにドロップダウンリストをバインドすることを提案するかもしれません。

このリンクをチェックアウト:(それはたDetailsViewの外に存在することができます)

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

編集テンプレートの外側に:私はまた、あなたのproductcategeoriesデータソースを移動しなければならないと思ってい http://weblogs.asp.net/sukumarraju/archive/2009/11/22/binding-drop-down-list-control-when-details-view-is-in-edit-mode.aspx

を。

+0

ありがとうございました。どうすれば編集コンテナを見つけることができますか?私はasp.netマークアップを追加しました、あなたは私を導くことができますか? –

関連する問題