0

タグDropDownListを使用して作成した にデータをバインドしようとしています。<asp:DropDownList最初の については正常に機能しDropDownListますが、残りの 2 については例外がスローされます。データ ソースがnull ではなく、実際に値を持っていることはわかっているので、他の 2 つのリスト ボックスがまだロードまたはバインドされていないことしか考えられません。

メソッドを使用してコンテンツを追加しようとしましたPage_LoadCompleteが、起動していないようです。私は MVC に慣れていて、Web フォームに慣れていないので、何が間違っているのかわかりません。要素がまだロードされていないと述べて正しいですか?もしそうなら、要素がロードされたらどうすればそれらをバインドできますか?

protected void Page_Load(object sender, EventArgs e)
    {
        //Page.LoadComplete += new EventHandler(Page_LoadComplete);
        if (DataContext == null)
        {
            DataContext = new ReuseLibraryDataContext(SPContext.Current.Web.Url);
        }

        // top level category for all documents
        _functions = DataContext.ReuseLibrary.Select(p => p.FunctionalCategories).Distinct().ToList();

        // the first sub category
        _sc1 = DataContext.ReuseLibrary.Select(p => p.SubCategoriesLevel1).Distinct().ToList();

        // the second sub category
        _sc2 = DataContext.ReuseLibrary.Select(p => p.SubCategoriesLevel2).Distinct().ToList();


        // add the functions to the dropdown
        listFunctions.DataSource = _functions;
        listFunctions.DataBind();

        // add the sub cat 1 to the dropdown
        listSC1.DataSource = _sc1;
        listSC1.DataBind();

        // add the sub cat 2 to the dropdown
        listSC2.DataSource = _sc2;
        listSC2.DataBind();
    }

    //protected void Page_LoadComplete(object sender, EventArgs e)
    //{
    //    // add the functions to the dropdown
    //    listFunctions.DataSource = _functions;
    //    listFunctions.DataBind();

    //    // add the sub cat 1 to the dropdown
    //    listSC1.DataSource = _sc1;
    //    listSC1.DataBind();

    //    // add the sub cat 2 to the dropdown
    //    listSC2.DataSource = _sc2;
    //    listSC2.DataBind();
    //}
4

1 に答える 1

3

私はばかげているように感じます-しかし、他の誰かがこのようなことに巻き込まれた場合に備えて、問題が何であったかを投稿します.

データのリストを提供していnullましたが、そのリスト内のオブジェクトもNullReferenceException. かなり理解できます。

null解決するために、値とビオラを削除しました。期待どおりに動作します。

_sc1.RemoveAll(item => item == null);
_sc2.RemoveAll(item => item == null);

null 値を除外するようにクエリを更新することになりました。

_sc1 = DataContext.ReuseLibrary.Select(p => p.SubCategoriesLevel1).Where(p => p != null).Distinct().ToList();

于 2013-09-20T18:54:08.837 に答える