【发布时间】:2013-02-11 03:42:32
【问题描述】:
我的 asp.net Web 表单中有一个下拉菜单 ddlInstructorAllocated,它从数据库中获取列表中的所有项目。我插入了另一个项目以显示为“请选择讲师”,以便用户知道该怎么做。保存此表单时,它会填充输入到数据库中的信息。 ddlInstructorAllocated 对应的列不是必填字段,可以保留为 NULL。我要做的是默认选择“请选择讲师”时,其值为NULL。我尝试了很多方法,但似乎无法弄清楚。以下是我尝试过的方法之一。请帮忙。对于从列表中选择讲师的实例,返回其 ID,将其转换为字符串,然后保存到数据库中。
private void DisplayInstructors()
{
ListItem li;
li = new ListItem("Please Select Instructor");
li.Value = null;
ddlInstructorAllocated.Items.Add(li);
var instructors = EmployeeRepository.GetInstructors();
foreach (var i in instructors)
{
ddlInstructorAllocated.Items.Add(new ListItem(string.Format("{0} {1}", i.FirstName, i.LastName), i.EmployeeID.ToString()));
}
}
数据库中的 ID 存储为 int,稍后我正在执行 EmployeeID = int.Parse(ddlInstructorAllocated.SelectedValue),我收到输入字符串格式不正确的异常
【问题讨论】:
-
因为数据库中的 ID 存储为 int 并且我稍后会执行 'EmployeeID = int.Parse(ddlInstructorAllocated.SelectedValue)',所以我收到一个异常,即输入字符串不在正确的格式
-
您必须将 selectedvalue 转换为您的 int id 属性。
-
如何将 null 转换为 int。你能举个例子吗?谢谢
-
请检查我的回答。
标签: asp.net c#-4.0 drop-down-menu null webforms