比较完整的后台文件是:
using System;
using System.Collections;
using
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace WebApplication1
{
/// <summary>
/// gds1 的摘要说明。
/// </summary>
public class gds1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.TextBox editgoods_name;
protected System.Web.UI.WebControls.TextBox editprice;
protected System.Web.UI.WebControls.TextBox editprovider;
protected System.Web.UI.WebControls.TextBox editmore;
protected System.Web.UI.WebControls.TextBox h;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Data.SqlClient.SqlCommand sqlSelectCommand1;
protected System.Data.SqlClient.SqlCommand sqlInsertCommand1;
protected System.Data.SqlClient.SqlCommand sqlEditCommand1;
protected System.Data.SqlClient.SqlCommand sqlCancelCommand1;
protected System.Data.SqlClient.SqlCommand sqlUpdateCommand1;
protected System.Data.SqlClient.SqlCommand sqlDeleteCommand1;
protected System.Data.SqlClient.SqlConnection sqlConnection1;
protected System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;
protected WebApplication1.dsgoods dsgoods1;
protected System.Web.UI.WebControls.HyperLink HyperLink1;
protected System.Web.UI.WebControls.Button Button1;
private void Page_Load(object sender, System.EventArgs e)
{
if (! IsPostBack)
{
Bind();
}
// 在此处放置用户代码以初始化页面
}
public void Bind()
{
sqlDataAdapter1.Fill(dsgoods1);
DataGrid1.DataSource=dsgoods1;
DataGrid1.DataBind();
}
public void Grid1_Edit(Object sender, DataGridCommandEventArgs dgcea)
{
DataGrid1.EditItemIndex = dgcea.Item.ItemIndex;
Bind();
}
public void Grid1_Cancel(Object sender, DataGridCommandEventArgs dgcea)
{
DataGrid1.EditItemIndex = -1;
Bind();
}
public void Grid1_Update(Object sender, DataGridCommandEventArgs e)
{
Textbox h;
int Key=(int)DataGrid1.DataKeys[(int)e.Item.ItemIndex];
String goods_id = " goods_id =" + Key.ToString();
h=(TextBox)e.Item.FindControl("editgoods_name");
String goods_name = " goods_name = ' " + h.Text.Trim() +"
',";
h=(TextBox)e.Item.FindControl("editprice");
String price = " price = ' " + h.Text.Trim() +"
',";
h=(TextBox)e.Item.FindControl("editprovider");
String provider = " provider = ' " + h.Text.Trim() +"
',";
h=(TextBox)e.Item.FindControl("editmore");
String more = " more = ' " + h.Text.Trim() +"
',";
String g = "Update goods set";
g +=goods_name + price + provider + more ;
g +="where goods_id = " + goods_id;
RunSql(g);
DataGrid1_EditItemIndex = -1;
Bind();
}
public void Grid1_Delete(Object sender, DataGridCommandEventArgs e)
{
int Key=(int)DataGrid1.DataKeys[(int)e.Item.ItemIndex];
String goods_id = Key.ToString();
String g = "Delete from goods";
g +="where goods_id = " + goods_id;
RunSql(g);
DataGrid1.EditItemIndex = -1;
Bind();
}
public String RunSql (string vsql)
{
try
{
SqlCommand mycommand = new SqlCommand(vsql,sqlConnection1);
sqlConnection1.Open();
mycommand.ExecuteNonQuery();
sqlConnection1.Close();
}
catch(Exception e)
{
String cdg = "出现异常:" + e.ToString();
messages.Text=cdg;
}
return("OK");
}
窗体设计器生成的代码省略
}