DataList的用法
1 数据绑定,绑定的方式为
<%# DataBinder.Eval(Container.DataItem,”name") %>
public partial class _Default : System.Web.UI.Page
{
SqlConnection sqlcon;
string strCon = "Data Source=(local);Database=db_04;Uid=sa;Pwd=why20080104019";
protected void Page_Load(object sender, EventArgs e)
{
sqlcon = new SqlConnection(strCon);
string sqlstr = "select top 1* from tb_Member";
SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
DataSet myds = new DataSet();
sqlcon.Open();
myda.Fill(myds, "tb_Member");
DataList1.DataSource = myds;
DataList1.DataBind();
DataRowView mydrv = myds.Tables["tb_Member"].DefaultView[0];//ds中缓存的表格“tb_Member”
for (int i = 0; i <= DataList1.Items.Count - 1; i++)
{
Label lab = (Label)DataList1.Items[i].FindControl("Label1");
int money = Convert.ToInt32(mydrv["money"]);
lab.Text = money.ToString("¥#,###.00");
}
sqlcon.Close();
}
}
2.分页功能
int curpage = Convert.ToInt32(this.Label2.Text.ToString().Trim());
PagedDataSource ps = new PagedDataSource();
sqlcon = new SqlConnection(strcon);
SqlDataAdapter sda = new SqlDataAdapter("select * from tb_GoodsType", sqlcon);
DataSet ds = new DataSet();
sda.Fill(ds, "tb_GoodsType");
ps.DataSource = ds.Tables["tb_GoodsType"].DefaultView;
ps.AllowPaging = true;
ps.PageSize = 2;//实现分页
LinkButton1.Enabled = true;
LinkButton2.Enabled = true;
LinkButton3.Enabled = true;
LinkButton4.Enabled = true;
ps.CurrentPageIndex = curpage-1;
if (curpage == 1)
{
this.LinkButton1.Enabled = false;
this.LinkButton2.Enabled = false;
}
if (curpage == ps.PageCount)
{
this.LinkButton3.Enabled = false;
this.LinkButton4.Enabled = false;
}
this.Label3.Text = Convert.ToString(ps.PageCount);
DataList1.DataSource = ps;
this.DataList1.DataKeyField = "GoodsTypeID";
DataList1.DataBind();
}
2.查找Datalist中的控件
CheckBox cb=(CheckBox)Datalist.Item[0].FindControl(“CheckBox
Datalist的属性是:
<asp:DataList ID="DataList1" runat="server" BorderColor="#FFFF99"
BorderWidth="2px" onitemdatabound="DataList1_ItemDataBound" Height="400px"
ondeletecommand="DataList1_DeleteCommand" Width="300px">
<ItemTemplate>
<table class="style1">
<tr>
<td>
</td>
<td class="style2">
<span lang="zh-cn">ID:</span></td>
<td>
<asp:Label ID="Label1" runat="server"
Text='<%# DataBinder.Eval(Container.DataItem,"ID") %>'></asp:Label>
</td>
</tr>
<tr>
<td>
</td>
<td class="style2">
<span lang="zh-cn">姓名:</span></td>
<td>
<asp:Label ID="Label2" runat="server"
Text='<%# DataBinder.Eval(Container.DataItem,"au_lname") %>'></asp:Label>
</td>
</tr>
<tr>
<td>
</td>
<td class="style2">
<span lang="zh-cn">性别:</span></td>
<td>
<asp:DropDownList ID="DropDownList1" runat="server" DataTextField="sex"
DataValueField="sex">
<asp:ListItem>男</asp:ListItem>
<asp:ListItem>女</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td class="style3">
<asp:CheckBox ID="cb1" runat="server" Text="勾选此项" AutoPostBack="True" />
</td>
<td class="style4">
<span lang="zh-cn">学位:</span></td>
<td class="style3">
<asp:Label ID="Label3" runat="server"
Text='<%# DataBinder.Eval(Container.DataItem,"state") %>'></asp:Label>
</td>
</tr>
<tr>
<td>
</td>
<td class="style2">
<span lang="zh-cn">电话:</span></td>
<td>
<asp:Label ID="Label4" runat="server"
Text='<%# DataBinder.Eval(Container.DataItem,"phone") %>'></asp:Label>
</td>
</tr>
<tr>
<td>
</td>
<td class="style2">
<span lang="zh-cn">地址:</span></td>
<td>
<asp:Label ID="Label5" runat="server"
Text='<%# DataBinder.Eval(Container.DataItem,"address") %>'></asp:Label>
</td>
</tr>
<tr><td></td>
<td class="style2">
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="delete">删除该项目</asp:LinkButton>
</td>
<td></td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
具体的代码是:
protected void DataList1_DeleteCommand(object source, DataListCommandEventArgs e)
{
SqlCon = new SqlConnection(strcon);
for (int i = 0; i < DataList1.Items.Count; i++)
{
CheckBox cb = (CheckBox)DataList1.Items[i].FindControl("cb1");
if (cb.Checked)
{
string strdel = "delete from tb_mrEmp05 where ID='" + DataList1.DataKeys[i].ToString() + "'";
SqlCon.Open();
SqlCommand sqlcmd = new SqlCommand(strdel, SqlCon);
sqlcmd.ExecuteNonQuery();
SqlCon.Close();
}
else
{
Response.Write("请选择!");
}
}
bind();
}
3.Datalist实现自动编号功能
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
int id = e.Item.ItemIndex+1;
((Label)e.Item.FindControl("Label3")).Text = id.ToString();
}
4.Datalist实现删除功能
在Datalist中的Button控件的Commandname必须设置为delete
protected void DataList1_DeleteCommand(object source, DataListCommandEventArgs e)
{
string strdel="delete from tb_Revert where RevertID='"+DataList1.DataKeys[e.Item.ItemIndex].ToString()+"'";
sqlcon=new SqlConnection(strcon);
sqlcon.Open();
SqlCommand sqlcmd=new SqlCommand(strdel,sqlcon);
sqlcmd.ExecuteNonQuery();
sqlcon.Close();
bind();
}
5.Datalist实现主细表
Datalist的详细属性是:
<asp:DataList ID="DataList1" runat="server" BackColor="#DEBA84"
BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3"
CellSpacing="2" GridLines="Both">
<FooterStyle BackColor="#F7DFB5" ForeColor="#
<ItemStyle BackColor="#FFF7E7" ForeColor="#
<SelectedItemStyle BackColor="#
<HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
<ItemTemplate>
<table class="style1">
<tr>
<td>
<span lang="zh-cn">员工ID:</span><asp:Label ID="Label1" runat="server"
Text='<%# DataBinder.Eval(Container.DataItem,"UserID") %>'></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:DataList ID="DataList2" runat="server" DataSource='<%#((System.Data.DataRowView)Container.DataItem).CreateChildView("tableRelation") %>' DataKeyField="UserID">
<ItemTemplate>
UserID:
<asp:Label ID="UserIDLabel" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"UserID") %>' />
UserLoginName:
<asp:Label ID="UserLoginNameLabel" runat="server"
Text='<%# DataBinder.Eval(Container.DataItem,"UserLoginName") %>' />
UserSex:
<asp:Label ID="UserSexLabel" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"UserSex") %>' />
<br />
UserPwd:
<asp:Label ID="UserPwdLabel" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"UserPwd") %>' />
UserName:
<asp:Label ID="UserNameLabel" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"UserName") %>' />
<br />
UserQuePwd:
<asp:Label ID="UserQuePwdLabel" runat="server"
Text='<%# DataBinder.Eval(Container.DataItem,"UserQuePwd") %>' />
UserAnsPwd:
<asp:Label ID="UserAnsPwdLabel" runat="server"
Text='<%# DataBinder.Eval(Container.DataItem,"UserAnsPwd") %>' />
<br />
UserTel:
<asp:Label ID="UserTelLabel" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"UserTel") %>' />
UserEmail:
<asp:Label ID="UserEmailLabel" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"UserEmail") %>' />
<br />
UserAddress:
<asp:Label ID="UserAddressLabel" runat="server"
Text='<%# DataBinder.Eval(Container.DataItem,"UserAddress") %>' />
UserPostCode:
<asp:Label ID="UserPostCodeLabel" runat="server"
Text='<%# DataBinder.Eval(Container.DataItem,"UserPostCode") %>' />
<br />
UserQQ:
<asp:Label ID="UserQQLabel" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"UserQQ") %>' />
UserDate:
<asp:Label ID="UserDateLabel" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"UserDate") %>' />
</ItemTemplate>
</asp:DataList></td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
后台代码是:
protected void Page_Load(object sender, EventArgs e)
{
string strselect = "select top 3* from tb_EmpID select top
SqlConnection sqlcon = new SqlConnection("Data Source=localhost;Initial Catalog=db_04;uid=sa;pwd=why20080104019");
sqlcon.Open();
SqlDataAdapter sda = new SqlDataAdapter(strselect, sqlcon);
DataSet ds = new DataSet();
sda.Fill(ds);
ds.Tables[0].TableName = "tb_EmpID";
ds.Tables[1].TableName = "tb_Employee";
DataColumn Parent = ds.Tables["tb_EmpID"].Columns["UserID"];
DataColumn Child = ds.Tables["tb_Employee"].Columns["UserID"];
DataRelation tableRelation = new DataRelation("tableRelation", Parent, Child,false);
ds.Relations.Add(tableRelation);
DataList1.DataSource = ds.Tables["tb_EmpID"].DefaultView;
DataList1.DataBind();
sqlcon.Close();
}
6.Datalist实现编辑功能:
.Datalist的属性为:
<asp:DataList ID="DataList1" runat="server"
oncancelcommand="DataList1_CancelCommand" oneditcommand="DataList1_EditCommand"
onupdatecommand="DataList1_UpdateCommand">
<EditItemTemplate>
<table class="style2">
<tr>
<td>
<span lang="zh-cn">类型编号</span></td>
<td>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("GoodsTypeID") %>'></asp:Label>
</td>
</tr>
<tr>
<td>
<span lang="zh-cn">类型名称</span></td>
<td>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("GoodsTypeName") %>'></asp:TextBox>
</td>
</tr>
<tr>
<td>
<span lang="zh-cn">商店名称</span></td>
<td>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Eval("StoreID") %>'></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Button ID="Button2" runat="server" Text=" 更新" CommandName="update" />
</td> <td>
<asp:Button ID="Button3" runat="server" CommandName="cancel" Text="取消"
onclick="Button3_Click" />
</td>
</tr>
</table>
</EditItemTemplate>
<ItemTemplate>
<table class="style1">
<tr>
<td>
<span lang="zh-cn">姓名:</span></td>
<td>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("GoodsTypeID") %>'></asp:Label>
</td>
<td>
<asp:Button ID="Button1" runat="server" CommandName="edit" Text="编辑" />
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
后台代码是:
protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)//进入编辑状态
{
DataList1.EditItemIndex = e.Item.ItemIndex;
bind();
}
protected void DataList1_CancelCommand(object source, DataListCommandEventArgs e)//取消编辑 {
DataList1.EditItemIndex = -1;
bind();
}
protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)//实现更新
{
string id = DataList1.DataKeys[e.Item.ItemIndex].ToString();
string strname = ((TextBox)e.Item.FindControl("TextBox1")).Text;
string strstore = ((TextBox)e.Item.FindControl("TextBox2")).Text;
string strupdate = "update tb_GoodsType set GoodsTypeName='"+strname+"',StoreID='"+strstore+"' where GoodsTypeID='"+id+"'";
sqlcon = new SqlConnection(strcon);
SqlCommand sqlcmd = new SqlCommand(strupdate, sqlcon);
sqlcon.Open();
sqlcmd.ExecuteNonQuery();
sqlcon.Close();
DataList1.EditItemIndex = -1;
bind();
}
}
7.DataList的查看详细信息功能
:DataList的属性为:
asp:DataList ID="DataList1" runat="server"
onitemcommand="DataList1_ItemCommand">
<ItemTemplate>
<table class="style1">
<tr>
<td>
商品<span lang="zh-cn">编号:</span></td>
<td>
<asp:LinkButton ID="LinkButton1" runat="server"
Text='<%# Eval("GoodsTypeName") %>' CommandName="select"></asp:LinkButton>
</td>
</tr>
</table>
</ItemTemplate>
<SelectedItemTemplate>
<table class="style1">
<tr>
<td class="style2">
<span lang="zh-cn">类型编号:</span></td>
<td>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("GoodsTypeID") %>'></asp:Label>
</td>
<td>
</td>
</tr>
<tr>
<td class="style2">
<span lang="zh-cn">类型名称:</span></td>
<td>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("GoodsTypeName") %>'></asp:Label>
</td>
<td>
</td>
</tr>
<tr>
<td class="style2">
<span lang="zh-cn"> 供应商:</span></td>
<td>
<asp:Label ID="Label3" runat="server" Text='<%# Eval("StoreID") %>'></asp:Label>
</td>
<td>
</td>
</tr>
<tr>
</td>
<td class="style2">
<td>
<asp:LinkButton ID="LinkButton2" runat="server" CommandName="back">返回</asp:LinkButton>
</td>
</td>
<td>
</tr>
</table>
</SelectedItemTemplate>
</asp:DataList>
后台代码是:
public partial class Default6 : System.Web.UI.Page
{
SqlConnection sqlcon;
string strcon = "Data Source=localhost;Initial Catalog=db_04;uid=sa;pwd=why20080104019";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bind();
}
}
protected void bind()
{
sqlcon = new SqlConnection(strcon);
SqlDataAdapter sda = new SqlDataAdapter("select * from tb_GoodsType", sqlcon);
DataSet ds = new DataSet();
sda.Fill(ds, "tb_GoodsType");
DataList1.DataSource = ds;
DataList1.DataKeyField = "GoodsTypeID";
DataList1.DataBind();
}
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "back")
{
DataList1.SelectedIndex = -1;
bind();
}
if (e.CommandName == "select")
{
DataList1.SelectedIndex = e.Item.ItemIndex;
bind();
}
}