NullReferenceException Occured - dropdownlist ASP.Net 4 C#

本文关键字:Net ASP dropdownlist Occured NullReferenceException | 更新日期: 2023-09-27 18:29:44

我有一个包含空白、是和否的下拉列表。如果用户选择否,我希望Label1说"不需要进一步的信息"。

当我调试时,我得到了一个nullreferenceexception-当用户开始使用下拉菜单时,该字段的SQL数据库中的值为NULL,我希望他们能够选择Yes或No,如果他们之前选择并存储了"Yes"或"No",我还希望他们能够返回并选择空白,这将向数据库反馈NULL。

处理这个问题最简单的方法是什么?我认为错误是因为DB中的NULL值而引发的?

代码:

<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true"
             SelectedValue='<%# Bind("BlahBlah") %>' 
             onselectedindexchanged="DropDownList1_SelectedIndexChanged">
            <asp:ListItem></asp:ListItem>
            <asp:ListItem>Yes</asp:ListItem>
            <asp:ListItem>No</asp:ListItem>
         </asp:DropDownList>`

完整cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace FrontEnd_v1
{
public partial class WebForm1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {

        if (DropDownList1.SelectedValue == "No")
        {
            Label1.Text="No Further Info Required";
        }
        else
        {
            Label1.Text="";
        }

    }
}

}

designer.cs:

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated. 
// </auto-generated>
//------------------------------------------------------------------------------
namespace FrontEnd_v1 {

public partial class WebForm1 {
    /// <summary>
    /// FormView1 control.
    /// </summary>
    /// <remarks>
    /// Auto-generated field.
    /// To modify move field declaration from designer file to code-behind file.
    /// </remarks>
    protected global::System.Web.UI.WebControls.FormView FormView1;
    /// <summary>
    /// RM_Remediation control.
    /// </summary>
    /// <remarks>
    /// Auto-generated field.
    /// To modify move field declaration from designer file to code-behind file.
    /// </remarks>
    protected global::System.Web.UI.WebControls.SqlDataSource SQLSource;

    /// <summary>
    /// Button1 control.
    /// </summary>
    /// <remarks>
    /// Auto-generated field.
    /// To modify move field declaration from designer file to code-behind file.
    /// </remarks>
    protected global::System.Web.UI.WebControls.Button Button1;
    protected global::System.Web.UI.WebControls.DropDownList DropDownList1;
    protected global::System.Web.UI.WebControls.Label Label1;
    }
 }

NullReferenceException Occured - dropdownlist ASP.Net 4 C#

如果它在SQL中显示为NULL,则意味着在它的位置没有输入任何值。您可能想要将值重新嵌入到数据库中,因为NULL实际上是空字段。

试试这个代码。在ListItem中添加值。

  <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true"
         SelectedValue='<%# Bind("BlahBlah") %>' 
         onselectedindexchanged="DropDownList1_SelectedIndexChanged">
      <asp:ListItem></asp:ListItem>
      <asp:ListItem Value="Yes" Text="Yes"></asp:ListItem>
      <asp:ListItem Value="No" Text="No"></asp:ListItem>
    </asp:DropDownList>`