代码很容易理解,但要声明的是,这是参考孔子的vb版改写的,只是因为有的朋友说用c#不好写。
我才写一个供大家参考。在此,谢谢孔子了。
appe_admin.aspx
<%@ page language="c#" codebehind="appe_admin.aspx.cs" autoeventwireup="false" inherits="bzh_home.appe_admin" %>
<!doctype html public "-//w3c//dtd html 4.0 transitional//en" >
<html>
<head>
<title>appe_admin</title>
<meta name="generator" content="microsoft visual studio .net 7.1">
<meta name="code_language" content="c#">
<meta name="vs_defaultclientscript" content="javascript">
<meta name="vs_targetschema" content="http://schemas.microsoft.com/intellisense/ie5">
</head>
<body ms_positioning="gridlayout">
<form id="form1" method="post" runat="server">
<font face="宋体"></font>
<asp:datagrid id="datagrid1" runat="server" autogeneratecolumns="false" showfooter="true" onitemcommand="itemsgrid_command"
cellpadding="4" backcolor="white" borderwidth="1px" borderstyle="none" bordercolor="#cc9966">
<selecteditemstyle font-bold="true" forecolor="#663399" backcolor="#ffcc66"></selecteditemstyle>
<itemstyle forecolor="#330099" backcolor="white"></itemstyle>
<headerstyle font-bold="true" forecolor="#ffffcc" backcolor="#990000"></headerstyle>
<footerstyle forecolor="#330099" backcolor="#ffffcc"></footerstyle>
<columns>
<asp:templatecolumn headertext="employee id">
<itemtemplate>
<asp:label id=label3 runat="server" text=<%# databinder.eval(container, "dataitem.编号") %>>
</asp:label>
</itemtemplate>
<footertemplate>
<asp:linkbutton id="linkbutton1" runat="server" commandname="insert">insert</asp:linkbutton>
</footertemplate>
<edititemtemplate>
<asp:textbox id=textbox5 runat="server" text=<%# databinder.eval(container, "dataitem.编号") %>>
</asp:textbox>
</edititemtemplate>
</asp:templatecolumn>
<asp:templatecolumn headertext="last name">
<itemtemplate>
<asp:label id=label1 runat="server" text=<%# databinder.eval(container, "dataitem.用户名") %>>
</asp:label>
</itemtemplate>
<footertemplate>
<asp:textbox id="textbox2" runat="server"></asp:textbox>
</footertemplate>
<edititemtemplate>
<asp:textbox id="textbox1" runat="server"></asp:textbox>
</edititemtemplate>
</asp:templatecolumn>
<asp:templatecolumn headertext="first name">
<itemtemplate>
<asp:label id=label2 runat="server" text=<%# databinder.eval(container, "dataitem.密码") %>>
</asp:label>
</itemtemplate>
<footertemplate>
<asp:textbox id="textbox4" runat="server"></asp:textbox>
</footertemplate>
<edititemtemplate>
<asp:textbox id="textbox3" runat="server"></asp:textbox>
</edititemtemplate>
</asp:templatecolumn>
</columns>
<pagerstyle horizontalalign="center" forecolor="#330099" backcolor="#ffffcc"></pagerstyle>
</asp:datagrid>
</form>
</body>
</html>
appe_admin.aspx.cs
using system;
using system.collections;
using system.componentmodel;
using system.data;
using system.data.sqlclient;
using system.drawing;
using system.web;
using system.web.sessionstate;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.htmlcontrols;
namespace bzh_home
{
/// <summary>
/// appe_admin 的摘要说明。
/// </summary>
public class appe_admin : system.web.ui.page
{
protected system.web.ui.webcontrols.datagrid datagrid1;
string connstr = "user id=sa;data source=\"xidongs\\datamanage\";initial catalog=bzh_data";
private void page_load(object sender, system.eventargs e)
{
// 在此处放置用户代码以初始化页面
if(!page.ispostback){
bindgrid();
}
}
private void bindgrid(){
sqlconnection cnn = new sqlconnection(connstr);
sqldataadapter da = new sqldataadapter("select * from admin", cnn);
dataset ds = new dataset();
da.fill(ds,"admin");
this.datagrid1.datasource = ds;
this.datagrid1.databind();
}
public void itemsgrid_command(object sender, datagridcommandeventargs e)
{
if(e.commandname == "insert")
{
//this.page.response.write("ss");
sqlconnection cnn = new sqlconnection(connstr);
textbox t1 = (textbox)e.item.findcontrol("textbox2");
textbox t2 = (textbox)e.item.findcontrol("textbox4");
cnn.open();
sqlcommand cmd = new sqlcommand("insert into admin(用户名,密码) values(" + t1.text + "," + t2.text + ")", cnn);
cmd.executenonquery();
cnn.close();
bindgrid();
}
}
#region web 窗体设计器生成的代码
override protected void oninit(eventargs e)
{
//
// codegen: 该调用是 asp.net web 窗体设计器所必需的。
//
initializecomponent();
base.oninit(e);
}
/// <summary>
/// 设计器支持所需的方法 – 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void initializecomponent()
{
this.load += new system.eventhandler(this.page_load);
}
#endregion
}
}