为何后台得到的数据与输入的不一致

minzexiang 2010-04-01 05:14:02
这是前台代码。
<body>
<div runat="server">
<form runat="server" id="from1">
<table >
<tr >
<td>时间:</td><td><input id="time" type="text" runat="server" value="" /></td>
<td>发布者:</td><td><input id="name" type="text" runat="server" value="" /></td>
</tr>

<tr>
<td >标题:</td><td colspan="3"><textarea id="title" type="text" runat="server" value="" rows="0" cols="43"></textarea></td>

</tr>
<tr>
<td colspan="4" align="center">内容:</td>

</tr>
<tr>
<td colspan="4"><textarea runat="server" id="neirong" rows="10" cols="50"></textarea> </td>

</tr>

<tr>
<td align="center"><asp:Button ID="ok" Text="保存" runat="server" onclick="ok_Click" /></td>
<td align="center"><asp:Button ID="chakan" Text="查看" runat="server" onclick="chakan_Click" /></td>
<td align="center"><asp:Button ID="clear" Text="清空" runat="server" onclick="clear_Click" /></td>
<td align="center"><asp:Button ID="update" Text="更新" runat="server" onclick="update_Click" /></td>
</tr>
<tr>
<td colspan=4><asp:Label runat="server" ID="oneid" Text="" Visible="false"/></td>
</tr>
</table>

</form>
</div>


</body>

我是通过GridView通过编辑跳到这张页面上来,对应各个值也能正确赋值。现在想修改信息。
如下
protected void update_Click(object sender, EventArgs e)
{

string strname=this.name.Value.Trim();
string strneirong=this.neirong .Value.Trim();
string strtime=this.time.Value.Trim();
string strtitle=this.title.Value.Trim();
int stroneid = Convert.ToInt32( this.oneid.Text.Trim());//这个是对应的ID值我保存在页面的一个控件里。

if (strtime.Length == 0 || strname.Length == 0 || strtitle.Length == 0 || strneirong.Length == 0)
{
Response.Write("<script>alert('信息输入不能为空')</script>");
return;
}

string sql = "update xw set name='" + strname + "',neirong='" + strneirong + "',timetime='" + strtime + "',title='" + strtitle + "' where id='"+stroneid+"'";

SqlConnection conn = new SqlConnection("server=150.0.0.1;uid=pp;pwd=pp;database=ss");
conn.Open();
SqlCommand com = new SqlCommand(sql, conn);
com.ExecuteNonQuery();
Response.Redirect("chakan.aspx");


}

但我发现
string strname=this.name.Value.Trim();
string strneirong=this.neirong .Value.Trim();
string strtime=this.time.Value.Trim();
string strtitle=this.title.Value.Trim();得到的值仍然为之前页面跳转过来的赋值。无论输入任何值都是原来的值。
我是新手大家能帮帮忙不?谢谢!!!
...全文
207 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
minzexiang 2010-04-01
  • 打赏
  • 举报
回复
把第一次赋值的方法放在这里面就好了。谢谢各位了.

protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
//......
}
}
cxm13 2010-04-01
  • 打赏
  • 举报
回复
原来如此 学习了
minzexiang 2010-04-01
  • 打赏
  • 举报
回复
第一次发帖就有那么多好心人帮忙太感谢了!!!我会按照你们的说法去做的。谢谢!!!
hjw01592 2010-04-01
  • 打赏
  • 举报
回复
string sql = "update xw set name='" + strname + "',neirong='" + strneirong + "',timetime='" + strtime + "',title='" + strtitle + "' where id='"+stroneid+"'";


这里加个断点调试下就知道了。
有可能是楼上说的,你page_load时没有if(!ispostback),你输入值因为页面的刷新被冲掉了;
也有可能是你的update语句执行失败,总之调试之后才清楚。
若情水 2010-04-01
  • 打赏
  • 举报
回复
IsPostBack可以检查aspx页是否为传递回服务器的页面,第一次加载为false,再加载则为true
所以要用
if(!IsPostBack)
{
//......
}
shaohaiou 2010-04-01
  • 打赏
  • 举报
回复
点保存的时候会先执行Page_Load里的代码再执行update_Click方法,在Page_Load里加IsPostBack判断就可以避免提交的时候再次赋值了
wx8849 2010-04-01
  • 打赏
  • 举报
回复
gridview 不能直接修改?要转换?
马老虎 2010-04-01
  • 打赏
  • 举报
回复

protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
//......
}
}

ping_2009 2010-04-01
  • 打赏
  • 举报
回复
估计就是page_load时没有if(!ispostback)
丰云 2010-04-01
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 fuda_1985 的回复:]
你最好把你的page_load这个方法爆露出来让我们看看,估计是那里面没加if(!ispostback)
[/Quote]
fuda_1985 2010-04-01
  • 打赏
  • 举报
回复
你最好把你的page_load这个方法爆露出来让我们看看,估计是那里面没加if(!ispostback)

62,254

社区成员

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

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

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

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