Dropdown autopost back显示nullReference异常错误

时间:2017-11-29 07:05:00

标签: c# asp.net sql-server-2008

--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()函数,更新了编码

System.NullReferenceException:未将对象引用设置为对象的实例。

1 个答案:

答案 0 :(得分: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>