未绑定状态
绑定方法1
单击“确定”完成列表项的添加
看到default.aspx代码如下
<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>练习1</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="确定"OnClick="Button1_Click" />
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem>a</asp:ListItem>
<asp:ListItem>b</asp:ListItem>
<asp:ListItem>c</asp:ListItem>
<asp:ListItem>d</asp:ListItem>
<asp:ListItem>e</asp:ListItem>
</asp:DropDownList><br />
<br />
</div>
</form>
</body>
</html>
运行看到
绑定方法2用Dropdownlist1的items集合的Add方法添加项数据在确定按钮的单击事件中添加如下代码protected void Button1_Click(object sender, EventArgs e){this.DropDownList1. Items.Add(new ListItem("11") ) ;this.DropDownList1. Items.Add(new ListItem("22") ) ;this.DropDownList1. Items.Add(new ListItem("33") ) ;this.DropDownList1. Items.Add(new ListItem("44") ) ;this.DropDownList1. Items.Add(new ListItem("55") ) ;}
这样default.aspx.cs中的代码如下using System;using System.Data;using System.Configuration;using System.Web;
using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;public partial class _Default : System.Web.UI.Page
{protected void Page_Load(object sender, EventArgs e)
{
}protected void Button1_Click(object sender, EventArgs e)
{this.DropDownList1. Items.Add(new ListItem("11") ) ;this.DropDownList1. Items.Add(new ListItem("22") ) ;this.DropDownList1. Items.Add(new ListItem("33") ) ;this.DropDownList1. Items.Add(new ListItem("44") ) ;this.DropDownList1. Items.Add(new ListItem("55") ) ;
}
}
运行并单击“确定”按钮后发现列表框中多了11,22,33,44,55
绑定方法3先引入命名空间usingSystem.collections;后定义一个动态数组arraylist su=new arraylist() ;
接着用su.Add("aa") ;给数组添加数据最后用this.dropdownlist1.datasource=su;this.dropdownlist1.databind() ;把数组数据绑定到下拉列表框中default.aspx.cs代码变成如下using System;using System.Data;
using System.Configuration;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;using System.Collections;public partial class _Default : System.Web.UI.Page
{protected void Page_Load(object sender, EventArgs e)
{
}protected void Button1_Click(object sender, EventArgs e)
{this.DropDownList1. Items.Add(new ListItem("11") ) ;this.DropDownList1. Items.Add(new ListItem("22") ) ;this.DropDownList1. Items.Add(new ListItem("33") ) ;this.DropDownList1. Items.Add(new ListItem("44") ) ;this.DropDownList1. Items.Add(new ListItem("55") ) ;
ArrayList su = new ArrayList() ;su.Add("aa") ;su.Add("bb") ;su.Add("cc") ;su.Add("dd") ;su.Add("ee") ;this.DropDownList1.DataSource = su;this.DropDownList1.DataBind() ;
}
}
运行并单击“确定”后看到如下
在设计视图中添加一个文本框实现当我们在下拉列表框中选择某个索引项时立刻在文本框中显示的功能。
要实现这样的功能得先设置服务器回发为真即autopostback为true) ,可以在
属性中找到该属性并设置
然后双击下拉列表控件在其中输入代码
protected void DropDownList1_SelectedIndexChanged(object sender,EventArgs e)
{this.TextBox1.Text = this.DropDownList1.SelectedValue;
}
这时default.aspx.cs代码变成如下using System;using System.Data;using System.Configuration;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;using System.Collections;public partial class _Default : System.Web.UI.Page
{protected void Page_Load(object sender, EventArgs e)
{
}protected void Button1_Click(object sender, EventArgs e)
{this.DropDownList1. Items.Add(new ListItem("11") ) ;this.DropDownList1. Items.Add(new ListItem("22") ) ;this.DropDownList1. Items.Add(new ListItem("33") ) ;this.DropDownList1. Items.Add(new ListItem("44") ) ;this.DropDownList1. Items.Add(new ListItem("55") ) ;
ArrayList su = new ArrayList() ;su.Add("aa") ;su.Add("bb") ;su.Add("cc") ;su.Add("dd") ;
su.Add("ee") ;this.DropDownList1.DataSource = su;this.DropDownList1.DataBind() ;
}protected void DropDownList1_SelectedIndexChanged(object sender,EventArgs e)
{this.TextBox1.Text = this.DropDownList1.SelectedValue;
}
}
运行效果如下
华为云怎么样?华为云用在线的方式将华为30多年在ICT基础设施领域的技术积累和产品解决方案开放给客户,致力于提供稳定可靠、安全可信、可持续创新的云服务,做智能世界的“黑土地”,推进实现“用得起、用得好、用得放心”的普惠AI。华为云作为底座,为华为全栈全场景AI战略提供强大的算力平台和更易用的开发平台。本次年终聚惠618活动相当给力,1核2G内存1m云耀云服务器仅88元/年起,送主机安全基础版套餐,...
legionbox怎么样?legionbox是一家来自于澳大利亚的主机销售商,成立时间在2014年,属于比较老牌商家。主要提供VPS和独立服务器产品,数据中心包括美国洛杉矶、瑞士、德国和俄罗斯。其中VPS采用KVM和Xen架构虚拟技术,硬盘分机械硬盘和固态硬盘,系统支持Windows。当前商家有几款大硬盘的独立服务器,可选美国、德国和瑞士机房,有兴趣的可以看一下,付款方式有PAYPAL、BTC等。...
近日Friendhosting发布了最新的消息,新上线了美国迈阿密的云产品,之前的夏季优惠活动还在进行中,全场一次性45折优惠,最高可购买半年,超过半年优惠力度就不高了,Friendhosting商家的优势就是100Mbps带宽不限流量,有需要的朋友可以尝试一下。Friendhosting怎么样?Friendhosting服务器好不好?Friendhosting服务器值不值得购买?Friendho...