菜鸟求助,关于asp数据添加的问题,做一个网上商城的新增问题 ...
求源码,
就是下面的小部分
,其他的搜索,分页,删除,修改都做了,就差新增了,请高手帮忙using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace lab17
{
public partial class goodsInsert : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
getGoods();
}
}
private SqlConnection GreateConnection()
{
SqlConnection cn = new SqlConnection();
cn.ConnectionString = ConfigurationManager.ConnectionStrings["OnlineShopConnectionString"].ConnectionString;
return cn;
}
private DataSet GetGoodsByName(string strGoodsName)
{
SqlConnection cn = GreateConnection();
cn.Open();
SqlCommand cm = new SqlCommand();
= CommandType.StoredProcedure;
= "GoodsGetByName";
cm.Parameters.AddWithValue("@goodsName", strGoodsName);
cm.Connection = cn;
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cm;
DataSet ds = new DataSet();
da.Fill(ds, "tb_Goods");
cn.Close();
return ds;
}
private void getGoods()
{
if (ViewState["goodsname"] == null)
ViewState["goodsname"] = "";
string GoodsName = ViewState["goodsname"].ToString();
DataSet ds = GetGoodsByName(GoodsName);
gdvGoods.DataSource = ds.Tables["tb_Goods"];
gdvGoods.DataBind();
}
protected void gdvGoods_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gdvGoods.PageIndex = e.NewPageIndex;
getGoods();
}
protected void btnSearch_Click(object sender, EventArgs e)
{
ViewState["goodsname"] = txtSearch.Text;
getGoods();
}
protected void gdvGoods_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
SqlConnection cn = GreateConnection();
cn.Open();
SqlCommand cm = new SqlCommand();
= CommandType.StoredProcedure;
= "GoodsDelete";
cm.Parameters.AddWithValue("@goodsID", gdvGoods.DataKeys[e.RowIndex].Value);
cm.Connection = cn;
cm.ExecuteNonQuery();
cn.Close();
getGoods();
}
protected void gdvGoods_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow && (e.Row.RowState & DataControlRowState.Edit) == 0)
{
LinkButton lbtn = (LinkButton)e.Row.Cells[6].Controls[0];
lbtn.Attributes.Add("onclick", "javascript:return confirm('您确定要删除吗?')");
}
}
protected void gdvGoods_RowEditing(object sender, GridViewEditEventArgs e)
{
gdvGoods.EditIndex = e.NewEditIndex;
getGoods();
}
protected void gdvGoods_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
gdvGoods.EditIndex = -1;
getGoods();
}
protected void gdvGoods_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
SqlConnection cn = GreateConnection();
cn.Open();
SqlCommand cm = new SqlCommand();
= CommandType.StoredProcedure;
= "GoodsUpdata";
cm.Parameters.AddWithValue("@goodsID", gdvGoods.DataKeys[e.RowIndex].Value);
cm.Parameters.AddWithValue("@goodsName", ((TextBox)gdvGoods.Rows[e.RowIndex].Cells[0].FindControl("TextBox1")).Text.Trim());
cm.Parameters.AddWithValue("@goodsType", ((TextBox)gdvGoods.Rows[e.RowIndex].Cells[1].Controls[0]).Text.Trim());
cm.Parameters.AddWithValue("@goodsUnitPrice", ((TextBox)gdvGoods.Rows[e.RowIndex].Cells[2].Controls[0]).Text.Trim());
cm.Parameters.AddWithValue("@goodsImageName", ((TextBox)gdvGoods.Rows[e.RowIndex].Cells[3].Controls[0]).Text.Trim());
cm.Parameters.AddWithValue("@sellCount", ((TextBox)gdvGoods.Rows[e.RowIndex].Cells[4].Controls[0]).Text.Trim());
cm.Parameters.AddWithValue("@goodsDate", ((TextBox)gdvGoods.Rows[e.RowIndex].Cells[5].Controls[0]).Text.Trim());
cm.Connection = cn;
cm.ExecuteNonQuery();
gdvGoods.EditIndex = -1;
getGoods();
}