Asp.Net 绑定下拉框的值
方法1:下拉框列表直接绑定
方法2:cs代码实现:
(注:方法1和方法2不能同时使用,同时使用会以方法1为主,方法2无效)
protected void Page_Load(object sender, EventArgs e)
{
//绑定字符串的下拉列表
if (!IsPostBack)
{
List
strList.Add("Please select");
strList.Add("AAAA 1");
strList.Add("BBBB 2");
strList.Add("CCCC 3");
strList.Add("DDDD 4(不可选择)");
ddlBox.DataSource = strList;
ddlBox.DataBind();
}
//绑定自定义类的下拉列表
if (!IsPostBack)
{
List<StudentInfo> myList = new List<StudentInfo>();
myList.Add(new StudentInfo("1", "Please select"));
myList.Add(new StudentInfo("2", "AAAA 1"));
myList.Add(new StudentInfo("3", "BBBB 2"));
myList.Add(new StudentInfo("4", "CCCC 3"));
myList.Add(new StudentInfo("5", "DDDD 4(不可选择)"));
ddlBox.DataTextField = "Name";
ddlBox.DataValueField = "ID";
ddlBox.DataSource = myList;
ddlBox.DataBind();
}
}
public class StudentInfo
{
private string _id;
public string ID
{
get { return _id; }
set { _id = value; }
}
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
public StudentInfo(string id, string name)
{
_id = id;
_name = name;
}
}
protected void ddlBox_SelectedIndexChanged(object sender, EventArgs e)
{
string strSelect = ddlBox.SelectedValue;
if (ddlBox.SelectedIndex == 0)
{
Response.Write("请选择模板!");
}
}
幸福是一件礼物,得到它的秘诀是不怀期待,只在它来的时候尽情享受