| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 490 人关注过本帖
标题:datalist的手写代码更新记录的怪问题?
只看楼主 加入收藏
wuguoming
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2007-5-11
收藏
 问题点数:0 回复次数:5 
datalist的手写代码更新记录的怪问题?

昨天做"datalist的手写代码更新记录"的时候遇到一个难题:就是运行后更改了数据按了更新按钮,数据还是没有更改,找了一个下午也没能找出那里错了,试过在protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)事件的最后一个花括号}处加了一个断点来调试(也就是说在55行加了一个断点),发现string Score =(( TextBox)e.Item.FindControl("TextBox1")).Text和string Comment = ((TextBox)e.Item.FindControl("TextBox2")).Text的值是原来没有更新之前的值,而不是更新之后的值,真的很奇怪!望各位大侠帮忙看一下错在那里.

075010.aspx
1<%@ Page Language="C#" AutoEventWireup="true" CodeFile="070510.aspx.cs" Inherits="_070510" %>
2
3<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4
5<html xmlns="http://www.w3.org/1999/xhtml" >
6<head runat="server">
7 <title>Untitled Page</title>
8</head>
9<body>
10 <form id="form1" runat="server">
11 <div>
12 <asp:DataList ID="DataList1" runat="server" OnCancelCommand="DataList1_CancelCommand" OnEditCommand="DataList1_EditCommand" OnUpdateCommand="DataList1_UpdateCommand">
13 <ItemTemplate>
14 <asp:LinkButton ID="LinkButton1" runat="server" CommandName="edit">修改</asp:LinkButton>
15
16 <asp:LinkButton ID="LinkButton2" runat="server" CommandName="delete">删除</asp:LinkButton>
17
18 <%# Eval("HomeworkID")%>
19
20 <%# Eval("HScore")%>
21
22 <%# Eval("HComment")%>
23 </ItemTemplate>
24 <EditItemTemplate>
25 <asp:LinkButton ID="LinkButton1" runat="server" CommandName="update">更新</asp:LinkButton>
26
27 <asp:LinkButton ID="LinkButton2" runat="server" CommandName="cancel">删除</asp:LinkButton>
28
29 <%# Eval("HomeworkID")%>
30
31 <asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("HScore")%>'></asp:TextBox>
32 <asp:TextBox ID="TextBox2" runat="server" Text='<%# Eval("HComment")%>'></asp:TextBox>
33
34 </EditItemTemplate>
35 </asp:DataList>
36
37 </div>
38 </form>
39</body>
40</html>
41

070510.aspx.cs

1using System;
2using System.Data;
3using System.Configuration;
4using System.Collections;
5using System.Web;
6using System.Web.Security;
7using System.Web.UI;
8using System.Web.UI.WebControls;
9using System.Web.UI.WebControls.WebParts;
10using System.Web.UI.HtmlControls;
11using System.Data.OleDb;
12
13public partial class _070510 : System.Web.UI.Page
14{
15
16 private void bind()
17 {
18 OleDbConnection conn = new OleDbConnection(ConfigurationSettings.AppSettings["connstr"]);
19 OleDbCommand comm = new OleDbCommand("select * from Homework", conn);
20 OleDbDataAdapter da = new OleDbDataAdapter(comm);
21 DataSet ds = new DataSet();
22 da.Fill(ds);
23 DataList1.DataSource = ds;
24 DataList1.DataKeyField = "HomeworkID";
25 DataList1.DataBind();
26 }
27 protected void Page_Load(object sender, EventArgs e)
28 {
29 bind();
30 }
31
32 protected void DataList1_CancelCommand(object source, DataListCommandEventArgs e)
33 {
34 DataList1.EditItemIndex = -1;
35 bind();
36 }
37 protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)
38 {
39 DataList1.EditItemIndex = e.Item.ItemIndex;
40 bind();
41 }
42 protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
43 {
44 int HomeworkID=Convert.ToInt32( DataList1.DataKeys[e.Item.ItemIndex]);
45 string Score =(( TextBox)e.Item.FindControl("TextBox1")).Text;
46 string Comment = ((TextBox)e.Item.FindControl("TextBox2")).Text;
47 OleDbConnection conn = new OleDbConnection(ConfigurationSettings.AppSettings["connstr"]);
48 string updateString = "update Homework set HScore='"+ Score+"',HComment='"+Comment+"'where HomeworkID="+HomeworkID+"";
49 conn.Open();
50 OleDbCommand comm = new OleDbCommand(updateString, conn);
51 comm.ExecuteNonQuery();
52 conn.Close();
53 DataList1.EditItemIndex = -1;
54 bind();
55 }
56}
57

搜索更多相关主题的帖子: datalist 代码 记录 
2007-05-11 13:02
tel1982
Rank: 3Rank: 3
来 自:水星
等 级:新手上路
威 望:8
帖 子:852
专家分:0
注 册:2006-10-21
收藏
得分:0 
以下是引用wuguoming在2007-5-11 13:02:55的发言:

昨天做"datalist的手写代码更新记录"的时候遇到一个难题:就是运行后更改了数据按了更新按钮,数据还是没有更改,找了一个下午也没能找出那里错了,试过在protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)事件的最后一个花括号}处加了一个断点来调试(也就是说在55行加了一个断点),发现string Score =(( TextBox)e.Item.FindControl("TextBox1")).Text和string Comment = ((TextBox)e.Item.FindControl("TextBox2")).Text的值是原来没有更新之前的值,而不是更新之后的值,真的很奇怪!望各位大侠帮忙看一下错在那里.

075010.aspx
1<%@ Page Language="C#" AutoEventWireup="true" CodeFile="070510.aspx.cs" Inherits="_070510" %>
2
3<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4
5<html xmlns="http://www.w3.org/1999/xhtml" >
6<head runat="server">
7 <title>Untitled Page</title>
8</head>
9<body>
10 <form id="form1" runat="server">
11 <div>
12 <asp:DataList ID="DataList1" runat="server" OnCancelCommand="DataList1_CancelCommand" OnEditCommand="DataList1_EditCommand" OnUpdateCommand="DataList1_UpdateCommand">
13 <ItemTemplate>
14 <asp:LinkButton ID="LinkButton1" runat="server" CommandName="edit">修改</asp:LinkButton>
15
16 <asp:LinkButton ID="LinkButton2" runat="server" CommandName="delete">删除</asp:LinkButton>
17
18 <%# Eval("HomeworkID")%>
19
20 <%# Eval("HScore")%>
21
22 <%# Eval("HComment")%>
23 </ItemTemplate>
24 <EditItemTemplate>
25 <asp:LinkButton ID="LinkButton1" runat="server" CommandName="update">更新</asp:LinkButton>
26
27 <asp:LinkButton ID="LinkButton2" runat="server" CommandName="cancel">删除</asp:LinkButton>
28
29 <%# Eval("HomeworkID")%>
30
31 <asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("HScore")%>'></asp:TextBox>
32 <asp:TextBox ID="TextBox2" runat="server" Text='<%# Eval("HComment")%>'></asp:TextBox>
33
34 </EditItemTemplate>
35 </asp:DataList>
36
37 </div>
38 </form>
39</body>
40</html>
41

070510.aspx.cs

1using System;
2using System.Data;
3using System.Configuration;
4using System.Collections;
5using System.Web;
6using System.Web.Security;
7using System.Web.UI;
8using System.Web.UI.WebControls;
9using System.Web.UI.WebControls.WebParts;
10using System.Web.UI.HtmlControls;
11using System.Data.OleDb;
12
13public partial class _070510 : System.Web.UI.Page
14{
15
16 private void bind()
17 {
18 OleDbConnection conn = new OleDbConnection(ConfigurationSettings.AppSettings["connstr"]);
19 OleDbCommand comm = new OleDbCommand("select * from Homework", conn);
20 OleDbDataAdapter da = new OleDbDataAdapter(comm);
21 DataSet ds = new DataSet();
22 da.Fill(ds);
23 DataList1.DataSource = ds;
24 DataList1.DataKeyField = "HomeworkID";
25 DataList1.DataBind();
26 }
27 protected void Page_Load(object sender, EventArgs e)
28 {
29 bind();
30 }
31
32 protected void DataList1_CancelCommand(object source, DataListCommandEventArgs e)
33 {
34 DataList1.EditItemIndex = -1;
35 bind();
36 }
37 protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)
38 {
39 DataList1.EditItemIndex = e.Item.ItemIndex;
40 bind();
41 }
42 protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
43 {
44 int HomeworkID=Convert.ToInt32( DataList1.DataKeys[e.Item.ItemIndex]);
45 string Score =(( TextBox)e.Item.FindControl("TextBox1")).Text;
46 string Comment = ((TextBox)e.Item.FindControl("TextBox2")).Text;
47 OleDbConnection conn = new OleDbConnection(ConfigurationSettings.AppSettings["connstr"]);
48 string updateString = "update Homework set HScore='"+ Score+"',HComment='"+Comment+"'where HomeworkID="+HomeworkID;
49 conn.Open();
50 OleDbCommand comm = new OleDbCommand(updateString, conn);
51 comm.ExecuteNonQuery();
52 conn.Close();
53 DataList1.EditItemIndex = -1;
54 bind();
55 }
56}
57

把红色标记的改了看行不行。


Face up to life with smiles no matter what happens
2007-05-11 15:00
wuguoming
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2007-5-11
收藏
得分:0 
@tel1982 按你说的改了还是不行

坚持就是胜利
2007-05-11 15:31
guming
Rank: 4
等 级:业余侠客
威 望:5
帖 子:329
专家分:277
注 册:2006-11-9
收藏
得分:0 
if(!IsPostBack)
{
bind();
}

不想停留。。。
2007-05-12 12:48
wuguoming
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2007-5-11
收藏
得分:0 
@guming
行了,问题解决了,感谢你的帮忙!
原来
27 protected void Page_Load(object sender, EventArgs e)
28 {
29 bind();
30 }

if(!this.IsPostBack)
{
bind();
}
区别是这么大
我的程序就是因为没有
if(!this.IsPostBack)
{
}
所以按更新的时候触发了Page_Load事件再bind()一次把我更新的数据还原为原来的数据

坚持就是胜利
2007-05-12 15:33
tel1982
Rank: 3Rank: 3
来 自:水星
等 级:新手上路
威 望:8
帖 子:852
专家分:0
注 册:2006-10-21
收藏
得分:0 

这一点我倒是没有看到。不过解决了就好。

Face up to life with smiles no matter what happens
2007-05-15 07:13
快速回复:datalist的手写代码更新记录的怪问题?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016739 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved