假设gridview内有三行123,搜索结果是3,点击编辑后知道变回123,且编辑状态在第一行!这怎么解决?
你代码写错了呗
你好,
不是很懂你的意思啊。你已经在后台调试了,确定已经搜索到了第三行的数据,但是页面跳转到了搜索之前的样子? 是这意思吗? 如果是这样的话,那么就是在你搜索之后绑定gridview 数据的时候绑错了。如果你现在不是这个问题,那么请你再详细描述一下你的问题,然后可以分享一下你的代码。
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bind();
}
//加载
}
private void bind()
{
string sql = string.Format("select t1.GoodsID, t1.GoodsName, t1.GoodsType, t1.Unit, t1.Puchase_Price, t1.Whole_Sale, t1.Safety_Stock, t2.TID, t2.TypeName, t3.UID, t3.UnitName from Goods t1, Type t2, Unit t3 where t1.GoodsType = t2.TID and t1.Unit = t3.UID");
GridView1.DataSource = BLL.BLLManager.BLLList(sql);
this.GridView1.DataKeyNames = new string[] { "GoodsID" };
GridView1.DataBind();
}
// 搜索
protected void find_Click(object sender, EventArgs e)
{
string goodsid = findbox.Text;
string sql = string.Format("select t1.GoodsID,t1.GoodsName,t1.GoodsType,t1.Unit, t2.TypeName,t3.UnitName, t1.Puchase_Price,t1.Whole_Sale,t1.Safety_Stock from Goods t1,Type t2,Unit t3 where GoodsID='{0}' and t1.GoodsType=t2.TID and t1.Unit=t3.UID", goodsid);
GridView1.DataSource = BLL.BLLManager.BLLList(sql);
this.GridView1.DataKeyNames = new string[] { "GoodsID" };
GridView1.DataBind();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
var row = GridView1.Rows[e.RowIndex];
var gid = Convert.ToString(GridView1.DataKeys[e.RowIndex].Values[0].ToString());
string goodsname = ((TextBox)row.FindControl("txtGoodsName")).Text.Trim();
string goodstype = ((DropDownList)GridView1.Rows[e.RowIndex].Cells[2].FindControl("txtGoodsType")).Text;
string unit = ((DropDownList)GridView1.Rows[e.RowIndex].Cells[3].FindControl("txtUnit")).Text;
float puchase_price = float.Parse(((TextBox)row.FindControl("txtPuchase_Price")).Text);
float whole_sale = float.Parse(((TextBox)row.FindControl("txtWhole_Sale")).Text);
string safety_stock = ((TextBox)row.FindControl("txtSafety_Stock")).Text.Trim();
string sql = string.Format(@"update Goods set GoodsName='{0}', GoodsType='{1}',Unit='{2}',Puchase_Price={3},Whole_Sale={4},Safety_Stock={5} where GoodsID='{6}'", goodsname, goodstype, unit, puchase_price, whole_sale, safety_stock, gid);
int s = BLL.BLLManager.BLLisList(sql);
if (s > 0)
{
Response.Write("<script>alert(修改成功)</script>");
this.GridView1.EditIndex = -1;//没有这一句,修改后页面不会自动刷新
bind();
}
else
{
Response.Write("<script>alert(修改失败)</script>");
Response.Write("<script>window.open('Goods.aspx')</script>");
}
}