datagrid更新问题
也许是我的疏忽,以前做过N遍的更新问题,今天很耐心的做了10遍以上.还更新不了,我哭啊.一个人看不出错误,求教各位了
这个是程序和数据库的下载地址.
http://www.clingingboy.com/uploads/200603/04_134944_lx.rar
急死了...大家帮忙看看更新的部分就OK
代码在下面
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
string conn_str = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=";
protected void Page_Load(object sender, EventArgs e)
{
BindGrid();
}
public void BindGrid()
{ //从文件Web.config中读取连接字符串
OleDbConnection conn = new OleDbConnection(conn_str + Server.MapPath("list.mdb"));
string sqltxt = "Select * From student";
OleDbCommand cmd = new OleDbCommand(sqltxt, conn);
conn.Open();
dg_order.DataSource = cmd.ExecuteReader();
dg_order.DataBind();
conn.Close();
}
protected void dg_order_EditCommand(object source, DataGridCommandEventArgs e)
{
dg_order.EditItemIndex = (int)e.Item.ItemIndex;
BindGrid();
}
protected void dg_order_UpdateCommand(object source, DataGridCommandEventArgs e)
{
//连接
OleDbConnection conn = new OleDbConnection(conn_str + Server.MapPath("list.mdb"));
//更新
string strSql = "Update student Set age=@age Where ds_id=@id";
OleDbCommand cmdSql = new OleDbCommand(strSql, conn);
int ds_id = Convert.ToInt16((dg_order.DataKeys[e.Item.ItemIndex]));
//查找控件
TextBox myage = new TextBox();
myage = (TextBox)e.Item.FindControl("txtage");
cmdSql.Parameters.Add("@age", OleDbType.Integer).Value = Convert.ToInt16(myage.Text);
OleDbParameter parameterID = new OleDbParameter("@ds_id", OleDbType.Integer);
parameterID.Value = Convert.ToInt16(ds_id);
cmdSql.Parameters.Add(parameterID);
conn.Open();
//执行
cmdSql.ExecuteNonQuery();
conn.Close();
dg_order.EditItemIndex = -1;
//绑定
BindGrid();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DataGrid ID="dg_order" runat="server" Style="z-index: 100; left: 143px; position: absolute;
top: 56px" CellPadding="4" ForeColor="#333333" GridLines="None" Width="416px"
OnEditCommand="dg_order_EditCommand" AutoGenerateColumns="False" Font-Size="12px"
OnUpdateCommand="dg_order_UpdateCommand" DataKeyField="ds_id">
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<EditItemStyle BackColor="#2461BF" />
<SelectedItemStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<AlternatingItemStyle BackColor="White" />
<ItemStyle BackColor="#EFF3FB" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<Columns>
<asp:BoundColumn DataField="name" HeaderText="定单号" ReadOnly="True"></asp:BoundColumn>
<asp:BoundColumn DataField="address" HeaderText="address" ReadOnly="True"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="age">
<ItemTemplate>
<asp:Label runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.age") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtage" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.age") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:EditCommandColumn CancelText="取消" EditText="编辑" UpdateText="更新"></asp:EditCommandColumn>
</Columns>
</asp:DataGrid>
</div>
</form>
</body>
</html>
问题点数:20、回复次数:5Top
1 楼xiahouwen(武眉博<活靶子.NET>)回复于 2006-03-04 13:56:37 得分 20
protected void Page_Load(object sender, EventArgs e)
{
if(Page.IsPostBack)
{
BindGrid();
}
}Top
2 楼xiahouwen(武眉博<活靶子.NET>)回复于 2006-03-04 13:56:53 得分 0
sorry
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
BindGrid();
}
}Top
3 楼califord(远方)回复于 2006-03-04 13:59:53 得分 0
错在哪里?没有时间看,给你一个资料
http://www.ininnet.com/Content.aspx?ContentID=237,38
上面有下载的,也有源码,还有注释Top
4 楼flyinging(一路走来)回复于 2006-03-04 14:02:47 得分 0
不错Top
5 楼xzzhangg()回复于 2006-03-04 14:03:09 得分 0
晕,可以了.休息去了...郁闷啊,差几行代码郁闷我一天.谢谢楼上的朋友Top




