aspx网页代码:
<asp:DataList ID="DataList1" runat="server" DataKeyField ="ProductKey"
CaptionAlign="Bottom" CellPadding="4" EnableTheming="True"
ForeColor="#333333"
onselectedindexchanged="DataList1_SelectedIndexChanged" RepeatColumns="4"
RepeatDirection="Horizontal" oncancelcommand="DataList1_CancelCommand"
ondeletecommand="DataList1_DeleteCommand"
oneditcommand="DataList1_EditCommand" ondatabinding="DataList1_DataBinding"
onitemcreated="DataList1_ItemCreated"
onupdatecommand="DataList1_UpdateCommand" Font-Bold="False" Font-Italic="False"
Font-Overline="False" Font-Strikeout="False" Font-Underline="False"
>
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<AlternatingItemStyle BackColor="White"
ForeColor="#284775" />
<ItemStyle BackColor="#F7F6F3"
ForeColor="#333333" />
<EditItemTemplate>
序号:<asp:TextBox ID="textnum" runat="server" Text='<%# Eval("ProductKey") %>'></asp:TextBox>
<br />
时间:<asp:TextBox ID="texttime" runat="server"
Text='<%# Eval("ProductAlternateKey") %>'></asp:TextBox>
<br />
<asp:LinkButton ID="savebtn" runat="server" CommandName="update">保存</asp:LinkButton>
<asp:LinkButton ID="LinkButton4" runat="server" CommandName="cancel">取消</asp:LinkButton>
</EditItemTemplate>
<SelectedItemStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<HeaderTemplate>
演示头模板
</HeaderTemplate>
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<FooterTemplate>
脚模板
</FooterTemplate>
<SelectedItemTemplate > 序号:<asp:Label ID="num" runat="server" Text='<%# Eval("ProductKey") %>'></asp:Label>
<br />
时间:<asp:Label ID="time" runat="server"
Text='<%# Eval("ProductAlternateKey") %>'></asp:Label>
<br />
数字:<asp:Label ID="Label3" runat="server" Text='<%# Eval("ReorderPoint") %>'></asp:Label>
<br /></SelectedItemTemplate>
<ItemTemplate>
序号:<asp:Label ID="num" runat="server" Text='<%# Eval("ProductKey") %>'></asp:Label>
<br />
时间:<asp:Label ID="time" runat="server"
Text='<%# Eval("ProductAlternateKey") %>'></asp:Label>
<br />
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="edit">修改</asp:LinkButton>
<asp:LinkButton ID="delebtn" runat="server" CommandName="delete">删除</asp:LinkButton>
</ItemTemplate>
</asp:DataList>
cs代码文件:
using System;
using System.Data;
using System.Configuration;
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 _Default : System.Web.UI.Page


{
private void con()

{
string connstring = ConfigurationManager.ConnectionStrings["AdventureWorksDWConnectionString"].ConnectionString;
SqlConnection con = new SqlConnection(connstring);
SqlConnection conn = new SqlConnection();
DataSet ds = new DataSet();
SqlDataAdapter sda = new SqlDataAdapter("select * from DimProduct", con);
sda.Fill(ds, "name");
//SqlDataAdapter sda2 = new SqlDataAdapter("select * from ProspectiveBuyer", con);
//sda2.Fill(ds, "title");
PagedDataSource pds = new PagedDataSource();
pds.DataSource = ds.Tables["name"].DefaultView;
//PagedDataSource aa = new PagedDataSource();
pds.AllowPaging = true;//允许分页
DataList1.DataSource = pds;
DataList1.DataBind();

}

protected void Page_Load(object sender, EventArgs e)

{
if (!IsPostBack)

{
con();
}

}
protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)

{
DataList1.EditItemIndex = e.Item.ItemIndex;
con();

}
protected void DataList1_CancelCommand(object source, DataListCommandEventArgs e)

{
DataList1.EditItemIndex = -1;
con();
}
protected void DataList1_DeleteCommand(object source, DataListCommandEventArgs e)

{
string id = Convert.ToString(DataList1.DataKeys[e.Item.ItemIndex]);
string sqlcmd = "delete from DimProduct where ProductKey='" + id + "'";
string connstring = ConfigurationManager.ConnectionStrings["AdventureWorksDWConnectionString"].ConnectionString;
SqlConnection conn = new SqlConnection(connstring);
SqlCommand cmd = new SqlCommand(sqlcmd, conn);
conn.Open();
cmd.ExecuteNonQuery();
con();
}
protected void DataList1_DataBinding(object sender, EventArgs e)

{

}
protected void DataList1_ItemCreated(object sender, DataListItemEventArgs e)

{

switch (e.Item.ItemType)

{
case ListItemType.Item:
case ListItemType.AlternatingItem:
case ListItemType.SelectedItem:
((LinkButton)(e.Item.FindControl("delebtn"))).Attributes.Add(
"onclick", " return confirm('你确认要删除');");
break;
}
}
protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)

{
string id =Convert.ToString(DataList1.DataKeys[e.Item.ItemIndex]);
string num = ((TextBox)e.Item.FindControl("textnum")).Text;
string time = ((TextBox)e.Item.FindControl("texttime")).Text;
string sqlcmd = "update DimProduct set ProductAlternateKey='" + time + "'where ProductKey='" + id + "'";
string connstring = ConfigurationManager.ConnectionStrings["AdventureWorksDWConnectionString"].ConnectionString;
SqlConnection conn = new SqlConnection(connstring);
SqlCommand cmd = new SqlCommand(sqlcmd, conn);
conn.Open();
cmd.ExecuteNonQuery();
this.DataList1.EditItemIndex = -1;
con();
}
}
posted on 2008-06-06 20:15
默默無語中 阅读(228)
评论(1) 编辑 收藏