.NET常用控件绑定(DropDownList、RadioButtonList、CheckBoxList、ListBox、BulletedList)
2009-12-30 12:55
429 查看
提示:DropDownList、RadioButtonList、CheckBoxList、ListBox、BulletedList 绑定数据方法几乎一样
using System;
using System.Web.UI.WebControls;
using System.Data;
namespace ControlsBind
{
/// <summary>
/// ControlsBind 的摘要说明。
/// </summary>
public class ControlsBind
{
public ControlsBind()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
#region BindDropDownList
/// <summary>
/// 绑定下拉列表框
/// </summary>
/// <param name="strValueField">Value值</param>
/// <param name="strTextField">显示值</param>
/// <param name="dt">被绑定的DataTable</param>
/// <param name="ddlst_List">下拉列表框</param>
/// <param name="bFlag">是否绑定在绑定中插入"——请选择——"</param>
public static void BindDropDownList(string strValueField,string strTextField,DataTable dt,DropDownList ddlst_List, bool bFlag)
{
try
{
ddlst_List.Items.Clear();
ddlst_List.DataSource = dt;
ddlst_List.DataValueField = strValueField;
ddlst_List.DataTextField = strTextField;
ddlst_List.DataBind();
dt.Dispose();
if (bFlag)
{
ListItem NewItem = new ListItem("——请选择——","-1");
ddlst_List.Items.Insert(0,NewItem);
}
}
catch(Exception e)
{
throw(new Exception("绑定下拉列表框失败!"+ CONST.SPLITSTRING +e.Message));
}
}
#endregion
#region IntialDropDownList
/// <summary>
/// 根据当前Text值初始化DropDownList控件
/// </summary>
/// <param name="ddlstObj">DropDownList控件对象</param>
/// <param name="strText">文本</param>
public static void InitDropDownList(DropDownList ddlstObj,string strText)
{
for(int i=0; i<ddlstObj.Items.Count; i++)
{
if(ddlstObj.Items[i].Text == strText)
{
ddlstObj.SelectedIndex = i;
break;
}
}
}
/// <summary>
/// 根据当前Value值初始化DropDownList控件
/// </summary>
/// <param name="ddlstObj">DropDownList控件对象</param>
/// <param name="strValue">文本</param>
public static void InitDropDownList(string strValue, DropDownList ddlstObj)
{
for(int i=0; i<ddlstObj.Items.Count; i++)
{
if(ddlstObj.Items[i].Value == strValue)
{
ddlstObj.SelectedIndex = i;
break;
}
}
}
#endregion
}
}
using System;
using System.Web.UI.WebControls;
using System.Data;
namespace ControlsBind
{
/// <summary>
/// ControlsBind 的摘要说明。
/// </summary>
public class ControlsBind
{
public ControlsBind()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
#region BindDropDownList
/// <summary>
/// 绑定下拉列表框
/// </summary>
/// <param name="strValueField">Value值</param>
/// <param name="strTextField">显示值</param>
/// <param name="dt">被绑定的DataTable</param>
/// <param name="ddlst_List">下拉列表框</param>
/// <param name="bFlag">是否绑定在绑定中插入"——请选择——"</param>
public static void BindDropDownList(string strValueField,string strTextField,DataTable dt,DropDownList ddlst_List, bool bFlag)
{
try
{
ddlst_List.Items.Clear();
ddlst_List.DataSource = dt;
ddlst_List.DataValueField = strValueField;
ddlst_List.DataTextField = strTextField;
ddlst_List.DataBind();
dt.Dispose();
if (bFlag)
{
ListItem NewItem = new ListItem("——请选择——","-1");
ddlst_List.Items.Insert(0,NewItem);
}
}
catch(Exception e)
{
throw(new Exception("绑定下拉列表框失败!"+ CONST.SPLITSTRING +e.Message));
}
}
#endregion
#region IntialDropDownList
/// <summary>
/// 根据当前Text值初始化DropDownList控件
/// </summary>
/// <param name="ddlstObj">DropDownList控件对象</param>
/// <param name="strText">文本</param>
public static void InitDropDownList(DropDownList ddlstObj,string strText)
{
for(int i=0; i<ddlstObj.Items.Count; i++)
{
if(ddlstObj.Items[i].Text == strText)
{
ddlstObj.SelectedIndex = i;
break;
}
}
}
/// <summary>
/// 根据当前Value值初始化DropDownList控件
/// </summary>
/// <param name="ddlstObj">DropDownList控件对象</param>
/// <param name="strValue">文本</param>
public static void InitDropDownList(string strValue, DropDownList ddlstObj)
{
for(int i=0; i<ddlstObj.Items.Count; i++)
{
if(ddlstObj.Items[i].Value == strValue)
{
ddlstObj.SelectedIndex = i;
break;
}
}
}
#endregion
}
}
相关文章推荐
- DataSet绑定ListBox、DropDownList、RadioButtonList、CheckBoxList控件
- ASP.NET中 RadioButtonList(单选按钮组),CheckBoxList(复选框组),DropDownList(下拉框),ListBox(列表框),BulletedList(无序列表)的使用前台绑定
- DataSet绑定ListBox、DropDownList、RadioButtonList、CheckBoxList控件
- ListBox,CheckBoxList,DropDownList,RadioButtonList的常见数据绑定
- JQuery中对服务器控件 DropdownList, RadioButtonList, CheckboxList的操作总结
- JQuery中对服务器控件 DropdownList, RadioButtonList, CheckboxList的操作总结
- ListBox,CheckBoxList,DropDownList,RadioButtonList的常见用法
- c#(winform)中自定义ListItem类方便ComboBox和ListBox添加项完全解决(DropDownList 和Combox 数据绑定的常用方法)
- ASP.NET服务器端控件RadioButtonList,DropDownList,CheckBoxList的取值、赋值用法
- 教学思路ASP.Net之服务器控件:三、DropDownList、ListBox、CheckBoxList、RadioButtonList控件
- .net 获取checkBoxList 选中的值|radiobuttonlist|dropdownlist
- c#(winform)中自定义ListItem类方便ComboBox和ListBox添加项完全解决(DropDownList 和Combox 数据绑定的常用方法) .
- ASP.NET服务器端控件RadioButtonList,DropDownList,CheckBoxList的取值、赋值用法
- 教学思路ASP.Net之服务器控件:三、DropDownList、ListBox、CheckBoxList、RadioButtonList控件
- ASP.NET服务器端控件RadioButtonList,DropDownList,CheckBoxList的取值、赋值用法
- .net 获取checkBoxList 选中的值|radiobuttonlist|dropdownlist
- 使用复杂绑定完成 CheckBoxList 和 RadioButtonList 控件的绑定
- JQuery 对服务器控件 DropdownList, RadioButtonList, CheckboxList的操作总结
- 关于DropDownList、RadioButtonList和CheckBoxList绑定
- c#枚举类型在web控件CheckBoxList,RadioButtonList,DropDownList中的应用