整理点dropdownList的应用
{
DropDownList1_SelectedIndexChanged(sender, e);
}
ListItem li = DDLInvoiceType.Items.FindByValue("Value") as ListItem;
if (li != null)
{
li.Selected = true;
}else li.SelectedIndex=-1;
法二:
DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(DropDownList1.Items.FindByValue("Value值"));
就是如果通过FindByValue没有找到指定项则为null,而Items.IndexOf(null)会返回-1.
------------------------------------------------------------------------------------------------------
DDL的设置初始选定项:
DDL.Items.FindByValue("初始选定项").Selected=true;
或者 DDL.Items.FindByText("初始选定项").Selected=true;
DDL.SelectedIndex=DDL.Items.IndexOf(DDL.Items.FindByValue("初始选定项"));
或者 DDL.SelectedIndex=DDL.Items.IndexOf(DDL.Items.FindByText("初始选定项"));
DDL的各种绑定方式:
一、在页面初始化时候将集合绑定到DropDownList
public void Page_Load(Object src.EventArgs e)
{
ArrayList arrValue = new ArrayList();
arrValue.add("kk");
arrValue.add("dd");
arrValue.add("aa");
arrValue.add("cc");
//将数组绑定到DropDownList控件的DataSource属性
ddl.DataSource = arrValue;
ddl.DataBind();
}
//实现
选项有:<asp:DropDownList id="ddl" runat="server"/>
二、在页面初始化的时候向DropDownList添加数据public void Page_Load(Object src.EventArgs e)
{
ddl.Items.Add(new ListItem("text","value");
ddl.Items.Add(new ListItem("text1","value1");
ddl.Items.Add(new ListItem("text2","value2");
}
//实现
选项有:<asp:DropDownList id="ddl" runat="server"/>
三、将DataReader读取的数据动态绑定到DropDownList
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
string myconnstr="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+Server.MapPath(".")+"..\\database\\db.mdb";
OleDbConnnection myconn=new OleDbConnnection(myconnstr);
string sqlstr="select * from test";
OleDbCommand myComm = new OleDbCommand(sqlstr,myconn);
myComm.Connection.Open();
OleDbDataReader dr=myComm.ExecuteReader();
while (dr.Read())
{
bj.Items.Add(new ListItem(dr["bjmc"].ToString(), dr["id"].ToString()));//增加Item
//或者这样也能绑定,
//DropDownList1.Items.Add( new ListItem( myReader[1].ToString(),myReader[0].ToString() ) );//增加Item
//都是要在知道Sql语句或者数据表结构的前提下才能这样绑定
}
dr.Close();
}
}
四、将DataSet读取的数据动态绑定到DropDownList
SqlConnection sqlconn = new SqlConnection();
sqlconn.ConnectionString = "workstation id=localhost;packet size=4096;user
id=sa;data source=db1;persist security info=False;initial catalog=DB";
sqlconn.Open();
SqlDataAdapter sqldar = new SqlDataAdapter("select UserID,UserName from forums_Users",sqlconn);
sqldar.SelectCommand.CommandType = CommandType.Text;
DataSet Ds= new DataSet();
sqldar.Fill(Ds,"Users");
ddl.DataSource = Ds.Tables["Users"].DefaultView;
ddl.DataTextField="UsersName";
ddl.DataValueField ="UserID";
ddl.DataBind();
sqlconn.Close();
五、使用DataBinder.Eval(Container.DataItem,"表字段")输出绑定数据protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
string myconnstr="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+Server.MapPath(".")+"..\\database\\db.mdb";
OleDbConnnection myconn=new OleDbConnnection(myconnstr);
string sqlstr="select * from test";
OleDbCommand myComm = new OleDbCommand(sqlstr,myconn);
myComm.Connection.Open();
OleDbDataReader dr=myComm.ExecuteReader();
ddl.DataSource = dr;
ddl.DataBind(); 绑定倒DD1中
}
}
在UI界面的DDL中直接调用,绑定的方法调用
DataBinder.Eval(Container.DataItem,"表字段")