All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
利用RowFilter属性,将一个DataSet绑定到两个DataGrid_[Asp.Net教程]
使用PHP4.2.0及以后版本的注意事项_php资料_编程技术
正确实现,IDisposable,接口_[Asp.Net教程]
ASP.NET,2.0,Membership_[Asp.Net教程]
XML规范和学习资源小结_[XML教程]
SQL,SERVER,数据类型详解_mssql学习_编程技术
ASP.NET2.0服务器控件之创建复杂属性_[Asp.Net教程]
如何在SQL,Server中保存和输出图片_[SQL,Server教程]
如何在IE右键菜单中添加菜单项以及如何添加IE任务栏按钮_.net资料_编程技术
GridView的各种用法(2)_[Asp.Net教程]
Reading number is top 10 pictures
世界各国15岁的MM有什么不同
人美胸美腿更美1
人美胸美腿更美4
Exquisite decoration is not paying too much3
A man's favorite things14
From China fortress sora aoi5
Ashlynn Brooke a group sexy photo2
Startling Russian girl blind date scene1
YangYuYing and ZhengShaoQiu dance on the generous come interest dye-in-the-wood
治疗多发性骨髓瘤的特效药,一万二一支
Download software ranking
apache-tomcat-6.0.33
尖东毒玫瑰A
株洲本地在线棋牌游戏
Rio big adventure
Unix video tutorial8
电脑知识及技巧大合集
Kung.Fu.Panda.2
Boxer's Top ten classic battle4
功夫熊猫2(下集)
C#编程思想
归海一刀 published in(发表于) 2014/1/30 1:27:27 Edit(编辑)
DropdownList2种绑定方法实例代码_[Asp.Net教程]

DropdownList2种绑定方法实例代码_[Asp.Net教程]

DropdownList2种绑定方法实例代码_[Asp.Net教程]

















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

SqlConnection conn = UtilitySqlClass.OperateDataBase.ReturnConn();
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();

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

PRotected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SqlConnection conn = UtilitySqlClass.OperateDataBase.ReturnConn();
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());
}
}
catch (Exception ex)
{
Response.Write("alert(’" + ex.Message.ToString() + "’)");
}
finally
{
conn.Close();
}
}
}

第1种方法:
string ConnString = ConfigurationSettings.AppSettings["ConnectionString"];
//创建一个SqlConnection
SqlConnection Conn = new SqlConnection( ConnString );
string SQL_Select = "select id, ItemName from DDLItem order by id desc";
//构造一个SqlDataAdapter
SqlDataAdapter myAdapter = new SqlDataAdapter( SQL_Select, Conn);
//开始读取数据
Conn.Open();
DataSet dataSet = new DataSet();
myAdapter.Fill( dataSet,"Table1" );
Conn.Close();
//开始绑定DropDownList
//指定DropDownList使用的数据源
DropDownList1.DataSource = dataSet.Tables["Table1"].DefaultView;
//指定DropDownList使用的表里的那些字段
DropDownList1.DataTextField = "ItemName"; //dropdownlist的Text的字段
DropDownList1.DataValueField = "id";//dropdownlist的Value的字段
DropDownList1.DataBind();

第2种方法:
con.Open();
SqlCommand cmd = new SqlCommand(strSql,con);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
DropDownList1.Items.Add(new ListItem(dr["status"].ToString(), dr["status_Id"].ToString()));
}
































添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.