数据绑定技术(table,DataSet ,DataView,DataReader ,DropDownList,DataBinder.Eval(),datagrid,dr,)ArrayList,Hashtable绑定到ListBox控件,xml
数据绑定技术(CSJ_0808)
1 单值数据绑定:
<script language="C#" runat="server">
void Page_Load(Object sender, EventArgs e)
{
Page.DataBind();
}
string Message{
get {
return "abcdefg";
}
}
int MessageCount{
get{
return Message.Length;
}
}
</script>
<asp:TextBox id="TextBox1" runat="server" Text='<%# Message %>'>
<asp:Label id="Label1" runat="server" Text='<%# MessageCount %>'>
--------------------------------------------------------------------
2 将 DataTable 绑定到 DataGrid 控件示例
<script language=C# runat=server>
void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
// 创建 DataTable 对象
DataTable dt = new DataTable();
DataRow dr;
// 创建 DataTable 中的 DataColumn 列
dt.Columns.Add(new DataColumn("ID", typeof(System.Int32)));
dt.Columns.Add(new DataColumn("随机数", typeof(System.String)));
dt.Columns.Add(new DataColumn("时间", typeof(System.DateTime)));
// 填充数据到 DataTable 中
for(int i=1; i<9; i++)
{
dr = dt.NewRow();
dr[0] = i;
dr[1] = (new Random()).Next(0, 99);
dr[2] = DateTime.Now;
dt.Rows.Add(dr);
}
// 数据绑定代码
DataGrid1.DataSource = dt;
DataGrid1.DataBind();
}
}
</script>
-----------------------------------------------------
3 将 DataSet 绑定到 DataGrid 控件
myDS.Tables.Add(dt);
DataGrid1.DataSource = myDS;
DataGrid1.DataBind();
-----------------------------------------------------
4 将DataView绑定到DataGrid
dataGrid1.DataSource = new DataView(dt);
dataGrid1.DataBind();
-----------------------------------------------------
5 将 DataReader 做为数据源绑定到 DataGrid 控件
SqlDataReader dr=myCommand.ExecuteReader();
DataGrid1.DataSource = dr;
DataGrid1.DataBind();
dr.Close();
myCommand.Connection.Close();
-----------------------------------------------------
6 如何将表中的列绑定到DropDownList控件
SqlCommand myCommand = new SqlCommand(query, new SqlConnection(ConnStr));
SqlDataAdapter myAdapter = new SqlDataAdapter(myCommand);
DataTable dt = new DataTable();
myAdapter.Fill(dt);
// 数据绑定 DropDownList 控件
DropDownList1.DataSource = dt;
DropDownList1.DataTextField = "ProductName";
DropDownList1.DataValueField = "UnitPrice";
DropDownList1.DataBind();
Label1.Text = "UnitPrice: " + DropDownList1.SelectedValue;
----------------------------------------------------------------------------
7 如何使用DataBinder.Eval()方法进行数据绑定
<script language="C#" runat="server">
void Page_Load(Object semder, EventArgs e)
{
// 创建数据库连接字符串及SqlDataAdapter对象
string ConnStr = System.Configuration.ConfigurationSettings.AppSettings["ConnectionSqlServer_pubs"];
SqlConnection myConnection = new SqlConnection(ConnStr);
SqlDataAdapter myCommand = new SqlDataAdapter("select * from Titles", myConnection);
// 生成DataSet对象并填充数据
DataSet ds = new DataSet();
myCommand.Fill(ds, "Titles");
// 将Repeater控件进行数据绑定
MyRepeater.DataSource = ds.Tables["Titles"].DefaultView;
MyRepeater.DataBind();
}
</script>
-----------------------------------------------
<ItemTemplate>
<tr style="background-color:FFECD8">
<td>
<%# DataBinder.Eval(Container.DataItem, "title") %>
</td>
<td>
<%# DataBinder.Eval(Container.DataItem, "title_id") %>
</td>
<td>
<%# DataBinder.Eval(Container.DataItem, "type") %>
</td>
<td>
<%# DataBinder.Eval(Container.DataItem, "pub_id") %>
</td>
<td>
<%# DataBinder.Eval(Container.DataItem, "price", "$ {0}") %>
</td>
</tr>
</ItemTemplate>
----------------------------------------------------------------------------------------
8 将ArrayList绑定到ListBox控件
ArrayList values = new ArrayList();
values.Add ("北京");
values.Add ("上海");
values.Add ("广州");
values.Add ("深圳");
values.Add ("天津");
values.Add ("济南");
ListBox1.DataSource = values;
ListBox1.DataBind();
----------------------------------------------------------------------------------
void SubmitBtn_Click(Object sender, EventArgs e)
{
Label1.Text = "你的选择: ";
foreach(System.Web.UI.WebControls.ListItem item in ListBox1.Items)
{
if(item.Selected == true)
Label1.Text += item.Text;
}
}
----------------------------------------------------------------------------------------
9 将Hashtable绑定到RadioButtonList Web控件示例
// 创建 Hashtable 对象并填充数据
Hashtable hash = new Hashtable();
hash.Add("北京", "010");
hash.Add("广州", "020");
hash.Add("上海", "021");
hash.Add("天津", "022");
hash.Add("成都", "028");
hash.Add("济南", "0531");
// 进行数据绑定
RadioButtonList1.DataSource = hash;
RadioButtonList1.DataTextField = "Key";
RadioButtonList1.DataValueField = "Value";
RadioButtonList1.DataBind();
---------------------------------------------------------
void SubmitBtn_Click(object sender, System.EventArgs e)
{
Label1.Text = "区号是:" + RadioButtonList1.SelectedValue;
}
--------------------------------------------------------------------------------------------
10 将 XML 文件做为数据源绑定到控件
void Page_Load(object sender, System.EventArgs e)
{
// 创建 DataSet 对象
DataSet myDS = new DataSet();
// 将 XML 文件读入 DataSet
myDS.ReadXml(Server.MapPath("people.xml"));
// 显示 DataSet 中表的名称
Label1.Text = "表的名称是:" + myDS.Tables[0].TableName;
// 绑定到 DataGrid
DataGrid1.DataSource = myDS.Tables[0];
DataGrid1.DataBind();
}
people.xml:
<People>
<Person>
<Name>张三</Name>
<Phone>66666666</Phone>
<Address>北京市海淀区</Address>
</Person>
<Person>
<Name>李四</Name>
<Phone>88888888</Phone>
<Address>北京市丰台区</Address>
</Person>
</People>
----------------------------------------------------------------------------------------