深入DropDownList用法的一些学习总结分析
深入DropDownList用法的一些学习总结分析
发布时间:2016-12-28 来源:查字典编辑
摘要:首先绑定数据。现收集dropdownlist的三种databind方法如下:基础数据绑定:用ListItem直接枚举出来,适用于不需要修改的...

首先绑定数据。

现收集dropdownlist 的三种 databind 方法如下:

基础数据绑定:用ListItem直接枚举出来,适用于不需要修改的类型列表。

复制代码 代码如下:

<asp:DropDownList ID="DropDownList1" runat="server">

<asp:ListItem Value="设计家园">设计家园</asp:ListItem>

<asp:ListItem Value="网页设计">网页设计</asp:ListItem>

<asp:ListItem Value="网络编程">网络编程</asp:ListItem>

<asp:ListItem Value="酷站欣赏">酷站欣赏</asp:ListItem>

</asp:DropDownList>

动态绑定方法一:动态绑定数据库中的字段。

复制代码 代码如下:

SqlConnection conn = system.Configuration.ConfigurationSettings.AppSettings["SqlConnection"].ToString();

string strSQL = "select * from CompanyType";

SqlDataAdapter ada = new SqlDataAdapter(strSQL, conn);

DataSet ds = new DataSet();

ada.Fill(ds, "CompanyType");

DropDownList1.DataSource = ds.Tables["CompanyType"].DefaultView;

DropDownList1.DataValueField = ds.Tables["CompanyType"].Columns[1].ColumnName;

DropDownList1.DataTextField = ds.Tables["CompanyType"].Columns[1].ColumnName;

DropDownList1.DataBind();

ds.Dispose();

//其中datavaluefield属性是控件的一个关键属性,cs页面通过value值获取;

//而datatextfield是显示在视图页面的文本。

动态绑定方法二:利用DropDownList.Items.Add方法。

复制代码 代码如下:

protected void Page_Load(object sender, EventArgs e)

{

if (!IsPostBack)

{

SqlConnection conn = system.Configuration.ConfigurationSettings.AppSettings["SqlConnection"].ToString();

try

{

conn.Open();

this.DropDownList1.Items.Add("");

string strSQL = "select CompanyType from CompanyType";

SqlCommand com = new SqlCommand(strSQL, conn);

SqlDataReader dr = com.ExecuteReader();

while (dr.Read())

{

this.DropDownList1.Items.Add(dr["CompanyType"].ToString());

//或者

//DropDownList_name.Items.Add(new ListItem(TEXT, Value));

}

}

catch (Exception ex)

{

Response.Write("<scirpt>alert('" + ex.Message.ToString() + "')</script>");

}

finally

{

conn.Close();

}

}

}

绑定之后,我们来实现dropdownlist 的联动功能。

要实现联机变动,就要用到selectedindexchange 事件,记得要把AutoPostBack 的值设为 "true"

下面是一个最简单的联动效果。

复制代码 代码如下:

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)

{

DropDownList2.Items.Clear();

if (DropDownList1.Items[0].Selected)

{

DropDownList2.Items.Add("陆小凤");

DropDownList2.Items.Add("楚留香");

}

else

{

DropDownList2.Items.Add("杨过");

DropDownList2.Items.Add("小龙女");

}

}

如果要实现无刷新联动,自己去找度娘。网上有很多很好的文档案例。

同理,如要下级也自动获取对于的数据字段。

string id=dropdownlist1.SelectedValue;

可以然后根据此"id“去数据库中读出相应部分的数据

最后,是一个不错的通过DataSet逐行读数据的例子,业务系统“计划中心”的下拉列表.

复制代码 代码如下:

DataSet Ds = null;

string SqlStr = null;

SqlServer sqlserverDB = new SqlServer();

SqlStr = "select name,account from qdvc_usersimple";

Ds = sqlserverDB.DataSetRun(null, SqlStr, "qdvc_usersimple");

foreach (DataRow dataRow in Ds.Tables[0].Rows)

{

object[] itemArray = dataRow.ItemArray; //获取dataRow的所有的单元格里的数据Array

// itemArray[0].ToString()是"name",itemArray[1].ToString()是"account"

DropDownList_name.Items.Add(new ListItem(itemArray[0].ToString(), itemArray[1].ToString()));

}

推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
相关阅读
网友关注
最新C#教程学习
热门C#教程学习
编程开发子分类