Posted on 2007-05-07 22:31
苑 阅读(324)
评论(0) 编辑 收藏 所属分类:
ASP/ASP.NET
<%@ Page Language="C#" ContentType="text/html" ResponseEncoding="gb2312" EnableEventValidation="false"%>
<%@ import Namespace = "System.Data"%>
<%@ import Namespace = "System.Data.OleDb"%>
<style type="text/css">
td{ font-size:12px}
</style>
<script runat="server" language="c#">
string strSQL = "select * from product";
OleDbConnection mycon;
OleDbCommand mycmd;
public void page_load(Object sender,EventArgs e){
if(!IsPostBack){
string strDSN = "Provider=Microsoft.Jet.OleDb.4.0;Data Source="+Server.MapPath("data.mdb");
try{
mycon = new OleDbConnection(strDSN);
mycmd = new OleDbCommand(strSQL,mycon);
mycon.Open();
datagrid1.DataSource = mycmd.ExecuteReader();
datagrid1.DataBind();
}catch(OleDbException f){
Response.Write("数据库连接出错!");
}
}
}
public void DataGrid_DeleteItem(Object sender,DataGridCommandEventArgs e){
string delCmd = "delete from product where pid=@pid";
mycon = new OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0;Data Source="+Server.MapPath("data.mdb"));
mycmd = new OleDbCommand(delCmd,mycon);
mycmd.Parameters.Add("@pid",datagrid1.DataKeys[e.Item.ItemIndex]);
mycon.Open();
mycmd.ExecuteNonQuery();
Response.Write("<script type='text/javascript'> alert('删除成功完成!'); window.location.href='ConnAcc2.aspx'; </s" + "cript>");
//Response.Write(datagrid1.DataKeys[e.Item.ItemIndex]);
}
public void DataGrid_EditorItem(Object sender,DataGridCommandEventArgs e){
Response.Write(datagrid1.DataKeys[e.Item.ItemIndex]);
}
</script>
<form runat="server">
<asp:DataGrid ID="datagrid1" runat="server" ShowFooter="false" AutoGenerateColumns="false" OnDeleteCommand="DataGrid_DeleteItem" DataKeyField="pid"
OnEditCommand="DataGrid_EditorItem">
<columns>
<asp:BoundColumn DataField="pname" HeaderText="产品名称"></asp:BoundColumn>
<asp:ButtonColumn CommandName="delete" Text="删除" ButtonType="PushButton"></asp:ButtonColumn>
<asp:HyperLinkColumn text="编辑" DataNavigateUrlField="pid" DataNavigateUrlFormatString="Update.aspx?pid={0}"></asp:HyperLinkColumn>
<asp:TemplateColumn>
<itemtemplate>
<asp:HyperLink Text="修改" NavigateUrl='<%#"Update.aspx?id="+DataBinder.Eval(Container.DataItem,"pid")+"&name="+DataBinder.Eval(Container.DataItem,"pname")%>' runat="server">
</asp:HyperLink>
</itemtemplate>
</asp:TemplateColumn>
</columns>
</asp:DataGrid>
</form>