这个是后台代码。ID是另一个页面传递过来的,不知道为什么当我点击修改的时候显示是修改成功的,实际上数据库还没被修改,悲剧啊
protected void Page_Load(object sender, EventArgs e)
{
uphezuo.Attributes.Add("onclick", @"javascript:if(confirm('你是否真的要修改吗?')){return true;}else{return false;}");
string id = Request.Params["id"];
this.hezuoid.Text = id;
SqlConnection con = new SqlConnection(connectionString);
try
{
con.Open();
SqlCommand cmd = new SqlCommand("select * from cooperation where id='"+hezuoid.Text+"'", con);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
this.hezuoname.Text = dr["name"].ToString();
this.hezuoid.Text = dr["id"].ToString(); }
dr.Dispose();
cmd.Dispose(); }
catch (SqlException ex)
{
throw ex;
}
finally
{
con.Close();
} }
protected void uphezuo_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(connectionString);
con.Open();
StringBuilder sqlStr = new StringBuilder();
sqlStr.Append(" update cooperation set ");
sqlStr.Append(" name='" + hezuoname.Text + "'");
sqlStr.Append(" where id='" + hezuoid.Text + "'");
SqlCommand cmd = new SqlCommand(sqlStr.ToString(), con);
int i = cmd.ExecuteNonQuery();
if (i == 0)
{
lesc.Text = "<script>alert('修改失败,没做任何修改!')</script>";
}
else
{
lesc.Text = "<script>alert('修改成功!')</script>";
Response.AddHeader("refresh", "0");
}
}
protected void Page_Load(object sender, EventArgs e)
{
uphezuo.Attributes.Add("onclick", @"javascript:if(confirm('你是否真的要修改吗?')){return true;}else{return false;}");
string id = Request.Params["id"];
this.hezuoid.Text = id;
SqlConnection con = new SqlConnection(connectionString);
try
{
con.Open();
SqlCommand cmd = new SqlCommand("select * from cooperation where id='"+hezuoid.Text+"'", con);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
this.hezuoname.Text = dr["name"].ToString();
this.hezuoid.Text = dr["id"].ToString(); }
dr.Dispose();
cmd.Dispose(); }
catch (SqlException ex)
{
throw ex;
}
finally
{
con.Close();
} }
protected void uphezuo_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(connectionString);
con.Open();
StringBuilder sqlStr = new StringBuilder();
sqlStr.Append(" update cooperation set ");
sqlStr.Append(" name='" + hezuoname.Text + "'");
sqlStr.Append(" where id='" + hezuoid.Text + "'");
SqlCommand cmd = new SqlCommand(sqlStr.ToString(), con);
int i = cmd.ExecuteNonQuery();
if (i == 0)
{
lesc.Text = "<script>alert('修改失败,没做任何修改!')</script>";
}
else
{
lesc.Text = "<script>alert('修改成功!')</script>";
Response.AddHeader("refresh", "0");
}
}
解决方案 »
- 求解释个正则表达式
- 坑爹的IE8
- 还是刷新页面问题
- Infragistics NetAdvantage里的WebScheduleInfo控件是干什么用的?
- 怎么用asp.net实现自定义标签
- 如何解决 视频文件 过大,播放慢 急!急!
- 用过Infragistics DataChooser控件的请进
- 求助了~~高手指点下-Server Error in '/' Application.错误
- 阅读 利用.Net框架开发应用系统 后一点疑问请教大家
- 我的WebForm设置成框架式的上、下、左三个aspx页面。左边是列表,右边是主功能页面。主功能页面插入一条新的记录后,我想要实现左边列表
- sqlDbtype 枚举值 4 无效
- 项目使用发布网站后,oledb不能再调试了?
设置一断点
看一下sqlStr是多少,注意参数的值
不添加ispostback会影响页面的显示但是不会影响数据库内的update操作我怀疑是他sql语句内参数问题
{}
这个 方法怎么用,忘记了。貌似要用这个方法才能修改面部过也谢谢q107770540的确忘记关闭了,呵呵
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack) //加上此句 后,page_load中只有一个select操作,应该不会影响你接下来的update
{
uphezuo.Attributes.Add("onclick", @"javascript:if(confirm('你是否真的要修改吗?')){return true;}else{return false;}");
string id = Request.Params["id"];
this.hezuoid.Text = id;
SqlConnection con = new SqlConnection(connectionString);
try
{
con.Open();
SqlCommand cmd = new SqlCommand("select * from cooperation where id='"+hezuoid.Text+"'", con);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
this.hezuoname.Text = dr["name"].ToString();
this.hezuoid.Text = dr["id"].ToString(); }
dr.Dispose();
cmd.Dispose(); }
catch (SqlException ex)
{
throw ex;
}
finally
{
con.Close();
}
}
}
{
id = Request.Params["id"]==null?"":Request.Params["id"].ToString();
this.hezuoid.Text = id;
}
public string id
{
get{return ViewState["id"].ToString();}
set{ViewState["id"]=value;}
}
using(SqlConnection con = new SqlConnection(connectionString))
{}
{
if(IsPostBack==false)
{
uphezuo.Attributes.Add("onclick", @"javascript:if(confirm('你是否真的要修改吗?')){return true;}else{return false;}");
string id = Request.Params["id"];
this.hezuoid.Text = id;
SqlConnection con = new SqlConnection(connectionString);
try
{
con.Open();
SqlCommand cmd = new SqlCommand("select * from cooperation where id='"+hezuoid.Text+"'", con);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
this.hezuoname.Text = dr["name"].ToString();
this.hezuoid.Text = dr["id"].ToString(); }
dr.Dispose();
cmd.Dispose(); }
catch (SqlException ex)
{
throw ex;
}
finally
{
con.Close();
}
}
}
果然加了if(!IsPostBack)
方法后可以修改了