俄罗斯VS沙特直播

从老师到销售员,再从销售员到创业当老板,刘敏从来不安于现状,敢于冒险的精神让他完成一个个身份的转变,实现人生的超越。我使用mark-compact算法:(1)找出所有要洗的锅碗瓢盆,(2)清洗它们,(3)想好要按什么顺序把它们放到架子上,(4)然后把它们放到架子上。主要特性功能丰富且易于使用AssertJ提供了丰富的断言集合,错误信息非常准确,提高了测试代码的可读性并且可以非常方便地在你喜爱的IDE中集成。在巴黎,刚刚落下帷幕的2016维多利亚的秘密大秀上,各路天使吸引了全世界的眼球。现在每天的客流量都很稳定,维持在220-230人之间,达到70%的接待率。
返回首页

实现给DropDownList服务器控件添加项的新方法详解

时间:2011-06-01 15:10来源:知行网www.youyuan-chem.com 编辑:麦田守望者

在ASP.NET 2.0中,可以在数据绑定时,通过设置DropDownList的AppendDataBoundItems属性为true,在数据绑定之前添加一个新的项目,并且这个新加的项目会保存在ViewState之中。下面就是一个实现的例子:

C#代码
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
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 Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
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();
}
}
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>DropDownList 补充例子</title>
</head>
<body>
<form id="form1" runat="server">
<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" />
</form>
</body>
</html>

VB.NET代码
<%@ Page Language="VB" AutoEventWireup="true" %>

<%@ Import Namespace="System.Data" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Response.Write("<li>DropDownList1 您选择的项目:" + DropDownList1.SelectedValue + _
" ; " + DropDownList1.SelectedItem.Text)
Response.Write("<li>DropDownList2 您选择的项目:" + DropDownList2.SelectedValue + _
" ; " + DropDownList2.SelectedItem.Text)
End Sub

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
If Not IsPostBack Then
DropDownList1.AppendDataBoundItems = True
DropDownList1.Items.Add(New ListItem("-- 请选择一个选择项 --", ""))
DropDownList2.DataSource = CreateDataSource()
DropDownList1.DataSource = CreateDataSource()
DropDownList2.DataTextField = "text"
DropDownList1.DataTextField = "text"
DropDownList2.DataValueField = "id"
DropDownList1.DataValueField = "id"
DropDownList1.DataBind()
DropDownList2.DataBind()
End If
End Sub

Function CreateDataSource() As ICollection
Dim dt As DataTable = New DataTable()
Dim dr As DataRow
dt.Columns.Add(New DataColumn("id", GetType(System.Int32)))
dt.Columns.Add(New DataColumn("text", GetType(String)))
For i As Integer = 0 To 6
dr = dt.NewRow()
dr(0) = i
dr(1) = "列表项目 " + i.ToString()
dt.Rows.Add(dr)
Next
Dim dv As DataView = New DataView(dt)
Return dv
End Function

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>DropDownList 补充例子</title>
</head>
<body>
<form id="form1" runat="server">
<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" />
</form>
</body>
</html>
 

顶一下
(0)
0%
踩一下
(0)
0%
标签(Tag):C# C#实例教程 c#基础教程 C#源代码
------分隔线----------------------------
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
验证码:点击我更换图片