【问题标题】:DropDownList.DataSource NullReferenceException?DropDownList.DataSource NullReferenceException?
【发布时间】:2014-09-15 15:17:14
【问题描述】:

我在后端 CS 代码中得到空引用。为什么是DataTablenull

<asp:TemplateField  HeaderText="Frequency" ItemStyle-Width = "150" >
    <ItemTemplate>
    <asp:Label ID="Frequency"  runat="server" Text='<% # Eval("frequency") %>' ></asp:Label>
    <asp:DropDownList ID="frequencydropdownlist" runat="server" Visible="false" ></asp:DropDownList>
     </ItemTemplate>
        <FooterTemplate>
            <asp:DropDownList ID="Addfrequencydropdownlist" runat="server"></asp:DropDownList>
        </FooterTemplate>
    </asp:TemplateField>
    </Columns>

代码背后:

public partial class CollectionHead : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {       
        DataTable dtt = new DataTable();
        string conString = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
        SqlConnection con = new SqlConnection(conString);
        SqlCommand mycommand = new SqlCommand();
        mycommand.Connection = con;
        mycommand.CommandText = "select ID,Frequency from FeesFrequency";
        mycommand.CommandType = CommandType.Text;
        mycommand.Connection = con;
        SqlDataAdapter sda = new SqlDataAdapter();
        sda.SelectCommand = mycommand;

        sda.Fill(dtt);
        DropDownList Addfrequencydropdownlist = CollectionHead_GridView.FindControl("Addfrequencydropdownlist") as DropDownList;

        Addfrequencydropdownlist.DataSource = dtt.DefaultView;// null reference exception ????????
        Addfrequencydropdownlist.DataTextField = "Frequency";
        Addfrequencydropdownlist.DataValueField = "ID";
        Addfrequencydropdownlist.DataBind();

【问题讨论】:

  • 那里有问题吗?
  • Addfrequencydropdownlist,在你试投之后是null

标签: c# asp.net datatables nullreferenceexception


【解决方案1】:

更新 您必须先对网格进行数据绑定,然后才能访问它的页脚。因此我会使用RowDataBound 事件来填充DropDownList

protected void grid_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.Footer)
    {
        DropDownList Addfrequencydropdownlist = (DropDownList)e.Row.FindControl("Addfrequencydropdownlist");
        // ...            
    }
}

旧答案(可能仍然有用):

它是Addfrequencydropdownlist,这是您尝试投射后的null,因为网格页脚中DropDownListNamingContainer 不是网格本身,而是FooterRow。所以这是null 导致NullReferenceException

CollectionHead_GridView.FindControl("Addfrequencydropdownlist")

您可以使用网格的FooterRow-property 来获取它的参考:

GridViewRow footer = CollectionHead_GridView.FooterRow;
DropDownList Addfrequencydropdownlist = (DropDownList)footer.FindControl("Addfrequencydropdownlist");

顺便说一句,我只会使用as 运算符,除非它是null 不是例外。否则,您将用代码中的错误替换一个令人讨厌的NullReferenceException(在这种情况下,NullReferenceException 在错误的位置)。

如果ViewState 已启用(默认),我还将它包装在if(!IsPostBack)-check 中,以便仅在初始加载时而不是在每次回发时对其进行数据绑定:

if(!IsPostBack)
{
    DataTable dtt = new DataTable();
    // rest of your code in Page_Load ....
}

【讨论】:

  • 感谢您的帮助,但我仍然在您的代码行中遇到相同的错误,并出现相同的空引用错误
  • 感谢您的帮助,它按预期帮助了我。
猜你喜欢
  • 2015-01-12
  • 2016-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多