关于IsPostBack操作数据库的问题

Jamescow 2012-06-07 02:11:09
有个简单的页面,刚打开时从数据中获取一个记录。
然后给用户修改
修改完后按保存。
这在WINFORM里是没问题的,可是WEBFORM就总是刚打开的值。貌似IsPostBack的问题,可无论我怎加IsPostBack,都没效
源码如下

protected void Page_Load(object sender, EventArgs e)
{

if (!IsPostBack)
{

DataSet ds = datas.Query("select * from intro where id=1");
intro.Text = ds.Tables[0].Rows[0]["intro"].ToString();
maxim.Text = ds.Tables[0].Rows[0]["maxim"].ToString();
address.Text = ds.Tables[0].Rows[0]["address"].ToString();
bot_zongzhi.Text = ds.Tables[0].Rows[0]["bot_zongzhi"].ToString();
bot_linian.Text = ds.Tables[0].Rows[0]["bot_linian"].ToString();
bot_yuanfeng.Text = ds.Tables[0].Rows[0]["bot_yuanfeng"].ToString();
bot_shide.Text = ds.Tables[0].Rows[0]["bot_shide"].ToString();
bot_mubiao.Text = ds.Tables[0].Rows[0]["bot_mubiao"].ToString();
msg.Text = IsPostBack.ToString();
}


}
protected void saved_Click(object sender, EventArgs e)
{

string sql = @"update intro set
intro='" + intro.Text + "'," +
"maxim='" + maxim.Text + "'," +
"address='" + address.Text + "'," +
"bot_zongzhi='" + bot_zongzhi.Text + "'," +
"bot_linian='" + bot_linian.Text + "'," +
"bot_yuanfeng='" + bot_yuanfeng.Text + "'," +
"bot_shide='" + bot_shide.Text + "'," +
"bot_mubiao='" + bot_mubiao.Text + "' where id=1";
if (datas.ExecuteSQL(sql) > 0)
{
msg.Text = "成功保存" + sql;
}
else
{
msg.Text = "保存失败";
}

}

在界面设计li 我只加了TEXTBOX,和一个保存按钮。其他没加。
...全文
223 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
东_子 2012-06-12
  • 打赏
  • 举报
回复
if(!IsPostBack)与这个msg.Text = IsPostBack.ToString();
是不是不合理啊?
whf496267900 2012-06-12
  • 打赏
  • 举报
回复
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="index_content.aspx.cs" Inherits="admin_index_content" %>

<form id="form1" runat="server" method="post" action="index_content.aspx">

public partial class admin_index_content : System.Web.UI.Page
{}

你是不是吧CodeFile的设置改了????
wuyq11 2012-06-12
  • 打赏
  • 举报
回复
if(!IsPostBack){
//绑定数据
}
单步查看sql:update语句
牛哥_ 2012-06-12
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]

我简化一下写我的代码完整贴出来吧
在ABC.ASPX文件里,我写如下代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="index_content.aspx.cs" Inherits="admin_index_content" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0……
[/Quote]
if(IsPostBack)缺了 if(!IsPostBack)
确定sql语句在数据库里面能执行,
修改后 在从新绑定就行了
wulei_ciety 2012-06-12
  • 打赏
  • 举报
回复
1.你首先确定你数据库里有ID为1的这条数据吗,2.加断点在后台SQL语句那边,请把SQL语句打出来查看下,看看页面的参数有没有取到
hhwolf76 2012-06-12
  • 打赏
  • 举报
回复
有没有commit?
net5354 2012-06-12
  • 打赏
  • 举报
回复
你根本未运行更新语句~!
Jamescow 2012-06-11
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]

saved_Click把这个方法里面的sql设置一个断点,然后看看获取到的值是不是你修改后的值,然后把这条SQL语句复制到数据库里面的查询分析器里面执行下,看看效果。
[/Quote]

我前面说了,在SQL那加了
msg.Text = "成功保存" + sql;//---这一句有执行,但数据仍然是旧的,没更新到

这个SQL里就看到了获取到的不是我修改后的值。
licai1210 2012-06-08
  • 打赏
  • 举报
回复
saved_Click把这个方法里面的sql设置一个断点,然后看看获取到的值是不是你修改后的值,然后把这条SQL语句复制到数据库里面的查询分析器里面执行下,看看效果。
Jamescow 2012-06-07
  • 打赏
  • 举报
回复
我简化一下写我的代码完整贴出来吧
在ABC.ASPX文件里,我写如下代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="index_content.aspx.cs" Inherits="admin_index_content" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server" method="post" action="index_content.aspx">
<div>

<br />
简介<br />
<asp:TextBox ID="intro" runat="server" Height="325px" TextMode="MultiLine"
Width="612px"></asp:TextBox>
<br />
<asp:Button ID="saved" runat="server" onclick="saved_Click" Text="保存" />

<br />

</div>
<asp:Label ID="msg" runat="server"></asp:Label>
</form>
</body>
</html>

在这个index_content.aspx.cs里,我写如下代码
public partial class admin_index_content : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(IsPostBack){

DataSet ds = datas.Query("select * from intro where id=1");
intro.Text = ds.Tables[0].Rows[0]["intro"].ToString();
msg.Text = IsPostBack.ToString();//--注意此处在第一次运行时没出现false.
}
else {

string sql = @"update intro set
"intro='" + intro.Text + "' where id=1";
if (datas.ExecuteSQL(sql) > 0)
{
msg.Text = "成功保存" + sql;//---这一句有执行,但数据仍然是旧的,没更新到
}
else
{
msg.Text = "保存失败";
}

}


}
protected void saved_Click(object sender, EventArgs e)
{
//form1.

}
}

请问是哪里出问题呢?真是摸不着头脑了。WINFORM里就没问题,WEBFORM就不行了
Jamescow 2012-06-07
  • 打赏
  • 举报
回复
数据根本就没保存到。还是和保存之前一样的数据。
然后我这样改,居然也不行
if(!IsPostBack){

DataSet ds = datas.Query("select * from intro where id=1");
intro.Text = ds.Tables[0].Rows[0]["intro"].ToString();
maxim.Text = ds.Tables[0].Rows[0]["maxim"].ToString();
address.Text = ds.Tables[0].Rows[0]["address"].ToString();
bot_zongzhi.Text = ds.Tables[0].Rows[0]["bot_zongzhi"].ToString();
bot_linian.Text = ds.Tables[0].Rows[0]["bot_linian"].ToString();
bot_yuanfeng.Text = ds.Tables[0].Rows[0]["bot_yuanfeng"].ToString();
bot_shide.Text = ds.Tables[0].Rows[0]["bot_shide"].ToString();
bot_mubiao.Text = ds.Tables[0].Rows[0]["bot_mubiao"].ToString();
msg.Text = IsPostBack.ToString();
}
else {

string sql = @"update intro set
intro='" + intro.Text + "'," +
"maxim='" + Request.Form["maxim"] + "'," +
"address='" + Request.QueryString["maxim"] + "'," +
"bot_zongzhi='" + bot_zongzhi.Text + "'," +
"bot_linian='" + bot_linian.Text + "'," +
"bot_yuanfeng='" + bot_yuanfeng.Text + "'," +
"bot_shide='" + bot_shide.Text + "'," +
"bot_mubiao='" + bot_mubiao.Text + "' where id=1";
if (datas.ExecuteSQL(sql) > 0)
{
msg.Text = "成功保存" + sql;
}
else
{
msg.Text = "保存失败";
}

}

另外,我是在ASPX里加了几个TEXTBOX、按钮,在同一个ASPX文件下的CS文件里执行上述代码的,会有影响吗?
licai1210 2012-06-07
  • 打赏
  • 举报
回复
DataSet ds = datas.Query("select * from intro where id=1");
intro.Text = ds.Tables[0].Rows[0]["intro"].ToString();
maxim.Text = ds.Tables[0].Rows[0]["maxim"].ToString();
address.Text = ds.Tables[0].Rows[0]["address"].ToString();
bot_zongzhi.Text = ds.Tables[0].Rows[0]["bot_zongzhi"].ToString();
bot_linian.Text = ds.Tables[0].Rows[0]["bot_linian"].ToString();
bot_yuanfeng.Text = ds.Tables[0].Rows[0]["bot_yuanfeng"].ToString();
bot_shide.Text = ds.Tables[0].Rows[0]["bot_shide"].ToString();
bot_mubiao.Text = ds.Tables[0].Rows[0]["bot_mubiao"].ToString();
msg.Text = IsPostBack.ToString();
这段代码应该做个一个方法,在保存成功的时候重新绑定下试试,另外你看看保存成功的时候数据库里面的数据有没有变化

62,267

社区成员

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

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

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

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