菜鸟求助,关于asp数据添加的问题,做一个网上商城的新增问题 ...
求源码,[attach]34596[/attach]就是下面的小部分[attach]34597[/attach],其他的搜索,分页,删除,修改都做了,就差新增了,请高手帮忙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();
cm.CommandType = CommandType.StoredProcedure;
cm.CommandText = "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();
cm.CommandType = CommandType.StoredProcedure;
cm.CommandText = "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();
cm.CommandType = CommandType.StoredProcedure;
cm.CommandText = "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();
} lz既然能做出来修改那么做新增就简单多了啊……
在你的新增的按钮的click事件中直接将修改的代码复制过来,再加上一些判断是否为空,是否已经存在等的操作,然后将修改的sql语句改成insert into就ok了啊…… 其实这都是老师教的,老师叫我们自己做'新增'的,不会啊,还有图片上传的
[tk39] lz还是自己做吧,其实不难的,具体的方法我都在上面说的很清楚了啊……
努力,相信你能做出来的…… 努力
[tk05] 看看书就可以了,努力,就是在新增的事件中,插入新数据和上传图片就行了。
页:
[1]
