手机
当前位置:查字典教程网 >编程开发 >C#教程 >深入DropDownList用法的一些学习总结分析
深入DropDownList用法的一些学习总结分析
摘要:首先绑定数据。现收集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()));

}

【深入DropDownList用法的一些学习总结分析】相关文章:

深入理解C#索引器(一种支持参数的属性)与属性的对比

c#下注册表操作的一个小细节

C# DES加密算法中向量的作用详细解析

C# DropDownList中点击打开新窗口的方法

ref 和out传参的区别分析

C#中一些字符串操作的常用用法

描述C#多线程中lock关键字的使用分析

C# SendKeys使用方法介绍

C#数据结构与算法揭秘二 线性结构

深入c# 类和结构的区别总结详解

精品推荐
分类导航