DataList01.asp代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DataList01.aspx.cs" Inherits="DataList01" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>DataList控件进行数据访问</title>
</head>
<body>
<form id="form1" runat="server">
<table id="TABLE1" runat="server" border="0" align=center style="width: 570px; height: 340px">
<tr>
<td align="center" colspan="2">
综合应用DataList进行数据访问</td>
</tr>
<tr>
<td style="width: 50%">
选择布局方式:</td>
<td style="width: 50%">
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem>表</asp:ListItem>
<asp:ListItem>流</asp:ListItem>
</asp:DropDownList></td>
</tr>
<tr>
<td style="width: 50%">
选择显示方向:</td>
<td style="width: 50%">
<asp:DropDownList ID="DropDownList2" runat="server">
<asp:ListItem>垂直</asp:ListItem>
<asp:ListItem>水平</asp:ListItem>
</asp:DropDownList></td>
</tr>
<tr>
<td style="width: 50%">
选择每行行数:</td>
<td style="width: 50%">
<asp:DropDownList ID="DropDownList3" runat="server">
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
<asp:ListItem>4</asp:ListItem>
</asp:DropDownList></td>
</tr>
<tr>
<td style="width: 50%">
显示表格边框:</td>
<td style="width: 50%">
<asp:CheckBox ID="CheckBox1" runat="server" /></td>
</tr>
<tr>
<td align="center" colspan="2">
<asp:Button ID="Button1" runat="server" Text="刷新DataList的显示" OnClick="Button1_Click" /></td>
</tr>
<tr>
<td colspan="2" align="center">
<asp:DataList ID="DataList1" runat="server" Height="152px" ShowFooter="False" Width="267px" OnCancelCommand="DataList1_CancelCommand" OnEditCommand="DataList1_EditCommand" OnItemCommand="DataList1_ItemCommand" OnUpdateCommand="DataList1_UpdateCommand">
<EditItemStyle BackColor="#FFFF99" />
<SelectedItemStyle BackColor="#FFFFCC" Font-Bold="True" />
<ItemTemplate>
<%#DataBinder.Eval(Container.DataItem,"ProductID") %>
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="select" Font-Size="X-Small"
Height="18px" Width="29px">选择</asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CommandName="edit" Font-Size="X-Small"
Height="19px" Width="81px">编辑</asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
商品ID:<asp:Label ID="Label1" runat="server" Height="19px" Width="54px" Text='<%#DataBinder.Eval(Container.DataItem,"ProductID") %>'></asp:Label><br />
数量:<asp:TextBox ID="TextBox1" runat="server" Height="13px" Width="77px" Text='<%#DataBinder.Eval(Container.DataItem,"UnitsInStock") %>'></asp:TextBox><br />
单价:<asp:TextBox ID="TextBox2" runat="server" Height="13px" Width="77px" Text='<%#DataBinder.Eval(Container.DataItem,"UnitPrice") %>'></asp:TextBox><br />
<asp:LinkButton ID="LinkButton4" runat="server" CommandName="update" Font-Size="X-Small">更新</asp:LinkButton>
<asp:LinkButton ID="LinkButton5" runat="server" CommandName="cancel" Font-Size="X-Small">取消</asp:LinkButton>
</EditItemTemplate>
<ItemStyle BackColor="#CCFFFF" />
<HeaderTemplate>
商品库存列表
</HeaderTemplate>
<HeaderStyle Font-Bold="True" ForeColor="Blue" />
<SelectedItemTemplate>
<p></p><font face="宋体">商品ID:<%#DataBinder.Eval(Container.DataItem,"ProductID") %></font>
<p></p><font face="宋体">数量:<%#DataBinder.Eval(Container.DataItem,"UnitsInStock") %></font>
<p></p><font face="宋体">单价:<%#DataBinder.Eval(Container.DataItem,"UnitPrice") %></font>
<asp:LinkButton ID="LinkButton3" runat="server" CommandName="unselect" Font-Size="X-Small">取消</asp:LinkButton>
</SelectedItemTemplate>
</asp:DataList></td>
</tr>
</table>
</form>
</body>
</html>
DataList01.asp.cs代码:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class DataList01 : System.Web.UI.Page
{
public DataSet ds = new DataSet();
protected void BindData()
{
SqlConnection myConnection = new SqlConnection("server=localhost;uid=sa;pwd=860712;database=Northwind");
string strSQL = "select * from Products";
SqlDataAdapter myCommand = new SqlDataAdapter(strSQL, myConnection);
myCommand.Fill(ds, "MyProducts");
DataList1.DataSource = ds.Tables["MyProducts"].DefaultView;
DataList1.DataBind();
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindData();
}
}
protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)
{
DataList1.EditItemIndex = (int)e.Item.ItemIndex;
BindData();
}
protected void DataList1_CancelCommand(object source, DataListCommandEventArgs e)
{
DataList1.EditItemIndex = -1;
BindData();
}
protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
{
string item = ((Label)e.Item.FindControl("Label1")).Text;
int qty = int.Parse(((TextBox)e.Item.FindControl("TextBox1")).Text.ToString());
float price = float.Parse(((TextBox)e.Item.FindControl("TextBox2")).Text.ToString());
SqlConnection myConnection1 = new SqlConnection("server=localhost;uid=sa;pwd=860712;database=Northwind");
string strSQL = "update Products set UnitsInStock=" + qty + ",UnitPrice=" + price + " where ProductID='" + item + "'";
SqlCommand myCommand1 = new SqlCommand(strSQL, myConnection1);
myConnection1.Open();
myCommand1.ExecuteNonQuery();
myConnection1.Close();
DataList1.EditItemIndex = -1;
BindData();
}
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
if (((LinkButton)e.CommandSource).CommandName == "select")
{
DataList1.SelectedIndex = e.Item.ItemIndex;
BindData();
}
else
{
if (((LinkButton)e.CommandSource).CommandName == "unselect")
{
DataList1.SelectedIndex = -1;
BindData();
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
if (DropDownList1.SelectedIndex == 0)
DataList1.RepeatLayout = RepeatLayout.Table;
else
DataList1.RepeatLayout = RepeatLayout.Flow;
if (DropDownList2.SelectedIndex == 0)
DataList1.RepeatDirection = RepeatDirection.Vertical;
else
DataList1.RepeatDirection = RepeatDirection.Horizontal;
DataList1.RepeatColumns = DropDownList3.SelectedIndex + 1;
if ((CheckBox1.Checked == true) && (DataList1.RepeatLayout == RepeatLayout.Table))
{
DataList1.BorderWidth = Unit.Pixel(1);
DataList1.GridLines = GridLines.Both;//显示表格边框
}
else
{
DataList1.BorderWidth = Unit.Pixel(0);
DataList1.GridLines = GridLines.None;//不显示表格边框
}
}
}