aijava

welcome to my online log ! open java new world! Taste java charm........
posts - 1, comments - 4, trackbacks - 0, articles - 42
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

asp.net 手写代码实现

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>

 

 

 

 


 


只有注册用户登录后才能发表评论。


网站导航:
 
http://freehost13.websamba.com