各位大侠帮帮忙啊,我在GirdView下做了个搜索功能,想在搜索出来的行进行编辑,可是会跳转到首页编辑,这是为什么?困扰了我很久了!

yezhubaobao123 2012-03-15 02:45:51
前台代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Utility;
namespace WebShopBackground
{
public partial class revise_p : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GridViewBind();
}
}
protected void Bind()
{
GridView1.DataSource = BLL.WBp_detailsBLL.GetInfo();
GridView1.DataBind();
}
private void GridViewBind()
{
try
{
DataSet ds = BLL.WBp_detailsBLL.GetInfo();
this.GridView1.DataSource = ds;
GridView1.DataBind();
}
catch (Exception ex)
{
Response.Write("数据库错误,错误原因:" + ex.Message);
Response.End();
}
}

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
Bind();
}

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
GridView1.DataBind();
Bind();
}

protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
GridView1.DataBind();
Bind();
}

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int id = Convert.ToInt32((GridView1.Rows[e.RowIndex].FindControl("Label2") as Label).Text);
string strSql = "delete from product where pid="+id ;
try
{
SqlHelper.ExecuteNonQuery(SqlHelper.CONN_STRING, CommandType.Text, strSql);
GridView1.EditIndex = -1;
GridView1.DataBind();
Bind();
}
catch (Exception ex)
{
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script type='text/javascript'>alert('删除失败,请查找原因')</script>");
Response.End();
}
}

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//string id = GridView1.DataKeys[e.RowIndex].Values[1].ToString();
int id = Convert.ToInt32((GridView1.Rows[e.RowIndex].FindControl("Label2") as Label).Text);
string Name = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox3")).Text;
string jieshao = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox4")).Text;
string pingpai = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox5")).Text;
string yuanjia = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox6")).Text;
string zhegoujia = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox7")).Text;
string shifouxinpin = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox8")).Text;
string guige = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox9")).Text;
string strSql = "update product set productname='" + Name + "',productintroduce='" + jieshao + "',productchu='" + pingpai + "',masterprice='" + yuanjia + "',hotprice='" + zhegoujia + "',newproduct='" + shifouxinpin + "',guige='" + guige + "' where pid=" + id;
try
{

SqlHelper.ExecuteNonQuery(SqlHelper.CONN_STRING, CommandType.Text, strSql);
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script type='text/javascript'>alert('更新成功')</script>");
GridView1.EditIndex = -1;
GridView1.DataBind();
Bind();
}
catch (Exception ex)
{
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script type='text/javascript'>alert('更新失败,请查找原因')</script>");
Response.End();
}
}

protected void Button1_Click(object sender, EventArgs e)
{
try
{
string bianhao = txtbianhao.Text;
DataSet ds = BLL.WBp_detailsBLL.Selectbianhao(bianhao);
GridView1.DataSource = ds;
GridView1.DataBind();
}
catch (Exception ex)
{
Response.Write("查询失败,请查找原因:" + ex.Message);
Response.End();
}
}

protected void Button3_Click(object sender, EventArgs e)
{
try
{
string mingcheng = txtmingcheng.Text;
DataSet ds = BLL.WBp_detailsBLL.Selectmingcheng(mingcheng);
GridView1.DataSource = ds;
GridView1.DataBind();
}
catch (Exception ex)
{
Response.Write("查询失败,请查找原因:" + ex.Message);
Response.End();
}
}

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//int i;
////执行循环,保证每条数据都可以更新
//for (i = 0; i < GridView1.Rows.Count; i++)
//{
// //首先判断是否是数据行
// if (e.Row.RowType == DataControlRowType.DataRow)
// {
// //当鼠标停留时更改背景色
// e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#00A9FF'");
// //当鼠标移开时还原背景色
// e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
// }
//}
if (e.Row.RowType == DataControlRowType.DataRow)
{
//鼠标经过时,行背景色变
e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#3399CC'");
//鼠标移出时,行背景色变
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#FFFFFF'");
}

}

}
}
...全文
65 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
烟波钓 2012-03-15
  • 打赏
  • 举报
回复
我去 你是要干啥了?
yezhubaobao123 2012-03-15
  • 打赏
  • 举报
回复
没人么
yezhubaobao123 2012-03-15
  • 打赏
  • 举报
回复
就是跳回了GridView的第一页进行编辑了!
yezhubaobao123 2012-03-15
  • 打赏
  • 举报
回复
打错了啊,这是后台代码
烟波钓 2012-03-15
  • 打赏
  • 举报
回复
[Quote=引用楼主 yezhubaobao123 的回复:]
前台代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Utility;
namespace Web……
[/Quote]
神马首页编辑?
yezhubaobao123 2012-03-15
  • 打赏
  • 举报
回复
这是后台代码,打错了!
yezhubaobao123 2012-03-15
  • 打赏
  • 举报
回复
在线等,请大侠帮忙啊

62,268

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧