2017-11-29 15 views
0
--code behind-- 

protected void findcourse() 
    { 
     foreach (GridViewRow grow in GridView2.Rows) 
     { 
      DropDownList drop1 = (DropDownList)grow.FindControl("DropDownList1"); 

      con.Open(); 
      SqlCommand cmd = new SqlCommand("select descr from restrnt_master", con); 
      SqlDataAdapter da = new SqlDataAdapter(cmd); 
      DataSet dt = new DataSet(); 
      da.Fill(dt); 
      con.Close(); 
      drop1.DataSource = dt; 
      drop1.DataTextField = "descr"; 
      drop1.DataBind(); 
      drop1.Items.Insert(0, new ListItem("--Select--", "0")); 
     } 

    } 

--source code-- 

    <asp:TemplateField HeaderText="Item"> 
            <ItemTemplate> 
             <asp:Label ID="Label8" runat="server" Text='<%# Eval("item") %>'></asp:Label> 
            </ItemTemplate> 
            <EditItemTemplate> 
             <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 
              SelectedValue='<%# Eval("item") %>' 
              onselectedindexchanged="DropDownList1_SelectedIndexChanged"> 
             </asp:DropDownList> 


            </EditItemTemplate> 
            <FooterTemplate> 
             <asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True" 

              onselectedindexchanged="DropDownList2_SelectedIndexChanged"> 
             </asp:DropDownList> 
             <br /> 

            </FooterTemplate> 
           </asp:TemplateField> 

これはページが読み込まれたときに次のエラーを示します。エラーを特定するのに役立ちます。 ページのロード時にfindcourse()関数を追加しました。挿入後、コーディングを更新しました。ドロップダウンの自動ポストバックがnullを示します。参照例外エラー

System.NullReferenceException:オブジェクト参照がオブジェクトのインスタンスに設定されていません。

+0

[NullReferenceExceptionとは何ですか?]を複製して修正するにはどうすればいいですか?(https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – john

答えて

1

あなたはこのライン

DropDownList drop1 = (DropDownList)grow.FindControl("DropDownList1"); 

あなたのコントロールが編集とフッターテンプレートであるので、それがある中でエラーを取得する必要があります。また、別途、それをバインドする必要があります。

代替ソリューションがあります。データシートを取得し、DataSourceを使用してaspxの編集とフッターのテンプレートにバインドします。解決

protected DataTable findCourse() 
    { 
     con.Open(); 
     SqlCommand cmd = new SqlCommand("select descr from restrnt_master", con); 
     SqlDataAdapter da = new SqlDataAdapter(cmd); 
     DataSet dt = new DataSet(); 
     da.Fill(dt); 
     con.Close(); 

     return dt; 
    } 



<EditItemTemplate> 
            <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" DataSource="<%# findCourse() %>" 
             SelectedValue='<%# Eval("item") %>' 
             onselectedindexchanged="DropDownList1_SelectedIndexChanged"> 
            </asp:DropDownList> 


           </EditItemTemplate> 

<FooterTemplate> 
            <asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True" DataSource="<%# findCourse() %>" 

             onselectedindexchanged="DropDownList2_SelectedIndexChanged"> 
            </asp:DropDownList> 
            <br /> 

           </FooterTemplate> 
1

..

私はSelectedValueの削除(SelectedValueの= '<%の#エバール( "項目を")%>')DropDownList1..andから属性その作業..可能