DropDownList的AppendDataBoundItems属性
2008-02-29 16:15
260 查看
在ASP.NET 2.0中,可以在数据绑定时,通过设置DropDownList的AppendDataBoundItems属性为true,在数据绑定之前添加一个新的项目,并且这个新加的项目会保存在ViewState之中。下面就是一个实现的例子:
protected void Page_Load(object sender, EventArgs e)
...{
if (!IsPostBack)
...{
//DropDownList的属性AppendDataBoundItems
//功能:指示是否在数据绑定之前清除列表
DropDownList1.AppendDataBoundItems = true;
DropDownList1.Items.Add(new ListItem("-- 请选择一个选择项 --", ""));
DropDownList2.DataSource = DropDownList1.DataSource = CreateDataSource();
DropDownList2.DataTextField = DropDownList1.DataTextField = "text";
DropDownList2.DataValueField = DropDownList1.DataValueField = "id";
DropDownList1.DataBind();
DropDownList2.DataBind();
}
}
//自定义数据源
ICollection CreateDataSource()
...{
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add(new DataColumn("id", typeof(Int32)));
dt.Columns.Add(new DataColumn("text", typeof(string)));
for (int i = 0; i < 6; i++)
...{
dr = dt.NewRow();
dr[0] = i;
dr[1] = "列表项目 " + i.ToString();
dt.Rows.Add(dr);
}
DataView dv = new DataView(dt);
return dv;
}
protected void Button1_Click(object sender, EventArgs e)
...{
Response.Write("<li>DropDownList1 您选择的项目:" + DropDownList1.SelectedValue
+ " ; " + DropDownList1.SelectedItem.Text);
Response.Write("<li>DropDownList2 您选择的项目:" + DropDownList2.SelectedValue
+ " ; " + DropDownList2.SelectedItem.Text);
}
//也可以这样添加
/**//*
protected void DropDownList1_DataBound(object sender, EventArgs e)
{
DropDownList1.Items.Insert(0, new ListItem("--请选择--", ""));
}*/
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
<asp:DropDownList ID="DropDownList2" runat="server" AppendDataBoundItems="true">
<asp:ListItem Text="请选择" Value=""></asp:ListItem>
</asp:DropDownList>
<asp:Button ID="Button1" runat="server" Text="得到选择的值" OnClick="Button1_Click" />
另外,还可以使用下面的方法添加:
protected void DropDownList1_DataBound(object sender, EventArgs e)
{
DropDownList1.Items.Insert(0,new ListItem("--请选择--", ""));
}
相关文章推荐
- ListControl.AppendDataBoundItems 属性
- AppendDataBoundItems属性小用
- ListControl.AppendDataBoundItems 属性
- Append items at runtime to a dropdownlist
- AppendDataBoundItems 执行数据绑定之前将项添加到 listcontrol
- asp.net 2.0中新增的AppendDataBoundItems
- AppendDataBoundItems实现追加绑定
- asp.net 2.0中新增的AppendDataBoundItems
- DropDownList的常用属性和事件
- dropdownlist绑定对象属性
- Cannot have multiple items selected in a DropDownList
- #400 – 使用ItemsPanel 属性将WrapPanel 作为ListBox的显示面板(Using a WrapPanel as the Items Panel for a ListBox)
- asp.net给DropDownList添加ToolTip(title)属性
- dropdownlist控件的几个属性selectedIndex、selectedItem、selectedValue、selectedItem.Text、selectedItem.value的区别
- 最近在使用sps类库过程中发现了一个让我比较疑惑的问题(有关items属性的)
- C#:ListBox的2个常用方法Add,Clear与Items的2个属性
- DropDownList中的AutoPostBlack属性
- Asp.net WebForm DropDownList 无限极联动(不需要写C#代码,不需要写JS,只是配置属性)
- flex布局justify-content属性和align-items,align-self属性
- Flex中如何通过给openItems属性,设置一个XMLList或Array对象来打开Tree的一个节点