[英]NullReferenceException Occured - dropdownlist ASP.Net 4 C#
I have a dropdown containing blank, Yes and No. If the user selects No I want Label1 to say "No further info required". 我有一个包含空白的下拉列表,是和否。如果用户选择“否”,我希望Label1说“不需要其他信息”。
I'm getting a nullreferenceexception when I debug - The value in SQL db of the field is NULL when the user gets to using the dropdown, I want them to be able to select Yes or No and also if they have selected and stored "Yes" or "No" previously, I also want them to be able to go back in and select blank which will feed NULL back into the database. 我在调试时得到nullreferenceexception-用户开始使用下拉菜单时,该字段的SQL db中的值为NULL,我希望他们能够选择Yes或No,并且如果他们选择并存储了“ Yes ”或“否”,我也希望他们能够返回并选择空白,它将把NULL反馈回数据库。
What's the simplest way of handling this? 处理此问题的最简单方法是什么? I assume the error is thrown because of the NULL value in the DB? 我认为是由于DB中的NULL值引发了错误?
Code: 码:
<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>`
full cs: 全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: 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;
}
}
If it is showing NULL in the SQL, it means that no value was inputted at it's place. 如果在SQL中显示NULL,则表示在该位置未输入任何值 。 You might wanna re-embed values into your DB because a NULL is virtually empty field. 您可能想将值重新嵌入到数据库中,因为NULL实际上是空字段。
try this code. 试试这个代码。 Add Value in ListItem. 在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>`
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.