代码很容易理解,但要声明的是,这是参考孔子的vb版改写的,只是因为有的朋友说用c#不好写。
我才写一个供大家参考。在此,谢谢孔子了。
appe_admin.ASPx
<%@ Page language="c#" Codebehind="appe_admin.ASPx.cs" AutoEventWireup="false" Inherits="bzh_home.appe_admin" %>
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
{
///
/// appe_admin 的摘要说明。
///
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);
}
///
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
///
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}

