DropDownList的几种绑定数据方法
2008-11-11 08:50
453 查看
在写页面时经常用到DropDownList,当它的数据源类型不同时,我们应该选择不同的绑定方法,尽量避免错误。
当数据源是NoticeTexts时可以这样:
var expertNation = Database.NoticeTexts.GetNoticeTexts(NoticeTextType.Nation);//获取Nation
expertNation.Insert(0, new NoticeText { Value = "请选择民族".Trim() });//在原有数据源的基础上加上一项
expertNation.ApplyToListControl(DropDownListExpertNation, expert.Nation);//绑定
2. 数据源是枚举类型:
页面直接绑定:
<td class="data">
<asp:DropDownList ID="DropDownListEpertState" Width="155px" runat="server">
<asp:ListItem Text="在职" Value="1"></asp:ListItem>
<asp:ListItem Text="出国" Value="2"></asp:ListItem>
<asp:ListItem Text="非在职" Value="3"></asp:ListItem>
</asp:DropDownList>
</td>
3.数据库中直接获取(一般情况用这种方法比较安全,可以避免一些奇怪的bug):
var expertCollege = Database.Departments.GetCollege();
DropDownListExpertCollege.Items.Add(new ListItem { Text = "请选择所在学院", Value = "请选择所在学院", Selected = ! expert.CollegeID.HasValue });
foreach (var college in expertCollege)
DropDownListExpertCollege.Items.Add(new ListItem { Text = college.Name, Value = college.ID.ToString(), Selected = expert.CollegeID.HasValue && expert.CollegeID.Value == college.ID });
暂时学到这些,如有新的方法,再续。
当数据源是NoticeTexts时可以这样:
var expertNation = Database.NoticeTexts.GetNoticeTexts(NoticeTextType.Nation);//获取Nation
expertNation.Insert(0, new NoticeText { Value = "请选择民族".Trim() });//在原有数据源的基础上加上一项
expertNation.ApplyToListControl(DropDownListExpertNation, expert.Nation);//绑定
2. 数据源是枚举类型:
页面直接绑定:
<td class="data">
<asp:DropDownList ID="DropDownListEpertState" Width="155px" runat="server">
<asp:ListItem Text="在职" Value="1"></asp:ListItem>
<asp:ListItem Text="出国" Value="2"></asp:ListItem>
<asp:ListItem Text="非在职" Value="3"></asp:ListItem>
</asp:DropDownList>
</td>
3.数据库中直接获取(一般情况用这种方法比较安全,可以避免一些奇怪的bug):
var expertCollege = Database.Departments.GetCollege();
DropDownListExpertCollege.Items.Add(new ListItem { Text = "请选择所在学院", Value = "请选择所在学院", Selected = ! expert.CollegeID.HasValue });
foreach (var college in expertCollege)
DropDownListExpertCollege.Items.Add(new ListItem { Text = college.Name, Value = college.ID.ToString(), Selected = expert.CollegeID.HasValue && expert.CollegeID.Value == college.ID });
暂时学到这些,如有新的方法,再续。
相关文章推荐
- ASP.NET-GridView数据绑定的几种方法
- 8种方法把数据绑定到Dropdownlist
- ASP.Net应用深入系列(一):GridView行绑定时取得某个数据列的几种方法比较
- 如何解决FormView中实现DropDownList连动选择时出现 "Eval()、XPath() 和 Bind() 这类数据绑定方法只能在数据绑定控件的上下文中使用" 的错误
- c#(winform)中自定义ListItem类方便ComboBox和ListBox添加项完全解决(DropDownList 和Combox 数据绑定的常用方法)
- 引用 几种绑定DropdownList的方法
- -将DropDownList控件绑定 到GridView控当中,并调用方法给DropDownList控件在页面进行数据绑定
- 数据绑定以及Container.DataItem几种方式与使用方法分析
- ASP.NET中DropDownList下拉框列表控件绑定数据的4种方法
- GridView行绑定时取得某个数据列的几种方法比较
- 如何解决FormView中实现DropDownList连动选择时出现 "Eval()、XPath() 和 Bind() 这类数据绑定方法只能在数据绑定控件的上下文中使用" 的错误
- 数据绑定以及Container.DataItem几种方式与使用方法分析
- Asp.Net--DropDownList与DataTable数据绑定的方法(C#)[学习笔记]
- ASP.Net应用深入系列(一):GridView行绑定时取得某个数据列的几种方法比较
- DropDownList绑定数据的几种方式
- struts2中Action数据绑定几种方法
- ASP.NET中dropdownlist 动态绑定数据的二种方法
- asp.net实现XML文件读取数据绑定到DropDownList的方法
- c#(winform)中自定义ListItem类方便ComboBox和ListBox添加项完全解决(DropDownList 和Combox 数据绑定的常用方法) .
- [Asp.Net]4种方法把数据绑定到Dropdownlist