昨天做"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